Наследование: implements flow\RequestInterface
Пример #1
0
 /**
  * uploadFile
  *
  * @param  string $mediaDir
  * @param  Request $request
  * @throws HttpException
  *
  * @return string
  */
 public static function uploadFile($mediaDir, Request $request)
 {
     $uploadedFile = null;
     if ($request->files->get('file')) {
         /** @var \Symfony\Component\HttpFoundation\File\UploadedFile $fileFromFileBag */
         $fileFromFileBag = $request->files->get('file');
         $uploadedFile = array('name' => $fileFromFileBag->getClientOriginalName(), 'type' => $fileFromFileBag->getMimeType(), 'tmp_name' => $fileFromFileBag->getPathname(), 'error' => $fileFromFileBag->getError(), 'size' => $fileFromFileBag->getSize());
     }
     $config = new FlowConfig();
     $config->setTempDir($mediaDir);
     $flowRequest = new FlowRequest($request->request->all(), $uploadedFile);
     $flowFile = new FlowFile($config, $flowRequest);
     if ($request->getMethod() === 'GET') {
         if ($flowFile->checkChunk()) {
             header("HTTP/1.1 200 Ok");
         } else {
             header("HTTP/1.1 204 No Content");
             return false;
         }
     } else {
         if ($flowFile->validateChunk()) {
             rename($uploadedFile['tmp_name'], $flowFile->getChunkPath($flowRequest->getCurrentChunkNumber()));
         } else {
             throw new HttpException(400);
         }
     }
     $filename = $mediaDir . '/' . $uploadedFile['name'];
     if ($flowFile->validateFile() && $flowFile->save($filename)) {
         return $filename;
     } else {
         // This is not a final chunk, continue to upload
     }
     return false;
 }
Пример #2
0
 /**
  * @param ImageUploadRequest $request
  * @return \Illuminate\Http\Response|JsonResponse
  */
 public function upload(ImageUploadRequest $request, \Flow\File $file, \Flow\Request $flowRequest)
 {
     // We switch to flow.js
     if ($request->getMethod() === 'GET') {
         if (!$file->checkChunk()) {
             return \Response::make('', 204);
         }
     } else {
         if ($file->validateChunk()) {
             $file->saveChunk();
         } else {
             return \Response::make('', 400);
         }
     }
     $filename = FileHelper::generateUniqueName($flowRequest->getFileName());
     $destination = FileHelper::getTempDirectory() . DIRECTORY_SEPARATOR . $filename;
     if ($file->validateFile() && $file->save($destination)) {
         $tempFile = Temp::createFromFile($destination, $flowRequest->getFileName());
         return new JsonResponse(['id' => $tempFile->getKey()]);
     }
 }
Пример #3
0
 /**
  * Handle Image Upload
  *
  * @throws \Exception
  *
  * @return ApiFileService
  */
 private function handleImageUpload()
 {
     try {
         $config = new FlowConfig();
         $request = new FlowRequest();
         $basic = new Basic();
         $config->setTempDir(storage_path() . '/tmp');
         $config->setDeleteChunksOnSave(false);
         $totalSize = $request->getTotalSize();
         $uploadFile = $request->getFile();
         $fileName = md5($request->getFileName());
         $extension = explode('.', $request->getFileName())[1];
         $extraNumber = 1;
         if ($totalSize && $totalSize > 1024 * 1024 * 4) {
             throw new \Exception('File size exceeds 4MB', 400);
         }
         while ($this->isNameDuplicated($fileName . '.' . $extension, $this->location)) {
             $fileName = $fileName . $extraNumber;
             ++$extraNumber;
         }
         $fileName = $fileName . '.' . $extension;
         if ($basic->save($this->directory . $this->location . $fileName, $config, $request)) {
             $file = $this->handleImageSave($fileName, $this->location);
             return $file;
         }
         throw new \Exception('unable to save file', 500);
     } catch (\Exception $e) {
         throw new \Exception($e->getMessage(), $e->getCode());
     }
 }
