public function upload($input) { $this->mergeConfig($input); $fineUploader = $this->createFineUploaderInstance($this->mergeFineUploaderConfig($input, $this->config->get('fine_uploader'))); $filePath = $fineUploader->getName(); // Upload the file to the temporary directory so it can be forwarded // to the proper storage by the storage engine $file = new RootFile($filePath); $tempPath = $this->uploaderPath($this->config->get('temp_folder')); $upload = $fineUploader->handleUpload($tempPath, $file->getFilename()); if (isset($upload['error'])) { return (new ErrorResponse($upload['error']))->toArray(); } // Chunked upload if (isset($input['qqpartindex'])) { if ($input['qqpartindex'] + 1 < $input['qqtotalparts']) { return (new ChunkResponse())->toArray(); } } // Get the full temporary path of the file, something like // storage/uploads/temp/{qquid}/file.ext $newTempPath = sprintf('%s/%s/%s', $tempPath, $upload['uuid'], $file->getFilename()); $tempFile = new RootFile($newTempPath); // Get a new name based on the naming strategy $newName = $this->namingStrategy->generateName($tempFile); if ($newName !== $tempFile->getFilename()) { $renamedTempPath = sprintf('%s/%s', $tempFile->getPath(), $newName); rename($newTempPath, $renamedTempPath); $tempFile = new RootFile($renamedTempPath); } // This is the relative path from the uploader folder // i.e. /products/1 $uploaderPath = $this->getStoragePath($input); $tempFile->setUploaderPath($uploaderPath); $core = function (RootFile $tempFile) use($uploaderPath) { return $this->storage->store($tempFile, $uploaderPath); }; $storage = $this->middleware->peel($tempFile, $core); // TODO: Error handling // Remove temp directory optimus_delete_directory(sprintf('%s/%s', $tempPath, $upload['uuid'])); $responseClass = $this->config->get('success_response_class'); $response = new $responseClass($tempFile, $upload, $storage); if (!$response instanceof SuccessfulResponseInterface) { throw new Exception("Response class " . get_class($response) . " must implement " . "Optimus\\FineuploaderServer\\Response\\ResponseInterface"); } return $this->prepareUploadSuccessfulResponse($response->toArray(), $tempFile); }
function optimus_delete_directory($dir) { if (!file_exists($dir)) { return true; } if (!is_dir($dir)) { return unlink($dir); } foreach (scandir($dir) as $item) { if ($item == '.' || $item == '..') { continue; } if (!optimus_delete_directory($dir . DIRECTORY_SEPARATOR . $item)) { return false; } } return rmdir($dir); }