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.
|
2016-10-04 23:52:15 -05:00
|
|
|
*
|
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
|
|
|
*/
|
2017-04-09 00:44:22 -05:00
|
|
|
declare(strict_types=1);
|
2016-06-10 14:00:00 -05:00
|
|
|
|
|
|
|
namespace FireflyIII\Repositories\ImportJob;
|
|
|
|
|
2017-06-10 08:09:41 -05:00
|
|
|
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;
|
2017-06-10 08:09:41 -05:00
|
|
|
use FireflyIII\Repositories\User\UserRepositoryInterface;
|
2016-06-10 14:00:00 -05:00
|
|
|
use FireflyIII\User;
|
|
|
|
use Illuminate\Support\Str;
|
2017-06-10 08:09:41 -05:00
|
|
|
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;
|
|
|
|
|
|
|
|
/**
|
2017-12-16 01:03:35 -06: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
|
|
|
*/
|
2017-12-16 01:03:35 -06:00
|
|
|
public function create(string $type): ImportJob
|
2016-06-10 14:00:00 -05:00
|
|
|
{
|
2017-12-16 01:03:35 -06: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);
|
2017-12-16 01:03:35 -06:00
|
|
|
$importJob->file_type = $type;
|
2016-08-13 14:51:01 -05:00
|
|
|
$importJob->key = Str::random(12);
|
2017-06-10 08:09:41 -05:00
|
|
|
$importJob->status = 'new';
|
2018-01-03 00:35:29 -06:00
|
|
|
$importJob->configuration = [];
|
2016-08-14 04:31:09 -05:00
|
|
|
$importJob->extended_status = [
|
2017-06-23 23:57:24 -05:00
|
|
|
'steps' => 0,
|
|
|
|
'done' => 0,
|
|
|
|
'tag' => 0,
|
|
|
|
'errors' => [],
|
2016-08-14 04:31:09 -05:00
|
|
|
];
|
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
|
|
|
}
|
2017-12-16 01:03:35 -06: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
|
|
|
|
{
|
2016-08-11 03:21:32 -05:00
|
|
|
$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
|
|
|
|
2017-06-10 08:09:41 -05:00
|
|
|
/**
|
|
|
|
* @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')) {
|
2017-06-10 08:09:41 -05:00
|
|
|
Log::debug(
|
2017-11-15 03:52:29 -06:00
|
|
|
'Uploaded configuration file',
|
|
|
|
['name' => $file->getClientOriginalName(), 'size' => $file->getSize(), 'mime' => $file->getClientMimeType()]
|
2017-06-10 08:09:41 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
$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)) {
|
2017-06-10 08:09:41 -05:00
|
|
|
Log::debug('Found configuration', $configuration);
|
|
|
|
$this->setConfiguration($job, $configuration);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-16 10:19:10 -06:00
|
|
|
* @param ImportJob $job
|
|
|
|
* @param null|UploadedFile $file
|
2017-06-10 08:09:41 -05:00
|
|
|
*
|
2017-12-16 10:19:10 -06:00
|
|
|
* @return bool
|
2017-12-22 11:32:43 -06:00
|
|
|
*
|
2017-12-16 10:19:10 -06:00
|
|
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
|
2017-06-10 08:09:41 -05:00
|
|
|
*/
|
2017-12-16 10:19:10 -06:00
|
|
|
public function processFile(ImportJob $job, ?UploadedFile $file): bool
|
2017-06-10 08:09:41 -05:00
|
|
|
{
|
2017-12-16 10:19:10 -06:00
|
|
|
if (is_null($file)) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-06-10 08:09:41 -05:00
|
|
|
/** @var UserRepositoryInterface $repository */
|
|
|
|
$repository = app(UserRepositoryInterface::class);
|
|
|
|
$newName = sprintf('%s.upload', $job->key);
|
|
|
|
$uploaded = new SplFileObject($file->getRealPath());
|
2017-12-16 10:19:10 -06:00
|
|
|
$content = trim($uploaded->fread($uploaded->getSize()));
|
2017-06-10 08:09:41 -05:00
|
|
|
$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
|
|
|
|
{
|
2017-12-16 10:19:10 -06:00
|
|
|
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();
|
2017-12-16 10:19:10 -06:00
|
|
|
Log::debug(sprintf('Set config of job "%s" to: ', $job->key), $newConfig);
|
2017-03-19 11:54:21 -05: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;
|
|
|
|
}
|
2016-08-12 08:10:03 -05:00
|
|
|
}
|