setPathResolver() public method

Set path resolver
public setPathResolver ( FileUpload\PathResolver\PathResolver $pr )
$pr FileUpload\PathResolver\PathResolver
 /**
  * Create new instance of FileUpload with the preset modules
  * @param  array $upload
  * @param  array $server
  * @return FileUpload
  */
 public function create($upload, $server)
 {
     $fileupload = new FileUpload($upload, $server);
     $fileupload->setPathResolver($this->pathresolver);
     $fileupload->setFileSystem($this->filesystem);
     foreach ($this->validators as $validator) {
         $fileupload->addValidator($validator);
     }
     return $fileupload;
 }
Beispiel #2
0
 public function testGenerator()
 {
     $generator = new MD5();
     $playground_path = __DIR__ . '/../playground';
     $filename = "picture.jpg";
     $new_filename = md5("picture") . ".jpg";
     $server = array('CONTENT_TYPE' => 'image/jpg', 'CONTENT_LENGTH' => 30321);
     $file = array('tmp_name' => $playground_path . '/real-image.jpg', 'name' => 'real-image.jpg', 'size' => 30321, 'type' => 'image/jpg', 'error' => 0);
     $fileUpload = new FileUpload($file, $server, $generator);
     $fileUpload->setFileSystem(new Mock());
     $fileUpload->setPathResolver(new Path($playground_path . "/uploaded"));
     $this->assertEquals($generator->getFileName($filename, "image/jpg", "asdf.jpg", 0, "100", $fileUpload), $new_filename);
 }
Beispiel #3
0
 public function testFileIsUploadedDespiteAlreadyExistingOnTheFileSystem()
 {
     $generator = new MD5(true);
     //true would override files with the same name
     $playground_path = __DIR__ . '/../../playground';
     $filename = "real-image.jpg";
     $newFileName = md5("real-image") . '.jpg';
     $server = array('CONTENT_TYPE' => 'image/jpg', 'CONTENT_LENGTH' => 30321);
     $file = array('tmp_name' => $playground_path . '/uploaded/real-image.jpg', 'name' => 'real-image.jpg', 'size' => 30321, 'type' => 'image/jpg', 'error' => 0);
     $fileUpload = new FileUpload($file, $server, $generator);
     $fileUpload->setFileSystem(new Mock());
     $fileUpload->setPathResolver(new Path($playground_path));
     $this->assertEquals($generator->getFileName($filename, "image/jpg", "asdf.jpg", 0, "100", $fileUpload), $newFileName);
 }
Beispiel #4
0
 public function testGenerator()
 {
     $generator = new Random(32);
     $playground_path = __DIR__ . '/../playground';
     $filename = "picture.jpg";
     $filesystem = new Mock();
     $resolver = new Simple($playground_path . '/uploaded');
     $server = array('CONTENT_TYPE' => 'image/jpg', 'CONTENT_LENGTH' => 30321);
     $file = array('tmp_name' => $playground_path . '/real-image.jpg', 'name' => 'real-image.jpg', 'size' => 30321, 'type' => 'image/jpg', 'error' => 0);
     $fileUpload = new FileUpload($file, $server, $generator);
     $fileUpload->setPathResolver($resolver);
     $fileUpload->setFileSystem($filesystem);
     $new_name = $generator->getFileName($filename, "image/jpg", "asdf.jpg", 0, "100", $fileUpload);
     $this->assertEquals(32, strrpos($new_name, "."));
     $this->assertEquals(substr($new_name, strrpos($new_name, ".")), ".jpg");
 }
Beispiel #5
0
 public function testCallableGenerator()
 {
     function generateName()
     {
         return func_get_arg(0);
     }
     $generator = new Custom("FileUpload\\FileNameGenerator\\generateName");
     $playground_path = __DIR__ . '/../playground';
     $filename = "picture.jpg";
     $server = array('CONTENT_TYPE' => 'image/jpg', 'CONTENT_LENGTH' => 30321);
     $file = array('tmp_name' => $playground_path . '/real-image.jpg', 'name' => 'real-image.jpg', 'size' => 30321, 'type' => 'image/jpg', 'error' => 0);
     $fileUpload = new FileUpload($file, $server, $generator);
     $fileUpload->setFileSystem(new Mock());
     $fileUpload->setPathResolver(new Simple($playground_path . "/uploaded"));
     $new_filename = generateName($filename, "image/jpg", "asdf.jpg", 0, "100", $fileUpload);
     $this->assertEquals($generator->getFileName($filename, "image/jpg", "asdf.jpg", 0, "100", $fileUpload), $new_filename);
 }
