Ejemplo n.º 1
0
 public function upload(File $file)
 {
     if ($file->getError()) {
         throw new Exception\IOException('ERR_FILE_UPLOAD_FAILED');
     }
     $originalName = $file->getName();
     $tmp = $file->getTempName();
     $fileSize = $file->getSize();
     $type = $file->getType();
     $filenameArray = explode(".", $originalName);
     $fileExtension = strtolower(array_pop($filenameArray));
     $originalFileName = implode('.', $filenameArray);
     $fileName = Tag::friendlyTitle($originalFileName);
     $fileHash = null;
     if ($fileName == '-') {
         $fileName = Text::random(Text::RANDOM_ALNUM, 6);
     }
     //hash file less then 10M
     if ($fileSize < 1048576 * 10) {
         $fileHash = hash_file('CRC32', $tmp, false);
     }
     if (false === strpos($type, 'image')) {
         $isImage = 0;
     } else {
         $isImage = 1;
     }
     $fileinfo = array('title' => $originalFileName, 'status' => 'published', 'storageAdapter' => 'local', 'originalName' => $originalName, 'fileSize' => $fileSize, 'mimeType' => $type, 'fileExtension' => $fileExtension, 'fileHash' => $fileHash, 'isImage' => $isImage, 'fileName' => $fileName . '.' . $fileExtension, 'createdAt' => time());
     if ($isImage) {
         $image = getimagesize($tmp);
         $fileinfo['imageWidth'] = $image[0];
         $fileinfo['imageHeight'] = $image[1];
     }
     $filesystem = $this->getDI()->getFileSystem();
     $path = md5(microtime());
     $path = str_split($path, 2);
     $pathlevel = $this->getUploadPathLevel();
     $pathlevel = $pathlevel > 6 ? 6 : $pathlevel;
     $path = array_slice($path, 0, $pathlevel);
     $filePath = implode('/', $path);
     $path = $filePath . '/' . $fileName . '.' . $fileExtension;
     $fileinfo['filePath'] = $filePath;
     $this->assign($fileinfo);
     if ($this->save()) {
         if (!$filesystem->has($path)) {
             if ($filesystem->write($path, file_get_contents($tmp))) {
                 unlink($tmp);
             } else {
                 throw new Exception\IOException('ERR_FILE_MOVE_TO_STORAGE_FAILED');
             }
         } else {
             throw new Exception\ResourceConflictException('ERR_FILE_UPLOAD_BY_CONFLICT_NAME');
         }
     } else {
         throw new Exception\RuntimeException('ERR_FILE_SAVE_TO_DB_FAILED');
     }
     return $this;
 }
Ejemplo n.º 2
0
 /**
  * Validates if there are any errors
  *
  * @param UploadedFile $uploadedFile
  *
  * @return void
  * @throws Exception
  */
 private function validateError(UploadedFile $uploadedFile)
 {
     switch ($uploadedFile->getError()) {
         case UPLOAD_ERR_OK:
             //Do Nothing
             break;
         case UPLOAD_ERR_INI_SIZE:
             throw new Exception('File too large, must not be greater than ' . ini_get('upload_max_filesize'), 401);
             break;
         case UPLOAD_ERR_NO_FILE:
             throw new Exception('No File Uploaded', 401);
             break;
         default:
             throw new Exception($uploadedFile->getError(), 401);
             break;
     }
 }