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

122 lines
3.0 KiB
PHP
Raw Normal View History

2016-02-04 17:16:16 +01:00
<?php
/**
* UploadCollector.php
2016-04-01 16:44:46 +02:00
* Copyright (C) 2016 thegrumpydictator@gmail.com
2016-02-04 17:16:16 +01: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 17:16:16 +01:00
namespace FireflyIII\Export\Collector;
use Auth;
use Crypt;
use FireflyIII\Models\ExportJob;
2016-02-07 09:11:46 +01:00
use Illuminate\Contracts\Encryption\DecryptException;
use Log;
2016-02-23 07:05:08 +01:00
use Storage;
2016-02-04 17:16:16 +01:00
/**
* Class UploadCollector
*
* @package FireflyIII\Export\Collector
*/
class UploadCollector extends BasicCollector implements CollectorInterface
{
2016-02-23 07:05:08 +01:00
/** @var string */
private $expected;
/** @var \Illuminate\Contracts\Filesystem\Filesystem */
private $exportDisk;
/** @var \Illuminate\Contracts\Filesystem\Filesystem */
private $uploadDisk;
2016-02-04 17:16:16 +01:00
/**
*
2016-02-04 17:16:16 +01:00
* AttachmentCollector constructor.
*
* @param ExportJob $job
*/
public function __construct(ExportJob $job)
{
parent::__construct($job);
2016-02-23 07:05:08 +01:00
// make storage:
$this->uploadDisk = Storage::disk('upload');
$this->exportDisk = Storage::disk('export');
// todo needs work for new importer (potentially collect other types as well)
2016-02-23 07:05:08 +01:00
$this->expected = 'csv-upload-' . Auth::user()->id . '-';
2016-02-04 17:16:16 +01:00
}
2016-02-05 09:25:15 +01:00
/**
2016-04-06 16:37:28 +02:00
* @return bool
2016-02-05 09:25:15 +01:00
*/
2016-04-06 16:37:28 +02:00
public function run(): bool
2016-02-04 17:16:16 +01:00
{
// grab upload directory.
2016-02-23 07:05:08 +01:00
$files = $this->uploadDisk->files();
2016-02-04 17:16:16 +01:00
foreach ($files as $entry) {
2016-02-23 07:05:08 +01:00
$this->processOldUpload($entry);
}
2016-04-25 18:43:09 +02:00
2016-04-06 16:37:28 +02:00
return true;
2016-02-23 07:05:08 +01: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 isValidFile(string $entry): bool
{
$len = strlen($this->expected);
if (substr($entry, 0, $len) === $this->expected) {
return true;
}
2016-02-04 17:16:16 +01:00
2016-02-23 07:05:08 +01:00
return false;
}
/**
* @param $entry
*/
private function processOldUpload(string $entry)
{
$content = '';
2016-02-04 17:16:16 +01:00
2016-02-23 07:05:08 +01:00
if ($this->isValidFile($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());
2016-02-04 17:16:16 +01:00
}
}
2016-02-23 07:05:08 +01:00
if (strlen($content) > 0) {
// continue with file:
$date = $this->getOriginalUploadDate($entry);
$file = $this->job->key . '-Old CSV import dated ' . $date . '.csv';
$this->exportDisk->put($file, $content);
$this->getFiles()->push($file);
}
2016-02-04 17:16:16 +01:00
}
2016-02-10 16:01:18 +01:00
}