public function upload($uploaded, &$doc_array = null)
 {
     $extension = strtolower($uploaded->getClientOriginalExtension());
     if (empty(Document::$types[$extension]) && !empty(Document::$extraExtensions[$extension])) {
         $documentType = Document::$extraExtensions[$extension];
     } else {
         $documentType = $extension;
     }
     if (empty(Document::$types[$documentType])) {
         return 'Unsupported file type';
     }
     $documentTypeData = Document::$types[$documentType];
     $filePath = $uploaded->path();
     $name = $uploaded->getClientOriginalName();
     $size = filesize($filePath);
     if ($size / 1000 > MAX_DOCUMENT_SIZE) {
         return 'File too large';
     }
     $hash = sha1_file($filePath);
     $filename = \Auth::user()->account->account_key . '/' . $hash . '.' . $documentType;
     $document = Document::createNew();
     $disk = $document->getDisk();
     if (!$disk->exists($filename)) {
         // Have we already stored the same file
         $stream = fopen($filePath, 'r');
         $disk->getDriver()->putStream($filename, $stream, ['mimetype' => $documentTypeData['mime']]);
         fclose($stream);
     }
     // This is an image; check if we need to create a preview
     if (in_array($documentType, array('jpeg', 'png', 'gif', 'bmp', 'tiff', 'psd'))) {
         $makePreview = false;
         $imageSize = getimagesize($filePath);
         $width = $imageSize[0];
         $height = $imageSize[1];
         $imgManagerConfig = array();
         if (in_array($documentType, array('gif', 'bmp', 'tiff', 'psd'))) {
             // Needs to be converted
             $makePreview = true;
         } else {
             if ($width > DOCUMENT_PREVIEW_SIZE || $height > DOCUMENT_PREVIEW_SIZE) {
                 $makePreview = true;
             }
         }
         if (in_array($documentType, array('bmp', 'tiff', 'psd'))) {
             if (!class_exists('Imagick')) {
                 // Cant't read this
                 $makePreview = false;
             } else {
                 $imgManagerConfig['driver'] = 'imagick';
             }
         }
         if ($makePreview) {
             $previewType = 'jpeg';
             if (in_array($documentType, array('png', 'gif', 'tiff', 'psd'))) {
                 // Has transparency
                 $previewType = 'png';
             }
             $document->preview = \Auth::user()->account->account_key . '/' . $hash . '.' . $documentType . '.x' . DOCUMENT_PREVIEW_SIZE . '.' . $previewType;
             if (!$disk->exists($document->preview)) {
                 // We haven't created a preview yet
                 $imgManager = new ImageManager($imgManagerConfig);
                 $img = $imgManager->make($filePath);
                 if ($width <= DOCUMENT_PREVIEW_SIZE && $height <= DOCUMENT_PREVIEW_SIZE) {
                     $previewWidth = $width;
                     $previewHeight = $height;
                 } else {
                     if ($width > $height) {
                         $previewWidth = DOCUMENT_PREVIEW_SIZE;
                         $previewHeight = $height * DOCUMENT_PREVIEW_SIZE / $width;
                     } else {
                         $previewHeight = DOCUMENT_PREVIEW_SIZE;
                         $previewWidth = $width * DOCUMENT_PREVIEW_SIZE / $height;
                     }
                 }
                 $img->resize($previewWidth, $previewHeight);
                 $previewContent = (string) $img->encode($previewType);
                 $disk->put($document->preview, $previewContent);
                 $base64 = base64_encode($previewContent);
             } else {
                 $base64 = base64_encode($disk->get($document->preview));
             }
         } else {
             $base64 = base64_encode(file_get_contents($filePath));
         }
     }
     $document->path = $filename;
     $document->type = $documentType;
     $document->size = $size;
     $document->hash = $hash;
     $document->name = substr($name, -255);
     if (!empty($imageSize)) {
         $document->width = $imageSize[0];
         $document->height = $imageSize[1];
     }
     $document->save();
     $doc_array = $document->toArray();
     if (!empty($base64)) {
         $mime = Document::$types[!empty($previewType) ? $previewType : $documentType]['mime'];
         $doc_array['base64'] = 'data:' . $mime . ';base64,' . $base64;
     }
     return $document;
 }
Beispiel #2
0
 /**
  * @return mixed
  */
 public function cloneDocument()
 {
     $document = Document::createNew($this);
     $document->path = $this->path;
     $document->preview = $this->preview;
     $document->name = $this->name;
     $document->type = $this->type;
     $document->disk = $this->disk;
     $document->hash = $this->hash;
     $document->size = $this->size;
     $document->width = $this->width;
     $document->height = $this->height;
     return $document;
 }