/**
  * @param Attachment $attachment
  * @param array      $data
  *
  * @return Attachment
  */
 public function update(Attachment $attachment, array $data) : Attachment
 {
     $attachment->title = $data['title'];
     $attachment->description = $data['description'];
     $attachment->notes = $data['notes'];
     $attachment->save();
     return $attachment;
 }
 /**
  *
  * @param UploadedFile $file
  * @param Model        $model
  *
  * @return Attachment
  */
 protected function processFile(UploadedFile $file, Model $model) : Attachment
 {
     $validation = $this->validateUpload($file, $model);
     if ($validation === false) {
         return new Attachment();
     }
     $attachment = new Attachment();
     // create Attachment object.
     $attachment->user()->associate(auth()->user());
     $attachment->attachable()->associate($model);
     $attachment->md5 = md5_file($file->getRealPath());
     $attachment->filename = $file->getClientOriginalName();
     $attachment->mime = $file->getMimeType();
     $attachment->size = $file->getSize();
     $attachment->uploaded = 0;
     $attachment->save();
     $fileObject = $file->openFile('r');
     $fileObject->rewind();
     $content = $fileObject->fread($file->getSize());
     $encrypted = Crypt::encrypt($content);
     // store it:
     $this->uploadDisk->put($attachment->fileName(), $encrypted);
     $attachment->uploaded = 1;
     // update attachment
     $attachment->save();
     $name = e($file->getClientOriginalName());
     // add message:
     $msg = (string) trans('validation.file_attached', ['name' => $name]);
     $this->messages->add('attachments', $msg);
     // return it.
     return $attachment;
 }
Example #3
0
 /**
  * @param UploadedFile $file
  * @param Model        $model
  *
  * @return bool|Attachment
  */
 protected function processFile(UploadedFile $file, Model $model)
 {
     $validation = $this->validateUpload($file, $model);
     if ($validation === false) {
         return false;
     }
     $attachment = new Attachment();
     // create Attachment object.
     $attachment->user()->associate(Auth::user());
     $attachment->attachable()->associate($model);
     $attachment->md5 = md5_file($file->getRealPath());
     $attachment->filename = $file->getClientOriginalName();
     $attachment->mime = $file->getMimeType();
     $attachment->size = $file->getSize();
     $attachment->uploaded = 0;
     $attachment->save();
     $path = $file->getRealPath();
     // encrypt and move file to storage.
     $content = file_get_contents($path);
     $encrypted = Crypt::encrypt($content);
     // store it:
     $upload = $this->getAttachmentLocation($attachment);
     if (is_writable(dirname($upload))) {
         file_put_contents($upload, $encrypted);
     }
     $attachment->uploaded = 1;
     // update attachment
     $attachment->save();
     $name = e($file->getClientOriginalName());
     // add message:
     $msg = (string) trans('validation.file_attached', ['name' => $name]);
     $this->messages->add('attachments', $msg);
     // return it.
     return $attachment;
 }