Beispiel #6
0
 public function upload(Request $req, Response $res)
 {
     if (!$req->isPost()) {
         throw new \Chalk\Exception("Upload action only accepts POST requests");
     }
     $dir = $this->chalk->config->dataDir->dir('upload', true);
     $uploader = new FileUpload($_FILES['files'], $req->servers());
     $uploader->setPathResolver(new PathResolver\Simple($dir->name()));
     $uploader->setFileSystem(new FileSystem\Simple());
     list($uploads, $headers) = $uploader->processAll();
     foreach ($uploads as $upload) {
         if (isset($upload->path)) {
             $content = isset($req->route['params']['content']) ? $this->em($req->info)->id($req->route['params']['content']) : $this->em($req->info)->create();
             $view = $content->isNew() ? 'content/thumb' : 'content/card-upload';
             if (!$this->em->isPersisted($content)) {
                 $content->newFile = new \Coast\File($upload->path);
                 $this->em->persist($content);
             } else {
                 $content->move(new \Coast\File($upload->path));
             }
             $this->em->flush();
             unset($upload->path);
             $upload->html = $this->view->render($view, ['content' => $content, 'covered' => true, 'isEditAllowed' => (bool) $req->isEditAllowed] + (array) $req->view, 'core')->toString();
         }
     }
     return $res->headers($headers)->json(['files' => $uploads]);
 }
Beispiel #7
0
 /**
  * Process of uploading file
  *
  * @param Closure $callback
  * @param Validator $validator
  * @return array
  */
 public function process(Closure $callback = null, Validator $validator = null)
 {
     $fileId = $this->request->get($this->fileIdAttr);
     $files = $this->request->files;
     if (!$fileId) {
         $file = new File();
         $file->user_id = $this->user->id;
         $file->addNewFromFiles($files);
         $fileId = $file->id;
     }
     $localFileSystem = $this->localFileSystem;
     $remoteFileSystem = $this->remoteFileSystem;
     $tempPath = $this->tempPath . '/' . $fileId;
     $destPath = $this->destPath . '/' . $fileId;
     $tempAbsolutePath = $this->basicPath . $this->tempPath . '/' . $fileId;
     !$localFileSystem->has($tempPath) ? $localFileSystem->getAdapter()->createDir($tempAbsolutePath) : '';
     if (!$validator) {
         $validator = $this->validator;
     }
     $fileSystem = new FileUploaderSystem();
     $pathResolver = new PathResolver($this->basicPath . $this->tempPath . '/' . $fileId);
     $filesArray = $this->getFilesInfoArray($files);
     $serverArray = $this->request->server->all();
     $fileUploader = new FileUpload($filesArray['file'], $serverArray);
     $fileUploader->setPathResolver($pathResolver);
     $fileUploader->setFileSystem($fileSystem);
     $fileUploader->addValidator($validator);
     $callbackResult = null;
     //callback
     $fileUploader->addCallback('completed', function (FileUploadFile $files) use($callback, $fileId, $tempPath, $destPath, $localFileSystem, $remoteFileSystem, &$callbackResult) {
         $file = new File($fileId);
         $file->path = $destPath;
         $file->status = $file::STATUS_UPLOADED;
         $file->size = $localFileSystem->get($tempPath . '/' . $files->name)->getSize();
         $file->save();
         $fileContent = $localFileSystem->read($tempPath . '/' . $files->name);
         $remoteFileSystem->write($destPath . '/' . $files->name, $fileContent);
         $localFileSystem->delete($tempPath . '/' . $files->name);
         $localFileSystem->delete($tempPath);
         if ($callback) {
             $callbackResult = $callback($file);
         }
     });
     list($fileUploadObject, $headers) = $fileUploader->processAll();
     $properties = get_object_vars($fileUploadObject[0]);
     // O_O mama mia
     /* foreach ($properties as $property=> $value) {
            if ($property != 'path') {
                $info[$property] = $value;
            }
        } */
     unset($properties['path']);
     $result = array('info' => $properties);
     if ($result['info']['error'] != 0) {
         $file->delete();
     }
     if ($callbackResult) {
         $result = array_merge($result, $callbackResult);
     }
     $result[$this->fileIdAttr] = $fileId;
     $this->lastUploadedResult = $result;
     return $result;
 }