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

124 lines
3.5 KiB
PHP
Raw Normal View History

2016-02-04 10:16:16 -06:00
<?php
/**
* UploadCollector.php
2018-05-11 03:08:34 -05:00
* Copyright (c) 2018 thegrumpydictator@gmail.com
2016-02-04 10:16:16 -06:00
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
2017-10-21 01:40:00 -05:00
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 07:41:58 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
2016-02-04 10:16:16 -06:00
*/
2018-05-11 03:08:34 -05:00
declare(strict_types=1);
2016-02-04 10:16:16 -06:00
namespace FireflyIII\Export\Collector;
use Crypt;
2018-03-25 02:01:43 -05:00
use Exception;
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
/**
2017-11-15 05:25:49 -06:00
* Class UploadCollector.
*
* @codeCoverageIgnore
* @deprecated
2016-02-04 10:16:16 -06:00
*/
class UploadCollector extends BasicCollector implements CollectorInterface
{
2016-02-23 00:05:08 -06:00
/** @var \Illuminate\Contracts\Filesystem\Filesystem */
private $exportDisk;
/** @var \Illuminate\Contracts\Filesystem\Filesystem */
private $uploadDisk;
2016-02-04 10:16:16 -06:00
/**
* AttachmentCollector constructor.
*/
2017-02-05 08:58:55 -06:00
public function __construct()
2016-02-04 10:16:16 -06:00
{
2017-02-05 08:58:55 -06:00
parent::__construct();
2016-02-23 00:05:08 -06:00
$this->uploadDisk = Storage::disk('upload');
$this->exportDisk = Storage::disk('export');
2016-02-04 10:16:16 -06:00
}
2016-02-05 02:25:15 -06:00
/**
2016-10-23 02:44:14 -05:00
* Is called from the outside to actually start the export.
*
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
{
2017-02-05 08:58:55 -06:00
Log::debug('Going to collect attachments', ['key' => $this->job->key]);
2016-10-23 02:44:14 -05:00
$this->collectModernUploads();
2016-02-23 00:05:08 -06:00
2016-04-06 09:37:28 -05:00
return true;
2016-02-23 00:05:08 -06:00
}
/**
2016-10-23 02:44:14 -05:00
* This method collects all the uploads that are uploaded using the new importer. So after the summer of 2016.
*
2016-10-23 02:44:14 -05:00
* @return bool
*/
2016-10-23 02:44:14 -05:00
private function collectModernUploads(): bool
{
2017-08-18 07:45:42 -05:00
$set = $this->job->user->importJobs()->whereIn('status', ['import_complete', 'finished'])->get(['import_jobs.*']);
Log::debug(sprintf('Found %d import jobs', $set->count()));
2016-10-23 02:44:14 -05:00
$keys = [];
if ($set->count() > 0) {
2016-10-23 02:44:14 -05:00
$keys = $set->pluck('key')->toArray();
}
2016-10-23 02:44:14 -05:00
foreach ($keys as $key) {
$this->processModernUpload($key);
}
2016-10-23 02:44:14 -05:00
return true;
}
/** @noinspection MultipleReturnStatementsInspection */
2016-02-23 00:05:08 -06:00
/**
* Process new file uploads.
*
2016-10-23 02:44:14 -05:00
* @param string $key
*
* @return bool
2016-02-23 00:05:08 -06:00
*/
2016-10-23 02:44:14 -05:00
private function processModernUpload(string $key): bool
2016-02-23 00:05:08 -06:00
{
2016-10-23 02:44:14 -05:00
// find job associated with import file:
$job = $this->job->user->importJobs()->where('key', $key)->first();
2017-11-15 05:25:49 -06:00
if (null === $job) {
2016-10-23 02:44:14 -05:00
return false;
}
2016-10-23 02:44:14 -05:00
// find the file for this import:
2016-02-23 00:05:08 -06:00
$content = '';
try {
2016-10-23 02:44:14 -05:00
$content = Crypt::decrypt($this->uploadDisk->get(sprintf('%s.upload', $key)));
2018-03-25 02:01:43 -05:00
} catch (Exception | DecryptException $e) {
2016-10-23 02:44:14 -05:00
Log::error(sprintf('Could not decrypt old import file "%s". Skipped because: %s', $key, $e->getMessage()));
}
2016-02-04 10:16:16 -06:00
2018-04-27 23:23:13 -05:00
if (\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);
2016-10-23 02:44:14 -05:00
$this->getEntries()->push($file);
2016-02-04 10:16:16 -06:00
}
2016-10-23 02:44:14 -05:00
return true;
}
2016-02-10 09:01:18 -06:00
}