Пример #4
0
 /**
  * @covers ::save
  */
 public function testFile_save_preProcess()
 {
     //// Setup test
     $this->requestArr['flowTotalChunks'] = 1;
     $this->requestArr['flowTotalSize'] = 10;
     $processCalled = false;
     $process = function ($chunk) use(&$processCalled) {
         $processCalled = true;
     };
     $this->config->setPreprocessCallback($process);
     $request = new Request($this->requestArr);
     $file = new File($this->config, $request);
     $chunkPrefix = sha1($request->getIdentifier()) . '_';
     $chunk = vfsStream::newFile($chunkPrefix . '1', 0777);
     $chunk->setContent('1234567890');
     $this->vfs->addChild($chunk);
     $filePath = $this->vfs->url() . DIRECTORY_SEPARATOR . 'file';
     //// Actual test
     $this->assertTrue($file->save($filePath));
     $this->assertTrue(file_exists($filePath));
     $this->assertEquals($request->getTotalSize(), filesize($filePath));
     $this->assertTrue($processCalled);
 }
Пример #5
0
 /**
  * @covers ::__construct
  */
 public function testRequest_construct_withCustFiles()
 {
     $request = new Request(null, $this->filesArr['file']);
     $this->assertSame($this->filesArr['file'], $request->getFile());
 }
Пример #6
0
 public function upload()
 {
     require_once './vendor/autoload.php';
     $request = new Request();
     $staticServerRoot = './Public/ng/app/';
     $chunk = 'statics/chunks_temp_folder';
     $uploaded = 'statics/uploaded';
     $fileNameToSave = md5($request->getIdentifier()) . '.' . pathinfo($request->getFileName())['extension'];
     $pathReturn = $uploaded . '/' . $fileNameToSave;
     $savedFile = $staticServerRoot . $pathReturn;
     $config = new Config();
     $config->setTempDir($staticServerRoot . $chunk);
     $file = new File($config);
     if ($_SERVER['REQUEST_METHOD'] === 'GET') {
         if ($file->checkChunk()) {
             header("HTTP/1.1 200 Ok");
         } else {
             header("HTTP/1.1 204 No Content");
             return;
         }
     } else {
         if ($file->validateChunk()) {
             $file->saveChunk();
         } else {
             // error, invalid chunk upload request, retry
             header("HTTP/1.1 400 Bad Request");
             return;
         }
     }
     if ($file->validateFile() && $file->save($savedFile)) {
         //save to db
         $urlPre = 'http://' . $_SERVER['HTTP_HOST'] . '/';
         $id = M('materials')->add(array('type' => C('MATERIAL_IMAGE'), 'file_name' => $fileNameToSave, 'pic_url' => $urlPre . $pathReturn, 'title' => ''));
         //http://exp/Public/ng/app/statics/uploaded/
         // File upload was completed
         echo json_encode(array('path' => $urlPre . $pathReturn, 'id' => $id));
     } else {
         // This is not a final chunk, continue to upload
         echo 'no';
     }
 }
Пример #7
0
 public function testConfig_hashNameCallback()
 {
     $request = new Request($this->requestArr);
     $expHash = sha1($request->getIdentifier());
     $this->assertSame($expHash, Config::hashNameCallback($request));
 }
 public function callbackUpload()
 {
     $config = new Config();
     $config->setTempDir($this->getTempFolder());
     $file = new File($config);
     $request = new Request();
     if ($_SERVER['REQUEST_METHOD'] === 'GET') {
         if ($file->checkChunk()) {
             header("HTTP/1.1 200 Ok");
         } else {
             header("HTTP/1.1 204 No Content");
             exit;
         }
     } else {
         if ($file->validateChunk()) {
             $file->saveChunk();
         } else {
             // error, invalid chunk upload request, retry
             header("HTTP/1.1 400 Bad Request");
             exit;
         }
     }
     if ($file->validateFile() && $file->save($this->getUploadFolder() . '/' . $request->getFileName())) {
         // File upload was completed
         $file = Yii::$app->storage->addFile($this->getUploadFolder() . '/' . $request->getFileName(), $request->getFileName(), 0, true);
         if ($file) {
             @unlink($this->getUploadFolder() . '/' . $request->getFileName());
             $image = Yii::$app->storage->addImage($file->id);
             if ($image) {
                 $image->applyFilter('small-crop');
                 $model = $this->model;
                 $row = $this->getModelItem();
                 if ($row) {
                     $row->flowSaveImage($image);
                     return 'done';
                 }
             }
         }
     } else {
         // This is not a final chunk, continue to upload
     }
 }