Added debug info to the export routine.

This commit is contained in:
James Cole
2016-02-23 06:39:01 +01:00
parent 28fd719ce3
commit deeeb06488
8 changed files with 62 additions and 8 deletions

View File

@@ -11,11 +11,11 @@ declare(strict_types = 1);
namespace FireflyIII\Export\Collector; namespace FireflyIII\Export\Collector;
use Amount; use Amount;
use Auth;
use Crypt; use Crypt;
use FireflyIII\Models\Attachment; use FireflyIII\Models\Attachment;
use FireflyIII\Models\ExportJob; use FireflyIII\Models\ExportJob;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Attachment\AttachmentRepositoryInterface;
use Illuminate\Contracts\Encryption\DecryptException; use Illuminate\Contracts\Encryption\DecryptException;
use Log; use Log;
@@ -29,6 +29,9 @@ class AttachmentCollector extends BasicCollector implements CollectorInterface
/** @var string */ /** @var string */
private $explanationString = ''; private $explanationString = '';
/** @var AttachmentRepositoryInterface */
private $repository;
/** /**
* AttachmentCollector constructor. * AttachmentCollector constructor.
* *
@@ -36,6 +39,8 @@ class AttachmentCollector extends BasicCollector implements CollectorInterface
*/ */
public function __construct(ExportJob $job) public function __construct(ExportJob $job)
{ {
$this->repository = app('FireflyIII\Repositories\Attachment\AttachmentRepositoryInterface');
parent::__construct($job); parent::__construct($job);
} }
@@ -45,26 +50,27 @@ class AttachmentCollector extends BasicCollector implements CollectorInterface
public function run() public function run()
{ {
// grab all the users attachments: // grab all the users attachments:
$attachments = Auth::user()->attachments()->get(); $attachments = $this->repository->get();
Log::debug('Found ' . $attachments->count() . ' attachments.'); Log::debug('Found ' . $attachments->count() . ' attachments.');
/** @var Attachment $attachment */ /** @var Attachment $attachment */
foreach ($attachments as $attachment) { foreach ($attachments as $attachment) {
$originalFile = storage_path('upload') . DIRECTORY_SEPARATOR . 'at-' . $attachment->id . '.data'; $originalFile = storage_path('upload') . DIRECTORY_SEPARATOR . 'at-' . $attachment->id . '.data';
Log::debug('Original file is at "' . $originalFile . '".');
if (file_exists($originalFile)) { if (file_exists($originalFile)) {
Log::debug('Stored 1 attachment');
try { try {
$decrypted = Crypt::decrypt(file_get_contents($originalFile)); $decrypted = Crypt::decrypt(file_get_contents($originalFile));
$newFile = storage_path('export') . DIRECTORY_SEPARATOR . $this->job->key . '-Attachment nr. ' . $attachment->id . ' - ' $newFile = storage_path('export') . DIRECTORY_SEPARATOR . $this->job->key . '-Attachment nr. ' . $attachment->id . ' - '
. $attachment->filename; . $attachment->filename;
file_put_contents($newFile, $decrypted); file_put_contents($newFile, $decrypted);
$this->getFiles()->push($newFile); $this->getFiles()->push($newFile);
Log::debug('Stored file content in new file "' . $newFile . '", which will be in the final zip file.');
// explain: // explain:
$this->explain($attachment); $this->explain($attachment);
} catch (DecryptException $e) { } 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. // 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'; $explanationFile = storage_path('export') . DIRECTORY_SEPARATOR . $this->job->key . '-Source of all your attachments explained.txt';
file_put_contents($explanationFile, $this->explanationString); file_put_contents($explanationFile, $this->explanationString);
Log::debug('Also put explanation file "' . $explanationFile . '" in the zip.');
$this->getFiles()->push($explanationFile); $this->getFiles()->push($explanationFile);
} }

View File

@@ -15,6 +15,7 @@ use Crypt;
use FireflyIII\Models\ExportJob; use FireflyIII\Models\ExportJob;
use Illuminate\Contracts\Encryption\DecryptException; use Illuminate\Contracts\Encryption\DecryptException;
use Log; use Log;
/** /**
* Class UploadCollector * Class UploadCollector
* *
@@ -41,11 +42,14 @@ class UploadCollector extends BasicCollector implements CollectorInterface
// grab upload directory. // grab upload directory.
$path = storage_path('upload'); $path = storage_path('upload');
$files = scandir($path); $files = scandir($path);
Log::debug('Found ' . count($files) . ' in the upload directory.');
// only allow old uploads for this user: // only allow old uploads for this user:
$expected = 'csv-upload-' . Auth::user()->id . '-'; $expected = 'csv-upload-' . Auth::user()->id . '-';
Log::debug('Searching for files that start with: "' . $expected . '".');
$len = strlen($expected); $len = strlen($expected);
foreach ($files as $entry) { foreach ($files as $entry) {
if (substr($entry, 0, $len) === $expected) { if (substr($entry, 0, $len) === $expected) {
Log::debug($entry . ' is part of this users original uploads.');
try { try {
// this is an original upload. // this is an original upload.
$parts = explode('-', str_replace(['.csv.encrypted', $expected], '', $entry)); $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)); $content = Crypt::decrypt(file_get_contents($path . DIRECTORY_SEPARATOR . $entry));
$fullPath = storage_path('export') . DIRECTORY_SEPARATOR . $this->job->key . '-' . $newFileName; $fullPath = storage_path('export') . DIRECTORY_SEPARATOR . $this->job->key . '-' . $newFileName;
Log::debug('Will put "' . $fullPath . '" in the zip file.');
// write to file: // write to file:
file_put_contents($fullPath, $content); file_put_contents($fullPath, $content);
// add entry to set: // add entry to set:
$this->getFiles()->push($fullPath); $this->getFiles()->push($fullPath);
} catch (DecryptException $e) { } 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.');
} }
} }
} }

View File

@@ -11,6 +11,7 @@ declare(strict_types = 1);
namespace FireflyIII\Export; namespace FireflyIII\Export;
use FireflyIII\Models\ExportJob; use FireflyIII\Models\ExportJob;
use Log;
/** /**
* Class ConfigurationFile * Class ConfigurationFile
@@ -53,6 +54,8 @@ class ConfigurationFile
} }
$file = storage_path('export') . DIRECTORY_SEPARATOR . $this->job->key . '-configuration.json'; $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)); file_put_contents($file, json_encode($configuration, JSON_PRETTY_PRINT));
return $file; return $file;

View File

@@ -16,6 +16,7 @@ use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\ExportJob; use FireflyIII\Models\ExportJob;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Log;
use ZipArchive; use ZipArchive;
/** /**
@@ -88,6 +89,13 @@ class Processor
$args = [$this->accounts, Auth::user(), $this->settings['startDate'], $this->settings['endDate']]; $args = [$this->accounts, Auth::user(), $this->settings['startDate'], $this->settings['endDate']];
$journalCollector = app('FireflyIII\Export\JournalCollector', $args); $journalCollector = app('FireflyIII\Export\JournalCollector', $args);
$this->journals = $journalCollector->collect(); $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() public function collectOldUploads()
@@ -103,10 +111,13 @@ class Processor
*/ */
public function convertJournals() public function convertJournals()
{ {
$count = 0;
/** @var TransactionJournal $journal */ /** @var TransactionJournal $journal */
foreach ($this->journals as $journal) { foreach ($this->journals as $journal) {
$this->exportEntries->push(Entry::fromJournal($journal)); $this->exportEntries->push(Entry::fromJournal($journal));
$count++;
} }
Log::debug('Converted ' . $count . ' journals to "Entry" objects.');
} }
public function createConfigFile() public function createConfigFile()
@@ -119,6 +130,7 @@ class Processor
{ {
$zip = new ZipArchive; $zip = new ZipArchive;
$filename = storage_path('export') . DIRECTORY_SEPARATOR . $this->job->key . '.zip'; $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) { if ($zip->open($filename, ZipArchive::CREATE) !== true) {
throw new FireflyException('Cannot store zip file.'); throw new FireflyException('Cannot store zip file.');
@@ -127,6 +139,7 @@ class Processor
$search = storage_path('export') . DIRECTORY_SEPARATOR . $this->job->key . '-'; $search = storage_path('export') . DIRECTORY_SEPARATOR . $this->job->key . '-';
/** @var string $file */ /** @var string $file */
foreach ($this->getFiles() as $file) { foreach ($this->getFiles() as $file) {
Log::debug('Will add "' . $file . '" to zip file.');
$zipName = str_replace($search, '', $file); $zipName = str_replace($search, '', $file);
$zip->addFile($file, $zipName); $zip->addFile($file, $zipName);
} }
@@ -134,8 +147,10 @@ class Processor
// delete the files: // delete the files:
foreach ($this->getFiles() as $file) { foreach ($this->getFiles() as $file) {
Log::debug('Will now delete file "' . $file . '".');
unlink($file); unlink($file);
} }
Log::debug('Done!');
} }
/** /**
@@ -145,9 +160,11 @@ class Processor
{ {
$exporterClass = Config::get('firefly.export_formats.' . $this->exportFormat); $exporterClass = Config::get('firefly.export_formats.' . $this->exportFormat);
$exporter = app($exporterClass, [$this->job]); $exporter = app($exporterClass, [$this->job]);
Log::debug('Going to export ' . $this->exportEntries->count() . ' export entries into ' . $this->exportFormat . ' format.');
$exporter->setEntries($this->exportEntries); $exporter->setEntries($this->exportEntries);
$exporter->run(); $exporter->run();
$this->files->push($exporter->getFileName()); $this->files->push($exporter->getFileName());
Log::debug('Added "' . $exporter->getFileName() . '" to the list of files to include in the zip.');
} }
/** /**

View File

@@ -19,6 +19,7 @@ use FireflyIII\Http\Requests\ExportFormRequest;
use FireflyIII\Models\ExportJob; use FireflyIII\Models\ExportJob;
use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI; use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI;
use FireflyIII\Repositories\ExportJob\ExportJobRepositoryInterface as EJRI; use FireflyIII\Repositories\ExportJob\ExportJobRepositoryInterface as EJRI;
use Log;
use Preferences; use Preferences;
use Response; use Response;
use View; use View;
@@ -51,7 +52,7 @@ class ExportController extends Controller
$quoted = sprintf('"%s"', addcslashes($name, '"\\')); $quoted = sprintf('"%s"', addcslashes($name, '"\\'));
$job->change('export_downloaded'); $job->change('export_downloaded');
Log::debug('Will send user file "' . $file . '".');
return response(file_get_contents($file), 200) return response(file_get_contents($file), 200)
->header('Content-Description', 'File Transfer') ->header('Content-Description', 'File Transfer')
@@ -96,7 +97,7 @@ class ExportController extends Controller
$checked = array_keys($accountList); $checked = array_keys($accountList);
$formats = array_keys(Config::get('firefly.export_formats')); $formats = array_keys(Config::get('firefly.export_formats'));
$defaultFormat = Preferences::get('export_format', Config::get('firefly.default_export_format'))->data; $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'); $today = Carbon::create()->format('Y-m-d');
return view('export.index', compact('job', 'checked', 'accountList', 'formats', 'defaultFormat', 'first', 'today')); return view('export.index', compact('job', 'checked', 'accountList', 'formats', 'defaultFormat', 'first', 'today'));

View File

@@ -12,6 +12,7 @@ namespace FireflyIII\Models;
use Auth; use Auth;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Log;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/** /**
@@ -50,6 +51,7 @@ class ExportJob extends Model
*/ */
public function change($status) public function change($status)
{ {
Log::debug('Job ' . $this->key . ' to status "' . $status . '".');
$this->status = $status; $this->status = $status;
$this->save(); $this->save();
} }

View File

@@ -3,7 +3,9 @@ declare(strict_types = 1);
namespace FireflyIII\Repositories\Attachment; namespace FireflyIII\Repositories\Attachment;
use Auth;
use FireflyIII\Models\Attachment; use FireflyIII\Models\Attachment;
use Illuminate\Support\Collection;
/** /**
* Class AttachmentRepository * Class AttachmentRepository
@@ -26,9 +28,18 @@ class AttachmentRepository implements AttachmentRepositoryInterface
$file = $helper->getAttachmentLocation($attachment); $file = $helper->getAttachmentLocation($attachment);
unlink($file); unlink($file);
$attachment->delete(); $attachment->delete();
return true; return true;
} }
/**
* @return Collection
*/
public function get(): Collection
{
return Auth::user()->attachments()->get();
}
/** /**
* @param Attachment $attachment * @param Attachment $attachment
* @param array $data * @param array $data

View File

@@ -4,6 +4,7 @@ declare(strict_types = 1);
namespace FireflyIII\Repositories\Attachment; namespace FireflyIII\Repositories\Attachment;
use FireflyIII\Models\Attachment; use FireflyIII\Models\Attachment;
use Illuminate\Support\Collection;
/** /**
* Interface AttachmentRepositoryInterface * Interface AttachmentRepositoryInterface
@@ -20,6 +21,11 @@ interface AttachmentRepositoryInterface
*/ */
public function destroy(Attachment $attachment): bool; public function destroy(Attachment $attachment): bool;
/**
* @return Collection
*/
public function get(): Collection;
/** /**
* @param Attachment $attachment * @param Attachment $attachment
* @param array $attachmentData * @param array $attachmentData