diff --git a/app/Export/Collector/AttachmentCollector.php b/app/Export/Collector/AttachmentCollector.php index 0caa41be07..976fa4b5cc 100644 --- a/app/Export/Collector/AttachmentCollector.php +++ b/app/Export/Collector/AttachmentCollector.php @@ -10,10 +10,13 @@ declare(strict_types = 1); namespace FireflyIII\Export\Collector; +use Amount; use Auth; use Crypt; use FireflyIII\Models\Attachment; use FireflyIII\Models\ExportJob; +use FireflyIII\Models\TransactionJournal; +use Illuminate\Contracts\Encryption\DecryptException; use Log; /** @@ -23,6 +26,9 @@ use Log; */ class AttachmentCollector extends BasicCollector implements CollectorInterface { + /** @var string */ + private $explanationString = ''; + /** * AttachmentCollector constructor. * @@ -48,12 +54,39 @@ class AttachmentCollector extends BasicCollector implements CollectorInterface $originalFile = storage_path('upload') . DIRECTORY_SEPARATOR . 'at-' . $attachment->id . '.data'; if (file_exists($originalFile)) { Log::debug('Stored 1 attachment'); - $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); + 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); + + // explain: + $this->explain($attachment); + } catch (DecryptException $e) { + Log::error('Catchable error: could not decrypt attachment #' . $attachment->id); + } + } } + + // 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); + $this->getFiles()->push($explanationFile); + } + + /** + * @param Attachment $attachment + */ + private function explain(Attachment $attachment) + { + /** @var TransactionJournal $journal */ + $journal = $attachment->attachable; + $string = 'Attachment #' . $attachment->id . ' is part of ' . strtolower($journal->transactionType->type) . ' #' . $journal->id + . ', with description "' . $journal->description . '". This transaction was for ' . Amount::formatJournal($journal, false) . + ' and occured on ' . $journal->date->formatLocalized(config('config.month_and_day')) . "\n\n"; + $this->explanationString .= $string; + } } \ No newline at end of file diff --git a/app/Export/Collector/UploadCollector.php b/app/Export/Collector/UploadCollector.php index 803a7473c1..e88a2b8266 100644 --- a/app/Export/Collector/UploadCollector.php +++ b/app/Export/Collector/UploadCollector.php @@ -13,7 +13,8 @@ namespace FireflyIII\Export\Collector; use Auth; use Crypt; use FireflyIII\Models\ExportJob; - +use Illuminate\Contracts\Encryption\DecryptException; +use Log; /** * Class UploadCollector * @@ -45,19 +46,23 @@ class UploadCollector extends BasicCollector implements CollectorInterface $len = strlen($expected); foreach ($files as $entry) { if (substr($entry, 0, $len) === $expected) { - // this is an original upload. - $parts = explode('-', str_replace(['.csv.encrypted', $expected], '', $entry)); - $originalUpload = intval($parts[1]); - $date = date('Y-m-d \a\t H-i-s', $originalUpload); - $newFileName = 'Old CSV import dated ' . $date . '.csv'; - $content = Crypt::decrypt(file_get_contents($path . DIRECTORY_SEPARATOR . $entry)); - $fullPath = storage_path('export') . DIRECTORY_SEPARATOR . $this->job->key . '-' . $newFileName; + try { + // this is an original upload. + $parts = explode('-', str_replace(['.csv.encrypted', $expected], '', $entry)); + $originalUpload = intval($parts[1]); + $date = date('Y-m-d \a\t H-i-s', $originalUpload); + $newFileName = 'Old CSV import dated ' . $date . '.csv'; + $content = Crypt::decrypt(file_get_contents($path . DIRECTORY_SEPARATOR . $entry)); + $fullPath = storage_path('export') . DIRECTORY_SEPARATOR . $this->job->key . '-' . $newFileName; - // write to file: - file_put_contents($fullPath, $content); + // write to file: + file_put_contents($fullPath, $content); - // add entry to set: - $this->getFiles()->push($fullPath); + // add entry to set: + $this->getFiles()->push($fullPath); + } catch (DecryptException $e) { + Log::error('Could not decrypt old CSV import file ' . $entry . '. Skipped.'); + } } } } diff --git a/app/Http/Controllers/ReportController.php b/app/Http/Controllers/ReportController.php index 11d837dad1..6fa0f87b63 100644 --- a/app/Http/Controllers/ReportController.php +++ b/app/Http/Controllers/ReportController.php @@ -54,7 +54,7 @@ class ReportController extends Controller public function index(ARI $repository) { /** @var Carbon $start */ - $start = session('first'); + $start = clone session('first'); $months = $this->helper->listOfMonths($start); $customFiscalYear = Preferences::get('customFiscalYear', 0)->data; diff --git a/app/Http/Requests/ExportFormRequest.php b/app/Http/Requests/ExportFormRequest.php index 44d7d3f95a..d88b10702f 100644 --- a/app/Http/Requests/ExportFormRequest.php +++ b/app/Http/Requests/ExportFormRequest.php @@ -35,7 +35,9 @@ class ExportFormRequest extends Request */ public function rules() { - $first = session('first')->subDay()->format('Y-m-d'); + $sessionFirst = clone session('first'); + + $first = $sessionFirst->subDay()->format('Y-m-d'); $today = Carbon::create()->addDay()->format('Y-m-d'); $formats = join(',', array_keys(config('firefly.export_formats'))); diff --git a/public/js/export/index.js b/public/js/export/index.js index a44fe721c7..0e29d12da5 100644 --- a/public/js/export/index.js +++ b/public/js/export/index.js @@ -27,6 +27,7 @@ function startExport() { console.log('Start export...'); hideForm(); showLoading(); + hideError(); // do export callExport(); @@ -34,6 +35,10 @@ function startExport() { return false; } +function hideError() { + "use strict"; + $('#export-error').hide(); +} function hideForm() { "use strict"; diff --git a/resources/lang/en_US/firefly.php b/resources/lang/en_US/firefly.php index 926f2fd235..790c2b5bbe 100644 --- a/resources/lang/en_US/firefly.php +++ b/resources/lang/en_US/firefly.php @@ -73,6 +73,7 @@ return [ 'export_status_creating_zip_file' => 'Creating a zip file...', 'export_status_created_zip_file' => 'Created a zip file!', 'export_status_finished' => 'Export has succesfully finished! Yay!', + 'export_data_please_wait' => 'Please wait...', // rules 'rules' => 'Rules',