processAll() public method

Process entire submitted request
public processAll ( ) : array
return array Files and response headers
Beispiel #1
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 #2
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;
 }