Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Simplified the attachment collector.
James Cole0c9c0f2032bf
diff --git a/app/Export/Collector/AttachmentCollector.php b/app/Export/Collector/AttachmentCollector.phpindex 818c2f011f..2f67d299c7 100644--- a/app/Export/Collector/AttachmentCollector.php+++ b/app/Export/Collector/AttachmentCollector.php@@ -17,7 +17,9 @@ use FireflyIII\Models\ExportJob;use FireflyIII\Models\TransactionJournal;use FireflyIII\Repositories\Attachment\AttachmentRepositoryInterface;use Illuminate\Contracts\Encryption\DecryptException;+use Illuminate\Support\Collection;use Log;+use Storage;/*** Class AttachmentCollector@@ -28,9 +30,12 @@ class AttachmentCollector extends BasicCollector implements CollectorInterface{/** @var string */private $explanationString = '';-+ /** @var \Illuminate\Contracts\Filesystem\Filesystem */+ private $exportDisk;/** @var AttachmentRepositoryInterface */private $repository;+ /** @var \Illuminate\Contracts\Filesystem\Filesystem */+ private $uploadDisk;/*** AttachmentCollector constructor.@@ -40,6 +45,9 @@ class AttachmentCollector extends BasicCollector implements CollectorInterfacepublic function __construct(ExportJob $job){$this->repository = app('FireflyIII\Repositories\Attachment\AttachmentRepositoryInterface');+ // make storage:+ $this->uploadDisk = Storage::disk('upload');+ $this->exportDisk = Storage::disk('export');parent::__construct($job);}@@ -50,37 +58,18 @@ class AttachmentCollector extends BasicCollector implements CollectorInterfacepublic function run(){// grab all the users attachments:- $attachments = $this->repository->get();-- Log::debug('Found ' . $attachments->count() . ' attachments.');+ $attachments = $this->getAttachments();/** @var Attachment $attachment */foreach ($attachments as $attachment) {- $originalFile = storage_path('upload') . DIRECTORY_SEPARATOR . 'at-' . $attachment->id . '.data';- Log::debug('Original file is at "' . $originalFile . '".');- if (file_exists($originalFile)) {- try {- $decrypted = Crypt::decrypt(file_get_contents($originalFile));- $newFile = storage_path('export') . DIRECTORY_SEPARATOR . $this->job->key . '-Attachment nr. ' . $attachment->id . ' - '- . $attachment->filename;- file_put_contents($newFile, $decrypted);- $this->getFiles()->push($newFile);- Log::debug('Stored file content in new file "' . $newFile . '", which will be in the final zip file.');-- // explain:- $this->explain($attachment);- } catch (DecryptException $e) {- Log::error('Catchable error: could not decrypt attachment #' . $attachment->id . ' because: ' . $e->getMessage());- }-- }+ $this->exportAttachment($attachment);}// put the explanation string in a file and attach it as well.- $explanationFile = storage_path('export') . DIRECTORY_SEPARATOR . $this->job->key . '-Source of all your attachments explained.txt';- file_put_contents($explanationFile, $this->explanationString);- Log::debug('Also put explanation file "' . $explanationFile . '" in the zip.');- $this->getFiles()->push($explanationFile);+ $file = $this->job->key . '-Source of all your attachments explained.txt';+ $this->exportDisk->put($file, $this->explanationString);+ Log::debug('Also put explanation file "' . $file . '" in the zip.');+ $this->getFiles()->push($file);}/**@@ -103,4 +92,57 @@ class AttachmentCollector extends BasicCollector implements CollectorInterface$this->explanationString .= $string;}++ /**+ * @param Attachment $attachment+ *+ * @return bool+ */+ private function exportAttachment(Attachment $attachment): bool+ {+ $file = $attachment->fileName();+ Log::debug('Original file is at "' . $file . '".');+ 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);+ Log::debug('Stored file content in new file "' . $exportFile . '", which will be in the final zip file.');++ // explain:+ $this->explain($attachment);+ } catch (DecryptException $e) {+ Log::error('Catchable error: could not decrypt attachment #' . $attachment->id . ' because: ' . $e->getMessage());+ }++ }++ return true;+ }++ /**+ * Returns the new file name for the export file.+ *+ * @param $attachment+ *+ * @return string+ */+ private function exportFileName($attachment): string+ {++ return sprintf('%s-Attachment nr. %s - %s', $this->job->key, strval($attachment->id), $attachment->filename);+ }++ /**+ * @return Collection+ */+ private function getAttachments(): Collection+ {+ $attachments = $this->repository->get();++ Log::debug('Found ' . $attachments->count() . ' attachments.');++ return $attachments;+ }}diff --git a/app/Models/Attachment.php b/app/Models/Attachment.phpindex 4eeaad603c..8397fda56d 100644--- a/app/Models/Attachment.php+++ b/app/Models/Attachment.php@@ -36,6 +36,22 @@ class Attachment extends Modelprotected $fillable = ['attachable_id', 'attachable_type', 'user_id', 'md5', 'filename', 'mime', 'title', 'notes', 'description', 'size', 'uploaded'];+ /**+ * @param Attachment $value+ *+ * @return Attachment+ */+ public static function routeBinder(Attachment $value)+ {+ if (Auth::check()) {++ if ($value->user_id == Auth::user()->id) {+ return $value;+ }+ }+ throw new NotFoundHttpException;+ }+/*** Get all of the owning imageable models.*/@@ -45,15 +61,15 @@ class Attachment extends Model}/**- * @codeCoverageIgnore- * @return \Illuminate\Database\Eloquent\Relations\BelongsTo+ * Returns the expected filename for this attachment.+ *+ * @return string*/- public function user()+ public function fileName(): string{- return $this->belongsTo('FireflyIII\User');+ return sprintf('at-%s.data', strval($this->id));}-/*** @codeCoverageIgnore*@@ -61,7 +77,7 @@ class Attachment extends Model** @return null|string*/- public function getFilenameAttribute($value)+ public function getDescriptionAttribute($value){if (is_null($value)) {return null;@@ -71,11 +87,19 @@ class Attachment extends Model}/**- * @param string $value+ * @codeCoverageIgnore+ *+ * @param $value+ *+ * @return null|string*/- public function setFilenameAttribute($value)+ public function getFilenameAttribute($value){- $this->attributes['filename'] = Crypt::encrypt($value);+ if (is_null($value)) {+ return null;+ }++ return Crypt::decrypt($value);}/**@@ -94,14 +118,6 @@ class Attachment extends Modelreturn Crypt::decrypt($value);}- /**- * @param string $value- */- public function setMimeAttribute($value)- {- $this->attributes['mime'] = Crypt::encrypt($value);- }-/*** @codeCoverageIgnore*@@ -109,7 +125,7 @@ class Attachment extends Model** @return null|string*/- public function getTitleAttribute($value)+ public function getNotesAttribute($value){if (is_null($value)) {return null;@@ -118,14 +134,6 @@ class Attachment extends Modelreturn Crypt::decrypt($value);}- /**- * @param string $value- */- public function setTitleAttribute($value)- {- $this->attributes['title'] = Crypt::encrypt($value);- }-/*** @codeCoverageIgnore*@@ -133,7 +141,7 @@ class Attachment extends Model** @return null|string*/- public function getDescriptionAttribute($value)+ public function getTitleAttribute($value){if (is_null($value)) {return null;@@ -151,19 +159,19 @@ class Attachment extends Model}/**- * @codeCoverageIgnore- *- * @param $value- *- * @return null|string+ * @param string $value*/- public function getNotesAttribute($value)+ public function setFilenameAttribute($value){- if (is_null($value)) {- return null;- }+ $this->attributes['filename'] = Crypt::encrypt($value);+ }- return Crypt::decrypt($value);+ /**+ * @param string $value+ */+ public function setMimeAttribute($value)+ {+ $this->attributes['mime'] = Crypt::encrypt($value);}/**@@ -175,19 +183,20 @@ class Attachment extends Model}/**- * @param Attachment $value- *- * @return Attachment+ * @param string $value*/- public static function routeBinder(Attachment $value)+ public function setTitleAttribute($value){- if (Auth::check()) {+ $this->attributes['title'] = Crypt::encrypt($value);+ }- if ($value->user_id == Auth::user()->id) {- return $value;- }- }- throw new NotFoundHttpException;+ /**+ * @codeCoverageIgnore+ * @return \Illuminate\Database\Eloquent\Relations\BelongsTo+ */+ public function user()+ {+ return $this->belongsTo('FireflyIII\User');}}