/** * 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()); } }
/** * @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()]); } }
/** * @covers ::__construct * @covers ::getParam * @covers ::getFileName * @covers ::getTotalSize * @covers ::getIdentifier * @covers ::getRelativePath * @covers ::getTotalChunks * @covers ::getDefaultChunkSize * @covers ::getCurrentChunkNumber * @covers ::getCurrentChunkSize * @covers ::getFile * @covers ::isFustyFlowRequest */ public function testRequest_construct_withCustomRequest() { $request = new Request($this->requestArr); $this->assertSame('prettify.js', $request->getFileName()); $this->assertSame(100, $request->getTotalSize()); $this->assertSame('13632-prettifyjs', $request->getIdentifier()); $this->assertSame('home/prettify.js', $request->getRelativePath()); $this->assertSame(42, $request->getTotalChunks()); $this->assertSame(1048576, $request->getDefaultChunkSize()); $this->assertSame(1, $request->getCurrentChunkNumber()); $this->assertSame(10, $request->getCurrentChunkSize()); $this->assertSame(null, $request->getFile()); $this->assertFalse($request->isFustyFlowRequest()); }
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'; } }
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 } }