/** * @param Attachment $attachment * * @throws FireflyException * */ public function download(Attachment $attachment) { // create a disk. $disk = Storage::disk('upload'); $file = $attachment->fileName(); if ($disk->exists($file)) { $quoted = sprintf('"%s"', addcslashes(basename($attachment->filename), '"\\')); $content = Crypt::decrypt($disk->get($file)); Log::debug('Send file to user', ['file' => $quoted, 'size' => strlen($content)]); return response($content, 200)->header('Content-Description', 'File Transfer')->header('Content-Type', 'application/octet-stream')->header('Content-Disposition', 'attachment; filename=' . $quoted)->header('Content-Transfer-Encoding', 'binary')->header('Connection', 'Keep-Alive')->header('Expires', '0')->header('Cache-Control', 'must-revalidate, post-check=0, pre-check=0')->header('Pragma', 'public')->header('Content-Length', strlen($content)); } throw new FireflyException('Could not find the indicated attachment. The file is no longer there.'); }
/** * * @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; }
/** * @param Attachment $attachment * * @return bool */ private function exportAttachment(Attachment $attachment) : bool { $file = $attachment->fileName(); if ($this->uploadDisk->exists($file)) { try { $decrypted = Crypt::decrypt($this->uploadDisk->get($file)); $exportFile = $this->exportFileName($attachment); $this->exportDisk->put($exportFile, $decrypted); $this->getFiles()->push($exportFile); // explain: $this->explain($attachment); } catch (DecryptException $e) { Log::error('Catchable error: could not decrypt attachment #' . $attachment->id . ' because: ' . $e->getMessage()); } } return true; }