firefly-iii/app/Export/Collector/UploadCollector.php

200 lines
5.2 KiB
PHP
Raw Normal View History

2016-02-04 10:16:16 -06:00
<?php
/**
* UploadCollector.php
2016-04-01 09:44:46 -05:00
* Copyright (C) 2016 thegrumpydictator@gmail.com
2016-02-04 10:16:16 -06:00
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
declare(strict_types = 1);
2016-02-04 10:16:16 -06:00
namespace FireflyIII\Export\Collector;
use Crypt;
use FireflyIII\Models\ExportJob;
2016-02-07 02:11:46 -06:00
use Illuminate\Contracts\Encryption\DecryptException;
use Log;
2016-02-23 00:05:08 -06:00
use Storage;
2016-02-04 10:16:16 -06:00
/**
* Class UploadCollector
*
* @package FireflyIII\Export\Collector
*/
class UploadCollector extends BasicCollector implements CollectorInterface
{
2016-02-23 00:05:08 -06:00
/** @var string */
private $expected;
/** @var \Illuminate\Contracts\Filesystem\Filesystem */
private $exportDisk;
private $importKeys = [];
2016-02-23 00:05:08 -06:00
/** @var \Illuminate\Contracts\Filesystem\Filesystem */
private $uploadDisk;
2016-02-04 10:16:16 -06:00
/**
*
2016-02-04 10:16:16 -06:00
* AttachmentCollector constructor.
*
* @param ExportJob $job
*/
public function __construct(ExportJob $job)
{
parent::__construct($job);
2016-02-23 00:05:08 -06:00
Log::debug('Going to collect attachments', ['key' => $job->key]);
2016-02-23 00:05:08 -06:00
// make storage:
$this->uploadDisk = Storage::disk('upload');
$this->exportDisk = Storage::disk('export');
// file names associated with the old import routine.
2016-09-16 05:15:58 -05:00
$this->expected = 'csv-upload-' . auth()->user()->id . '-';
// for the new import routine:
$this->getImportKeys();
2016-02-04 10:16:16 -06:00
}
2016-02-05 02:25:15 -06:00
/**
2016-04-06 09:37:28 -05:00
* @return bool
2016-02-05 02:25:15 -06:00
*/
2016-04-06 09:37:28 -05:00
public function run(): bool
2016-02-04 10:16:16 -06:00
{
// grab upload directory.
2016-02-23 00:05:08 -06:00
$files = $this->uploadDisk->files();
2016-02-04 10:16:16 -06:00
foreach ($files as $entry) {
$this->processUpload($entry);
2016-02-23 00:05:08 -06:00
}
2016-04-25 11:43:09 -05:00
2016-04-06 09:37:28 -05:00
return true;
2016-02-23 00:05:08 -06:00
}
/**
*
*/
private function getImportKeys()
{
2016-09-16 05:15:58 -05:00
$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);
}
2016-02-23 00:05:08 -06:00
/**
* @param string $entry
*
* @return string
*/
private function getOriginalUploadDate(string $entry): string
{
// this is an original upload.
$parts = explode('-', str_replace(['.csv.encrypted', $this->expected], '', $entry));
$originalUpload = intval($parts[1]);
$date = date('Y-m-d \a\t H-i-s', $originalUpload);
return $date;
}
/**
* @param string $entry
*
* @return 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
2016-02-23 00:05:08 -06:00
{
$len = strlen($this->expected);
// file is part of the old import routine:
2016-02-23 00:05:08 -06:00
if (substr($entry, 0, $len) === $this->expected) {
return true;
}
2016-02-04 10:16:16 -06:00
2016-02-23 00:05:08 -06:00
return false;
}
/**
* @param $entry
*/
private function processUpload(string $entry)
2016-02-23 00:05:08 -06:00
{
// file is old import:
if ($this->isOldImport($entry)) {
$this->saveOldImportFile($entry);
}
// file is current import.
if ($this->isImportFile($entry)) {
$this->saveImportFile($entry);
}
}
/**
* @param string $entry
*/
private function saveImportFile(string $entry)
{
// find job associated with import file:
$name = str_replace('.upload', '', $entry);
2016-09-16 05:15:58 -05:00
$job = auth()->user()->importJobs()->where('key', $name)->first();
2016-02-23 00:05:08 -06:00
$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());
}
2016-02-04 10:16:16 -06:00
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->getFiles()->push($file);
2016-02-04 10:16:16 -06:00
}
}
/**
* @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());
}
2016-02-23 00:05:08 -06:00
if (strlen($content) > 0) {
// add to export disk.
2016-02-23 00:05:08 -06:00
$date = $this->getOriginalUploadDate($entry);
$file = $this->job->key . '-Old import dated ' . $date . '.csv';
2016-02-23 00:05:08 -06:00
$this->exportDisk->put($file, $content);
$this->getFiles()->push($file);
}
2016-02-04 10:16:16 -06:00
}
2016-02-10 09:01:18 -06:00
}