mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-02 02:18:20 +00:00
Include files from new import routine in export routine.
This commit is contained in:
@@ -92,6 +92,7 @@ class AttachmentCollector extends BasicCollector implements CollectorInterface
|
|||||||
'amount' => Amount::formatJournal($journal, false),
|
'amount' => Amount::formatJournal($journal, false),
|
||||||
];
|
];
|
||||||
$string = trans('firefly.attachment_explanation', $args) . "\n";
|
$string = trans('firefly.attachment_explanation', $args) . "\n";
|
||||||
|
Log::debug('Appended explanation string', ['string' => $string]);
|
||||||
$this->explanationString .= $string;
|
$this->explanationString .= $string;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -29,6 +29,7 @@ class UploadCollector extends BasicCollector implements CollectorInterface
|
|||||||
private $expected;
|
private $expected;
|
||||||
/** @var \Illuminate\Contracts\Filesystem\Filesystem */
|
/** @var \Illuminate\Contracts\Filesystem\Filesystem */
|
||||||
private $exportDisk;
|
private $exportDisk;
|
||||||
|
private $importKeys = [];
|
||||||
/** @var \Illuminate\Contracts\Filesystem\Filesystem */
|
/** @var \Illuminate\Contracts\Filesystem\Filesystem */
|
||||||
private $uploadDisk;
|
private $uploadDisk;
|
||||||
|
|
||||||
@@ -42,10 +43,17 @@ class UploadCollector extends BasicCollector implements CollectorInterface
|
|||||||
{
|
{
|
||||||
parent::__construct($job);
|
parent::__construct($job);
|
||||||
|
|
||||||
|
Log::debug('Going to collect attachments', ['key' => $job->key]);
|
||||||
|
|
||||||
// make storage:
|
// make storage:
|
||||||
$this->uploadDisk = Storage::disk('upload');
|
$this->uploadDisk = Storage::disk('upload');
|
||||||
$this->exportDisk = Storage::disk('export');
|
$this->exportDisk = Storage::disk('export');
|
||||||
$this->expected = 'csv-upload-' . Auth::user()->id . '-';
|
|
||||||
|
// file names associated with the old import routine.
|
||||||
|
$this->expected = 'csv-upload-' . Auth::user()->id . '-';
|
||||||
|
|
||||||
|
// for the new import routine:
|
||||||
|
$this->getImportKeys();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -57,12 +65,26 @@ class UploadCollector extends BasicCollector implements CollectorInterface
|
|||||||
$files = $this->uploadDisk->files();
|
$files = $this->uploadDisk->files();
|
||||||
|
|
||||||
foreach ($files as $entry) {
|
foreach ($files as $entry) {
|
||||||
$this->processOldUpload($entry);
|
$this->processUpload($entry);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private function getImportKeys()
|
||||||
|
{
|
||||||
|
$set = Auth::user()->importJobs()->where('status', 'import_complete')->get(['import_jobs.*']);
|
||||||
|
if ($set->count() > 0) {
|
||||||
|
$keys = $set->pluck('key')->toArray();
|
||||||
|
$this->importKeys = $keys;
|
||||||
|
|
||||||
|
}
|
||||||
|
Log::debug('Valid import keys are ', $this->importKeys);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $entry
|
* @param string $entry
|
||||||
*
|
*
|
||||||
@@ -83,9 +105,28 @@ class UploadCollector extends BasicCollector implements CollectorInterface
|
|||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
private function isValidFile(string $entry): bool
|
private function isImportFile(string $entry): bool
|
||||||
|
{
|
||||||
|
$name = str_replace('.upload', '', $entry);
|
||||||
|
if (in_array($name, $this->importKeys)) {
|
||||||
|
Log::debug(sprintf('Import file "%s" is in array', $name), $this->importKeys);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
Log::debug(sprintf('Import file "%s" is NOT in array', $name), $this->importKeys);
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $entry
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
private function isOldImport(string $entry): bool
|
||||||
{
|
{
|
||||||
$len = strlen($this->expected);
|
$len = strlen($this->expected);
|
||||||
|
// file is part of the old import routine:
|
||||||
if (substr($entry, 0, $len) === $this->expected) {
|
if (substr($entry, 0, $len) === $this->expected) {
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@@ -97,23 +138,63 @@ class UploadCollector extends BasicCollector implements CollectorInterface
|
|||||||
/**
|
/**
|
||||||
* @param $entry
|
* @param $entry
|
||||||
*/
|
*/
|
||||||
private function processOldUpload(string $entry)
|
private function processUpload(string $entry)
|
||||||
{
|
{
|
||||||
$content = '';
|
// file is old import:
|
||||||
|
if ($this->isOldImport($entry)) {
|
||||||
if ($this->isValidFile($entry)) {
|
$this->saveOldImportFile($entry);
|
||||||
try {
|
|
||||||
$content = Crypt::decrypt($this->uploadDisk->get($entry));
|
|
||||||
} catch (DecryptException $e) {
|
|
||||||
Log::error('Could not decrypt old CSV import file ' . $entry . '. Skipped because ' . $e->getMessage());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (strlen($content) > 0) {
|
|
||||||
// continue with file:
|
// file is current import.
|
||||||
$date = $this->getOriginalUploadDate($entry);
|
if ($this->isImportFile($entry)) {
|
||||||
$file = $this->job->key . '-Old CSV import dated ' . $date . '.csv';
|
$this->saveImportFile($entry);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $entry
|
||||||
|
*/
|
||||||
|
private function saveImportFile(string $entry)
|
||||||
|
{
|
||||||
|
// find job associated with import file:
|
||||||
|
$name = str_replace('.upload', '', $entry);
|
||||||
|
$job = Auth::user()->importJobs()->where('key', $name)->first();
|
||||||
|
$content = '';
|
||||||
|
try {
|
||||||
|
$content = Crypt::decrypt($this->uploadDisk->get($entry));
|
||||||
|
} catch (DecryptException $e) {
|
||||||
|
Log::error('Could not decrypt old import file ' . $entry . '. Skipped because ' . $e->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!is_null($job) && strlen($content) > 0) {
|
||||||
|
// add to export disk.
|
||||||
|
$date = $job->created_at->format('Y-m-d');
|
||||||
|
$file = sprintf('%s-Old %s import dated %s.%s', $this->job->key, strtoupper($job->file_type), $date, $job->file_type);
|
||||||
$this->exportDisk->put($file, $content);
|
$this->exportDisk->put($file, $content);
|
||||||
$this->getFiles()->push($file);
|
$this->getFiles()->push($file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $entry
|
||||||
|
*/
|
||||||
|
private function saveOldImportFile(string $entry)
|
||||||
|
{
|
||||||
|
$content = '';
|
||||||
|
try {
|
||||||
|
$content = Crypt::decrypt($this->uploadDisk->get($entry));
|
||||||
|
} catch (DecryptException $e) {
|
||||||
|
Log::error('Could not decrypt old CSV import file ' . $entry . '. Skipped because ' . $e->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (strlen($content) > 0) {
|
||||||
|
// add to export disk.
|
||||||
|
$date = $this->getOriginalUploadDate($entry);
|
||||||
|
$file = $this->job->key . '-Old import dated ' . $date . '.csv';
|
||||||
|
$this->exportDisk->put($file, $content);
|
||||||
|
$this->getFiles()->push($file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -305,6 +305,7 @@ class ImportController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function start(ImportJob $job)
|
public function start(ImportJob $job)
|
||||||
{
|
{
|
||||||
|
set_time_limit(0);
|
||||||
if ($job->status == "settings_complete") {
|
if ($job->status == "settings_complete") {
|
||||||
ImportProcedure::runImport($job);
|
ImportProcedure::runImport($job);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user