/** * @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 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 User $user */ private function createAttachments(User $user) { $toAccount = TestData::findAccount($user, 'TestData Checking Account'); $fromAccount = TestData::findAccount($user, 'Job'); $journal = TransactionJournal::create(['user_id' => $user->id, 'transaction_type_id' => 2, 'transaction_currency_id' => 1, 'description' => 'Some journal for attachment', 'completed' => 1, 'date' => new Carbon()]); Transaction::create(['account_id' => $fromAccount->id, 'transaction_journal_id' => $journal->id, 'amount' => -100]); Transaction::create(['account_id' => $toAccount->id, 'transaction_journal_id' => $journal->id, 'amount' => 100]); // and now attachments $encrypted = Crypt::encrypt('I are secret'); Attachment::create(['attachable_id' => $journal->id, 'attachable_type' => 'FireflyIII\\Models\\TransactionJournal', 'user_id' => $user->id, 'md5' => md5('Hallo'), 'filename' => 'empty-file.txt', 'title' => 'Empty file', 'description' => 'This file is empty', 'notes' => 'What notes', 'mime' => 'text/plain', 'size' => strlen($encrypted), 'uploaded' => 1]); // and now attachment. Attachment::create(['attachable_id' => $journal->id, 'attachable_type' => 'FireflyIII\\Models\\TransactionJournal', 'user_id' => $user->id, 'md5' => md5('Ook hallo'), 'filename' => 'empty-file-2.txt', 'title' => 'Empty file 2', 'description' => 'This file is empty too', 'notes' => 'What notes do', 'mime' => 'text/plain', 'size' => strlen($encrypted), 'uploaded' => 1]); // echo crypted data to the file. file_put_contents(storage_path('upload/at-1.data'), $encrypted); file_put_contents(storage_path('upload/at-2.data'), $encrypted); }
/** * @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; }
/** * Execute the console command. */ public function handle() { $attachments = Attachment::get(); $disk = Storage::disk('upload'); /** @var Attachment $attachment */ foreach ($attachments as $attachment) { $fileName = $attachment->fileName(); // try to grab file content: try { $content = $disk->get($fileName); } catch (FileNotFoundException $e) { $this->error(sprintf('Could not find data for attachment #%d', $attachment->id)); continue; } // try to decrypt content. try { $decrypted = Crypt::decrypt($content); } catch (DecryptException $e) { $this->error(sprintf('Could not decrypt data of attachment #%d', $attachment->id)); continue; } // make temp file: $tmpfname = tempnam(sys_get_temp_dir(), 'FireflyIII'); // store content in temp file: file_put_contents($tmpfname, $decrypted); // get md5 and mime $md5 = md5_file($tmpfname); $mime = mime_content_type($tmpfname); // update attachment: $attachment->md5 = $md5; $attachment->mime = $mime; $attachment->save(); $this->line(sprintf('Fixed attachment #%d', $attachment->id)); // find file: } }
/** * * @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; }
} } throw new NotFoundHttpException(); }); Route::bind('tj', function ($value) { if (Auth::check()) { $object = TransactionJournal::where('id', $value)->where('user_id', Auth::user()->id)->first(); if ($object) { return $object; } } throw new NotFoundHttpException(); }); Route::bind('attachment', function ($value) { if (Auth::check()) { $object = Attachment::where('id', $value)->where('user_id', Auth::user()->id)->first(); if ($object) { return $object; } } throw new NotFoundHttpException(); }); Route::bind('currency', function ($value) { if (Auth::check()) { $object = TransactionCurrency::find($value); if ($object) { return $object; } } throw new NotFoundHttpException(); });
/** * @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; }