From deeeb064886a4729df692b34b1227a1b897beed2 Mon Sep 17 00:00:00 2001 From: James Cole Date: Tue, 23 Feb 2016 06:39:01 +0100 Subject: [PATCH] Added debug info to the export routine. --- app/Export/Collector/AttachmentCollector.php | 15 +++++++++++---- app/Export/Collector/UploadCollector.php | 11 +++++++++-- app/Export/ConfigurationFile.php | 3 +++ app/Export/Processor.php | 17 +++++++++++++++++ app/Http/Controllers/ExportController.php | 5 +++-- app/Models/ExportJob.php | 2 ++ .../Attachment/AttachmentRepository.php | 11 +++++++++++ .../AttachmentRepositoryInterface.php | 6 ++++++ 8 files changed, 62 insertions(+), 8 deletions(-) diff --git a/app/Export/Collector/AttachmentCollector.php b/app/Export/Collector/AttachmentCollector.php index 706fc12ab8..818c2f011f 100644 --- a/app/Export/Collector/AttachmentCollector.php +++ b/app/Export/Collector/AttachmentCollector.php @@ -11,11 +11,11 @@ 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 FireflyIII\Repositories\Attachment\AttachmentRepositoryInterface; use Illuminate\Contracts\Encryption\DecryptException; use Log; @@ -29,6 +29,9 @@ class AttachmentCollector extends BasicCollector implements CollectorInterface /** @var string */ private $explanationString = ''; + /** @var AttachmentRepositoryInterface */ + private $repository; + /** * AttachmentCollector constructor. * @@ -36,6 +39,8 @@ class AttachmentCollector extends BasicCollector implements CollectorInterface */ public function __construct(ExportJob $job) { + $this->repository = app('FireflyIII\Repositories\Attachment\AttachmentRepositoryInterface'); + parent::__construct($job); } @@ -45,26 +50,27 @@ class AttachmentCollector extends BasicCollector implements CollectorInterface public function run() { // grab all the users attachments: - $attachments = Auth::user()->attachments()->get(); + $attachments = $this->repository->get(); Log::debug('Found ' . $attachments->count() . ' attachments.'); /** @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)) { - Log::debug('Stored 1 attachment'); 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); + Log::error('Catchable error: could not decrypt attachment #' . $attachment->id . ' because: ' . $e->getMessage()); } } @@ -73,6 +79,7 @@ class AttachmentCollector extends BasicCollector implements CollectorInterface // 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); } diff --git a/app/Export/Collector/UploadCollector.php b/app/Export/Collector/UploadCollector.php index 817385fc93..b791064a48 100644 --- a/app/Export/Collector/UploadCollector.php +++ b/app/Export/Collector/UploadCollector.php @@ -15,6 +15,7 @@ use Crypt; use FireflyIII\Models\ExportJob; use Illuminate\Contracts\Encryption\DecryptException; use Log; + /** * Class UploadCollector * @@ -41,11 +42,14 @@ class UploadCollector extends BasicCollector implements CollectorInterface // grab upload directory. $path = storage_path('upload'); $files = scandir($path); + Log::debug('Found ' . count($files) . ' in the upload directory.'); // only allow old uploads for this user: $expected = 'csv-upload-' . Auth::user()->id . '-'; - $len = strlen($expected); + Log::debug('Searching for files that start with: "' . $expected . '".'); + $len = strlen($expected); foreach ($files as $entry) { if (substr($entry, 0, $len) === $expected) { + Log::debug($entry . ' is part of this users original uploads.'); try { // this is an original upload. $parts = explode('-', str_replace(['.csv.encrypted', $expected], '', $entry)); @@ -55,14 +59,17 @@ class UploadCollector extends BasicCollector implements CollectorInterface $content = Crypt::decrypt(file_get_contents($path . DIRECTORY_SEPARATOR . $entry)); $fullPath = storage_path('export') . DIRECTORY_SEPARATOR . $this->job->key . '-' . $newFileName; + Log::debug('Will put "' . $fullPath . '" in the zip file.'); // write to file: file_put_contents($fullPath, $content); // add entry to set: $this->getFiles()->push($fullPath); } catch (DecryptException $e) { - Log::error('Could not decrypt old CSV import file ' . $entry . '. Skipped.'); + Log::error('Could not decrypt old CSV import file ' . $entry . '. Skipped because ' . $e->getMessage()); } + } else { + Log::debug($entry . ' is not part of this users original uploads.'); } } } diff --git a/app/Export/ConfigurationFile.php b/app/Export/ConfigurationFile.php index a7c989b8d9..c588ca2713 100644 --- a/app/Export/ConfigurationFile.php +++ b/app/Export/ConfigurationFile.php @@ -11,6 +11,7 @@ declare(strict_types = 1); namespace FireflyIII\Export; use FireflyIII\Models\ExportJob; +use Log; /** * Class ConfigurationFile @@ -53,6 +54,8 @@ class ConfigurationFile } $file = storage_path('export') . DIRECTORY_SEPARATOR . $this->job->key . '-configuration.json'; + Log::debug('Created JSON config file.'); + Log::debug('Will put "' . $file . '" in the ZIP file.'); file_put_contents($file, json_encode($configuration, JSON_PRETTY_PRINT)); return $file; diff --git a/app/Export/Processor.php b/app/Export/Processor.php index b1092a71c9..547efa123a 100644 --- a/app/Export/Processor.php +++ b/app/Export/Processor.php @@ -16,6 +16,7 @@ use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\ExportJob; use FireflyIII\Models\TransactionJournal; use Illuminate\Support\Collection; +use Log; use ZipArchive; /** @@ -88,6 +89,13 @@ class Processor $args = [$this->accounts, Auth::user(), $this->settings['startDate'], $this->settings['endDate']]; $journalCollector = app('FireflyIII\Export\JournalCollector', $args); $this->journals = $journalCollector->collect(); + Log::debug( + 'Collected ' . + $this->journals->count() . ' journals (between ' . + $this->settings['startDate']->format('Y-m-d') . ' and ' . + $this->settings['endDate']->format('Y-m-d') + . ').' + ); } public function collectOldUploads() @@ -103,10 +111,13 @@ class Processor */ public function convertJournals() { + $count = 0; /** @var TransactionJournal $journal */ foreach ($this->journals as $journal) { $this->exportEntries->push(Entry::fromJournal($journal)); + $count++; } + Log::debug('Converted ' . $count . ' journals to "Entry" objects.'); } public function createConfigFile() @@ -119,6 +130,7 @@ class Processor { $zip = new ZipArchive; $filename = storage_path('export') . DIRECTORY_SEPARATOR . $this->job->key . '.zip'; + Log::debug('Will create zip file at ' . $filename); if ($zip->open($filename, ZipArchive::CREATE) !== true) { throw new FireflyException('Cannot store zip file.'); @@ -127,6 +139,7 @@ class Processor $search = storage_path('export') . DIRECTORY_SEPARATOR . $this->job->key . '-'; /** @var string $file */ foreach ($this->getFiles() as $file) { + Log::debug('Will add "' . $file . '" to zip file.'); $zipName = str_replace($search, '', $file); $zip->addFile($file, $zipName); } @@ -134,8 +147,10 @@ class Processor // delete the files: foreach ($this->getFiles() as $file) { + Log::debug('Will now delete file "' . $file . '".'); unlink($file); } + Log::debug('Done!'); } /** @@ -145,9 +160,11 @@ class Processor { $exporterClass = Config::get('firefly.export_formats.' . $this->exportFormat); $exporter = app($exporterClass, [$this->job]); + Log::debug('Going to export ' . $this->exportEntries->count() . ' export entries into ' . $this->exportFormat . ' format.'); $exporter->setEntries($this->exportEntries); $exporter->run(); $this->files->push($exporter->getFileName()); + Log::debug('Added "' . $exporter->getFileName() . '" to the list of files to include in the zip.'); } /** diff --git a/app/Http/Controllers/ExportController.php b/app/Http/Controllers/ExportController.php index 27e65214af..f7fab1f043 100644 --- a/app/Http/Controllers/ExportController.php +++ b/app/Http/Controllers/ExportController.php @@ -19,6 +19,7 @@ use FireflyIII\Http\Requests\ExportFormRequest; use FireflyIII\Models\ExportJob; use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI; use FireflyIII\Repositories\ExportJob\ExportJobRepositoryInterface as EJRI; +use Log; use Preferences; use Response; use View; @@ -51,7 +52,7 @@ class ExportController extends Controller $quoted = sprintf('"%s"', addcslashes($name, '"\\')); $job->change('export_downloaded'); - + Log::debug('Will send user file "' . $file . '".'); return response(file_get_contents($file), 200) ->header('Content-Description', 'File Transfer') @@ -96,7 +97,7 @@ class ExportController extends Controller $checked = array_keys($accountList); $formats = array_keys(Config::get('firefly.export_formats')); $defaultFormat = Preferences::get('export_format', Config::get('firefly.default_export_format'))->data; - $first = session('first')->format('Y-m-d'); + $first = Carbon::create()->subWeek()->format('Y-m-d'); $today = Carbon::create()->format('Y-m-d'); return view('export.index', compact('job', 'checked', 'accountList', 'formats', 'defaultFormat', 'first', 'today')); diff --git a/app/Models/ExportJob.php b/app/Models/ExportJob.php index a03e1bca94..17583ef18c 100644 --- a/app/Models/ExportJob.php +++ b/app/Models/ExportJob.php @@ -12,6 +12,7 @@ namespace FireflyIII\Models; use Auth; use Illuminate\Database\Eloquent\Model; +use Log; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; /** @@ -50,6 +51,7 @@ class ExportJob extends Model */ public function change($status) { + Log::debug('Job ' . $this->key . ' to status "' . $status . '".'); $this->status = $status; $this->save(); } diff --git a/app/Repositories/Attachment/AttachmentRepository.php b/app/Repositories/Attachment/AttachmentRepository.php index d8520d0d54..5165d497f8 100644 --- a/app/Repositories/Attachment/AttachmentRepository.php +++ b/app/Repositories/Attachment/AttachmentRepository.php @@ -3,7 +3,9 @@ declare(strict_types = 1); namespace FireflyIII\Repositories\Attachment; +use Auth; use FireflyIII\Models\Attachment; +use Illuminate\Support\Collection; /** * Class AttachmentRepository @@ -26,9 +28,18 @@ class AttachmentRepository implements AttachmentRepositoryInterface $file = $helper->getAttachmentLocation($attachment); unlink($file); $attachment->delete(); + return true; } + /** + * @return Collection + */ + public function get(): Collection + { + return Auth::user()->attachments()->get(); + } + /** * @param Attachment $attachment * @param array $data diff --git a/app/Repositories/Attachment/AttachmentRepositoryInterface.php b/app/Repositories/Attachment/AttachmentRepositoryInterface.php index 0cd01dff73..2c25be8baa 100644 --- a/app/Repositories/Attachment/AttachmentRepositoryInterface.php +++ b/app/Repositories/Attachment/AttachmentRepositoryInterface.php @@ -4,6 +4,7 @@ declare(strict_types = 1); namespace FireflyIII\Repositories\Attachment; use FireflyIII\Models\Attachment; +use Illuminate\Support\Collection; /** * Interface AttachmentRepositoryInterface @@ -20,6 +21,11 @@ interface AttachmentRepositoryInterface */ public function destroy(Attachment $attachment): bool; + /** + * @return Collection + */ + public function get(): Collection; + /** * @param Attachment $attachment * @param array $attachmentData