Ejemplo n.º 1
0
 public function transform($uploadModel)
 {
     if ($uploadModel->extension === 'pdf') {
         $type = 'application';
     } else {
         $type = 'image';
     }
     return ['hash' => $uploadModel->hash, 'extension' => $uploadModel->extension, 'path' => $uploadModel->path, 'file' => \Cloudder::show($uploadModel->hash, ['width' => 200, 'height' => 250, 'crop' => 'thumb']), 'link' => \Cloudder::show($uploadModel->hash, ['width' => 640, 'height' => 960, 'format' => $uploadModel->extension]), 'type' => $uploadModel->upload_type, 'user_id' => $uploadModel->user_id];
 }
Ejemplo n.º 2
0
 /**
  * @param UserModel $user
  * @param UploadedFile $file
  * @param ImageTypeModel $image_type
  *
  * @throws InvalidCreationException
  * @return ImageModel
  */
 public function createUpload($owner, $file, $uploadTarget, $uploadInfo)
 {
     // Check that image is valid
     $ext = $file->guessClientExtension();
     if ($ext == "jpeg") {
         $ext = "jpg";
     }
     $size = $file->getClientSize();
     if (!in_array($ext, $this->valid_exts)) {
         throw new InvalidCreationException('File format unacceptable: ' . $ext);
     }
     if (!$size || $size > $this->max_size) {
         throw new InvalidCreationException('File is too large: ' . strval($size));
     }
     if ($uploadInfo['unique'] == true) {
         try {
             $target = \DB::table('upload_map')->where('owner_hash', $uploadTarget->hash)->where('upload_type', $uploadInfo['type'])->first();
             if ($target) {
                 $previousUpload = $this->uploadRepository->getByUploadWithTypeAndTarget($uploadTarget, $uploadInfo['type'])->first();
                 $this->deleteUpload($previousUpload);
                 \DB::table('upload_map')->where('upload_hash', $previousUpload->hash)->delete();
             }
         } catch (ModelNotFoundException $e) {
             //To be expected if there is no previous
         }
     }
     $upload_dir = storage_path() . '/app/' . $owner->hash;
     // Create the path for the upload file
     if (!\File::exists($upload_dir)) {
         \File::makeDirectory($upload_dir, 0775);
     }
     // Create data for object
     $data = [];
     $data['user_id'] = $owner->id;
     $data['extension'] = $ext;
     $data['upload_type'] = $uploadInfo['type'];
     $data['path'] = $upload_dir;
     // Create the object
     // Create the full image
     if ($ext != 'pdf') {
         \Cloudder::upload($file);
         $publicID = \Cloudder::getPublicId();
     } else {
         \Cloudder::upload($file);
         $publicID = \Cloudder::getPublicId();
     }
     $data['hash'] = $publicID;
     $upload = $this->uploadRepository->create($data);
     \DB::table('upload_map')->insert(['upload_hash' => $publicID, 'owner_hash' => $uploadTarget->hash, 'upload_type' => $uploadInfo['type'], 'created_at' => date('Y-m-d G:i:s'), 'updated_at' => date('Y-m-d G:i:s')]);
     \Log::info('New upload created', $upload->toArray());
     return $upload;
 }