Exemplo n.º 1
0
 public function uploadAction()
 {
     if ($this->request->hasFiles()) {
         $uploads = $this->request->getUploadedFiles();
         $this->view->disable();
         $uploaded = true;
         foreach ($uploads as $fileObj) {
             if (!$this->mediaModel->initFile($fileObj)) {
                 $uploaded = false;
             }
         }
         if (!$uploaded) {
             $error = implode("\n", $this->mediaModel->getError());
             $this->response->setStatusCode(406, $error);
             $this->response->setContent($error);
         } else {
             $this->response->setStatusCode(200, SUCCESS_MESSAGE);
         }
         return $this->response->send();
     }
     $this->assets->addCss('/core/assets/css/dropzone.css', false);
     $this->assets->addJs('/core/assets/js/dropzone.js', false);
     $this->view->acceptExt = implode(",", MediaType::getExtensionAllowed());
 }
Exemplo n.º 2
0
 /**
  * Input file for media, using for upload file
  * @param File Object $fileObj File upload by user
  * @return boolean           true if all ok. Otherwise, false
  */
 public function initFile($fileObj)
 {
     $fileExt = $fileObj->getExtension();
     $filesAccept = MediaType::getExtensionAllowed();
     // Check if file extension's allowed
     if (!in_array($fileExt, $filesAccept)) {
         return $this->setError(MEDIA_FILE_NOT_ACCEPT . ": " . $fileExt);
     }
     // determine directory for this file. <username>/<file type>/<year>/<month>/<filename>
     $userName = $this->getDI()->getAuth()->getUsername();
     $year = date("Y");
     $month = date("M");
     $fileName = $fileObj->getName();
     $fileType = MediaType::getTypeFromExt($fileExt);
     // generate path of file
     $serverPath = $userName . DS . $fileType->getName() . DS . $year . DS . $month . DS . $fileName;
     $localPath = $fileObj->getTempName();
     if (!file_exists($localPath)) {
         return $this->setError(MEDIA_TEMP_NOT_FOUND);
     }
     if ($this->fileSystem->checkFileExists($serverPath)) {
         return $this->setError(MEDIA_ALREADY_EXISTS);
     }
     if (!$this->fileSystem->uploadFile($localPath, $serverPath)) {
         return $this->setError(MEDIA_TEMP_NOT_FOUND);
     }
     $uploadStatus = $this->saveToDB($userName, $fileType->getId(), time(), $fileName);
     if (!$uploadStatus) {
         return $this->setError(MEDIA_UPLOAD_ERROR);
     }
     // Update analytic file
     $config = $this->fileSystem->getConfigFile($userName);
     $defaultConfig = MediaType::getConfig();
     $config = array_merge($defaultConfig, $config);
     $config[$fileType->getName()]++;
     $this->fileSystem->saveConfigFile($userName, $config);
     if ($fileType->getName() == "Images") {
         $this->generateThumb($localPath, $serverPath);
     }
     return true;
 }