firefly-iii/app/Repositories/ImportJob/ImportJobRepository.php

315 lines
9.3 KiB
PHP
Raw Normal View History

2016-06-10 14:00:00 -05:00
<?php
/**
* ImportJobRepository.php
2017-10-21 01:40:00 -05:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
2016-06-10 14:00:00 -05: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:44:05 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
2016-06-10 14:00:00 -05:00
*/
declare(strict_types=1);
2016-06-10 14:00:00 -05:00
namespace FireflyIII\Repositories\ImportJob;
use Crypt;
2016-09-17 02:50:40 -05:00
use FireflyIII\Exceptions\FireflyException;
2016-06-10 14:00:00 -05:00
use FireflyIII\Models\ImportJob;
2018-01-05 10:29:42 -06:00
use FireflyIII\Models\TransactionJournalMeta;
use FireflyIII\Repositories\User\UserRepositoryInterface;
2016-06-10 14:00:00 -05:00
use FireflyIII\User;
use Illuminate\Support\Str;
use Log;
use SplFileObject;
use Storage;
use Symfony\Component\HttpFoundation\File\UploadedFile;
2016-06-10 14:00:00 -05:00
/**
2017-11-15 05:25:49 -06:00
* Class ImportJobRepository.
2016-06-10 14:00:00 -05:00
*/
class ImportJobRepository implements ImportJobRepositoryInterface
{
/** @var User */
private $user;
2018-01-05 10:29:42 -06:00
/**
* @param ImportJob $job
* @param int $steps
*
* @return ImportJob
*/
public function addStepsDone(ImportJob $job, int $steps = 1): ImportJob
{
$job->addStepsDone($steps);
return $job;
}
/**
* Return number of imported rows with this hash value.
*
* @param string $hash
*
* @return int
*/
public function countByHash(string $hash): int
{
$json = json_encode($hash);
$count = TransactionJournalMeta::leftJoin('transaction_journals', 'transaction_journals.id', '=', 'journal_meta.transaction_journal_id')
->where('data', $json)
->where('name', 'importHash')
->count();
return intval($count);
}
2016-06-10 14:00:00 -05:00
/**
* @param string $type
2016-06-10 14:00:00 -05:00
*
* @return ImportJob
2017-11-15 05:25:49 -06:00
*
2016-10-09 00:58:27 -05:00
* @throws FireflyException
2016-06-10 14:00:00 -05:00
*/
public function create(string $type): ImportJob
2016-06-10 14:00:00 -05:00
{
$count = 0;
$type = strtolower($type);
2016-09-17 02:50:40 -05:00
2016-06-10 14:00:00 -05:00
while ($count < 30) {
$key = Str::random(12);
$existing = $this->findByKey($key);
2017-11-15 05:25:49 -06:00
if (null === $existing->id) {
2016-06-10 14:00:00 -05:00
$importJob = new ImportJob;
$importJob->user()->associate($this->user);
$importJob->file_type = $type;
$importJob->key = Str::random(12);
$importJob->status = 'new';
2018-01-03 00:35:29 -06:00
$importJob->configuration = [];
$importJob->extended_status = [
2017-06-23 23:57:24 -05:00
'steps' => 0,
'done' => 0,
'tag' => 0,
'errors' => [],
];
2016-06-10 14:00:00 -05:00
$importJob->save();
// breaks the loop:
return $importJob;
}
2017-11-15 05:25:49 -06:00
++$count;
2016-06-10 14:00:00 -05:00
}
throw new FireflyException('Could not create an import job with a unique key after 30 tries.');
2016-06-10 14:00:00 -05:00
}
/**
* @param string $key
*
* @return ImportJob
*/
public function findByKey(string $key): ImportJob
{
2018-01-04 11:34:51 -06:00
/** @var ImportJob $result */
$result = $this->user->importJobs()->where('key', $key)->first(['import_jobs.*']);
2017-11-15 05:25:49 -06:00
if (null === $result) {
2016-06-10 14:00:00 -05:00
return new ImportJob;
}
return $result;
}
2017-01-30 09:46:30 -06:00
2018-01-04 11:34:51 -06:00
/**
* Return configuration of job.
*
* @param ImportJob $job
*
* @return array
*/
public function getConfiguration(ImportJob $job): array
{
$config = $job->configuration;
if (is_array($config)) {
return $config;
}
return [];
}
2018-01-05 10:29:42 -06:00
/**
* Return extended status of job.
*
* @param ImportJob $job
*
* @return array
*/
public function getExtendedStatus(ImportJob $job): array
{
$status = $job->extended_status;
if (is_array($status)) {
return $status;
}
return [];
}
/**
* @param ImportJob $job
* @param UploadedFile $file
*
* @return bool
*/
public function processConfiguration(ImportJob $job, UploadedFile $file): bool
{
/** @var UserRepositoryInterface $repository */
$repository = app(UserRepositoryInterface::class);
// demo user's configuration upload is ignored completely.
2017-06-14 13:13:19 -05:00
if (!$repository->hasRole($this->user, 'demo')) {
Log::debug(
2017-11-15 03:52:29 -06:00
'Uploaded configuration file',
['name' => $file->getClientOriginalName(), 'size' => $file->getSize(), 'mime' => $file->getClientMimeType()]
);
$configFileObject = new SplFileObject($file->getRealPath());
$configRaw = $configFileObject->fread($configFileObject->getSize());
$configuration = json_decode($configRaw, true);
2017-11-15 05:25:49 -06:00
if (null !== $configuration && is_array($configuration)) {
Log::debug('Found configuration', $configuration);
$this->setConfiguration($job, $configuration);
}
}
return true;
}
/**
* @param ImportJob $job
* @param null|UploadedFile $file
*
* @return bool
2017-12-22 11:32:43 -06:00
*
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
public function processFile(ImportJob $job, ?UploadedFile $file): bool
{
if (is_null($file)) {
return false;
}
/** @var UserRepositoryInterface $repository */
$repository = app(UserRepositoryInterface::class);
$newName = sprintf('%s.upload', $job->key);
$uploaded = new SplFileObject($file->getRealPath());
$content = trim($uploaded->fread($uploaded->getSize()));
$contentEncrypted = Crypt::encrypt($content);
$disk = Storage::disk('upload');
// user is demo user, replace upload with prepared file.
if ($repository->hasRole($this->user, 'demo')) {
$stubsDisk = Storage::disk('stubs');
$content = $stubsDisk->get('demo-import.csv');
$contentEncrypted = Crypt::encrypt($content);
$disk->put($newName, $contentEncrypted);
Log::debug('Replaced upload with demo file.');
// also set up prepared configuration.
$configuration = json_decode($stubsDisk->get('demo-configuration.json'), true);
$this->setConfiguration($job, $configuration);
Log::debug('Set configuration for demo user', $configuration);
}
if (!$repository->hasRole($this->user, 'demo')) {
// user is not demo, process original upload:
$disk->put($newName, $contentEncrypted);
Log::debug('Uploaded file', ['name' => $file->getClientOriginalName(), 'size' => $file->getSize(), 'mime' => $file->getClientMimeType()]);
}
return true;
}
2017-03-19 11:54:21 -05:00
/**
* @param ImportJob $job
* @param array $configuration
*
* @return ImportJob
*/
public function setConfiguration(ImportJob $job, array $configuration): ImportJob
{
Log::debug(sprintf('Incoming config for job "%s" is: ', $job->key), $configuration);
$currentConfig = $job->configuration;
$newConfig = array_merge($currentConfig, $configuration);
$job->configuration = $newConfig;
2017-03-19 11:54:21 -05:00
$job->save();
Log::debug(sprintf('Set config of job "%s" to: ', $job->key), $newConfig);
2017-03-19 11:54:21 -05:00
return $job;
}
2018-01-05 10:29:42 -06:00
/**
* @param ImportJob $job
* @param array $array
*
* @return ImportJob
*/
public function setExtendedStatus(ImportJob $job, array $array): ImportJob
{
2018-01-06 13:26:21 -06:00
// remove 'errors' because it gets larger and larger and larger...
$display = $array;
unset($display['errors']);
Log::debug(sprintf('Incoming extended status for job "%s" is (except errors): ', $job->key), $display);
2018-01-05 10:29:42 -06:00
$currentStatus = $job->extended_status;
$newStatus = array_merge($currentStatus, $array);
$job->extended_status = $newStatus;
$job->save();
2018-01-06 13:26:21 -06:00
// remove 'errors' because it gets larger and larger and larger...
unset($newStatus['errors']);
Log::debug(sprintf('Set extended status of job "%s" to (except errors): ', $job->key), $newStatus);
2018-01-05 10:29:42 -06:00
return $job;
}
2017-01-30 09:46:30 -06:00
/**
* @param User $user
*/
public function setUser(User $user)
{
$this->user = $user;
}
2017-03-19 11:54:21 -05:00
/**
* @param ImportJob $job
* @param string $status
*
* @return ImportJob
*/
public function updateStatus(ImportJob $job, string $status): ImportJob
{
$job->status = $status;
$job->save();
return $job;
}
/**
* Return import file content.
*
* @param ImportJob $job
*
* @return string
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
public function uploadFileContents(ImportJob $job): string
{
return $job->uploadFileContents();
}
2016-08-12 08:10:03 -05:00
}