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;
|
2018-05-06 00:09:08 -05:00
|
|
|
use FireflyIII\Models\Attachment;
|
2016-06-10 14:00:00 -05:00
|
|
|
use FireflyIII\Models\ImportJob;
|
2018-05-03 10:23:16 -05:00
|
|
|
use FireflyIII\Models\Tag;
|
2018-01-05 10:29:42 -06:00
|
|
|
use FireflyIII\Models\TransactionJournalMeta;
|
2017-06-10 08:09:41 -05:00
|
|
|
use FireflyIII\Repositories\User\UserRepositoryInterface;
|
2016-06-10 14:00:00 -05:00
|
|
|
use FireflyIII\User;
|
2018-05-10 16:01:21 -05:00
|
|
|
use Illuminate\Support\Collection;
|
2018-05-06 00:09:08 -05:00
|
|
|
use Illuminate\Support\MessageBag;
|
2016-06-10 14:00:00 -05:00
|
|
|
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
|
|
|
|
{
|
2018-05-06 00:09:08 -05:00
|
|
|
/** @var \Illuminate\Contracts\Filesystem\Filesystem */
|
|
|
|
protected $uploadDisk;
|
2018-05-10 16:01:21 -05:00
|
|
|
/** @var int */
|
|
|
|
private $maxUploadSize;
|
|
|
|
/** @var User */
|
|
|
|
private $user;
|
2018-05-06 00:09:08 -05:00
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->maxUploadSize = (int)config('firefly.maxUploadSize');
|
|
|
|
$this->uploadDisk = Storage::disk('upload');
|
|
|
|
}
|
2016-06-10 14:00:00 -05:00
|
|
|
|
2018-01-10 09:49:32 -06:00
|
|
|
/**
|
|
|
|
* @param ImportJob $job
|
|
|
|
* @param int $index
|
|
|
|
* @param string $error
|
|
|
|
*
|
|
|
|
* @return ImportJob
|
|
|
|
*/
|
|
|
|
public function addError(ImportJob $job, int $index, string $error): ImportJob
|
|
|
|
{
|
2018-01-10 11:18:49 -06:00
|
|
|
$extended = $this->getExtendedStatus($job);
|
|
|
|
$extended['errors'][$index][] = $error;
|
2018-01-10 09:49:32 -06:00
|
|
|
|
2018-01-10 11:18:49 -06:00
|
|
|
return $this->setExtendedStatus($job, $extended);
|
2018-01-10 09:49:32 -06:00
|
|
|
}
|
|
|
|
|
2018-05-10 16:01:21 -05:00
|
|
|
/**
|
|
|
|
* Add message to job.
|
|
|
|
*
|
|
|
|
* @param ImportJob $job
|
|
|
|
* @param string $error
|
|
|
|
*
|
|
|
|
* @return ImportJob
|
|
|
|
*/
|
|
|
|
public function addErrorMessage(ImportJob $job, string $error): ImportJob
|
|
|
|
{
|
|
|
|
$errors = $job->errors;
|
|
|
|
$errors[] = $error;
|
|
|
|
$job->errors = $errors;
|
|
|
|
$job->save();
|
|
|
|
|
|
|
|
return $job;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
2018-01-10 11:18:49 -06:00
|
|
|
$status = $this->getExtendedStatus($job);
|
|
|
|
$status['done'] += $steps;
|
|
|
|
Log::debug(sprintf('Add %d to steps done for job "%s" making steps done %d', $steps, $job->key, $status['done']));
|
2018-01-05 10:29:42 -06:00
|
|
|
|
2018-01-10 12:06:27 -06:00
|
|
|
return $this->setExtendedStatus($job, $status);
|
2018-01-05 10:29:42 -06:00
|
|
|
}
|
|
|
|
|
2018-01-13 00:36:44 -06:00
|
|
|
/**
|
|
|
|
* @param ImportJob $job
|
|
|
|
* @param int $steps
|
|
|
|
*
|
|
|
|
* @return ImportJob
|
|
|
|
*/
|
|
|
|
public function addTotalSteps(ImportJob $job, int $steps = 1): ImportJob
|
|
|
|
{
|
|
|
|
$extended = $this->getExtendedStatus($job);
|
2018-04-02 08:10:40 -05:00
|
|
|
$total = (int)($extended['steps'] ?? 0);
|
2018-01-13 00:36:44 -06:00
|
|
|
$total += $steps;
|
|
|
|
$extended['steps'] = $total;
|
|
|
|
|
|
|
|
return $this->setExtendedStatus($job, $extended);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-01-05 10:29:42 -06:00
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
|
2018-04-02 08:10:40 -05:00
|
|
|
return (int)$count;
|
2018-01-05 10:29:42 -06:00
|
|
|
}
|
|
|
|
|
2016-06-10 14:00:00 -05:00
|
|
|
/**
|
2018-04-29 11:07:14 -05:00
|
|
|
* @param string $importProvider
|
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
|
|
|
*/
|
2018-04-29 11:07:14 -05:00
|
|
|
public function create(string $importProvider): ImportJob
|
2016-06-10 14:00:00 -05:00
|
|
|
{
|
2018-04-29 13:01:03 -05:00
|
|
|
$count = 0;
|
2018-04-29 11:07:14 -05:00
|
|
|
$importProvider = strtolower($importProvider);
|
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) {
|
2018-04-29 11:07:14 -05:00
|
|
|
$importJob = ImportJob::create(
|
|
|
|
[
|
|
|
|
'user_id' => $this->user->id,
|
2018-05-03 10:23:16 -05:00
|
|
|
'tag_id' => null,
|
2018-04-29 13:01:03 -05:00
|
|
|
'provider' => $importProvider,
|
2018-04-29 11:07:14 -05:00
|
|
|
'file_type' => '',
|
|
|
|
'key' => Str::random(12),
|
|
|
|
'status' => 'new',
|
|
|
|
'stage' => 'new',
|
|
|
|
'configuration' => [],
|
|
|
|
'extended_status' => [],
|
|
|
|
'transactions' => [],
|
2018-05-03 10:23:16 -05:00
|
|
|
'errors' => [],
|
2018-04-29 11:07:14 -05:00
|
|
|
]
|
|
|
|
);
|
2016-06-10 14:00:00 -05:00
|
|
|
|
|
|
|
// 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
|
|
|
|
{
|
2018-01-04 11:34:51 -06:00
|
|
|
/** @var ImportJob $result */
|
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
|
|
|
|
2018-05-10 16:01:21 -05:00
|
|
|
/**
|
|
|
|
* Return all attachments for job.
|
|
|
|
*
|
|
|
|
* @param ImportJob $job
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function getAttachments(ImportJob $job): Collection
|
|
|
|
{
|
|
|
|
return $job->attachments()->get();
|
|
|
|
}
|
|
|
|
|
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;
|
2018-04-27 23:23:13 -05:00
|
|
|
if (\is_array($config)) {
|
2018-01-04 11:34:51 -06:00
|
|
|
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;
|
2018-04-27 23:23:13 -05:00
|
|
|
if (\is_array($status)) {
|
2018-01-05 10:29:42 -06:00
|
|
|
return $status;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2018-01-10 11:18:49 -06:00
|
|
|
/**
|
|
|
|
* @param ImportJob $job
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getStatus(ImportJob $job): string
|
|
|
|
{
|
|
|
|
return $job->status;
|
|
|
|
}
|
|
|
|
|
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);
|
2018-02-04 01:14:22 -06:00
|
|
|
Log::debug(sprintf('Raw configuration is %s', $configRaw));
|
2018-04-27 23:23:13 -05:00
|
|
|
if (null !== $configuration && \is_array($configuration)) {
|
2017-06-10 08:09:41 -05:00
|
|
|
Log::debug('Found configuration', $configuration);
|
|
|
|
$this->setConfiguration($job, $configuration);
|
|
|
|
}
|
2018-02-04 01:14:22 -06:00
|
|
|
if (null === $configuration) {
|
|
|
|
Log::error('Uploaded configuration is NULL');
|
|
|
|
}
|
|
|
|
if (false === $configuration) {
|
|
|
|
Log::error('Uploaded configuration is FALSE');
|
|
|
|
}
|
2017-06-10 08:09:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
*
|
2018-03-29 12:01:47 -05:00
|
|
|
* @throws \Illuminate\Contracts\Encryption\EncryptException
|
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
|
|
|
{
|
2018-04-01 09:00:15 -05:00
|
|
|
if (null === $file) {
|
2017-12-16 10:19:10 -06:00
|
|
|
return false;
|
|
|
|
}
|
2017-06-10 08:09:41 -05:00
|
|
|
/** @var UserRepositoryInterface $repository */
|
2018-04-01 09:00:15 -05:00
|
|
|
$repository = app(UserRepositoryInterface::class);
|
|
|
|
$newName = sprintf('%s.upload', $job->key);
|
|
|
|
$uploaded = new SplFileObject($file->getRealPath());
|
|
|
|
$content = trim($uploaded->fread($uploaded->getSize()));
|
|
|
|
|
|
|
|
// verify content:
|
|
|
|
$result = mb_detect_encoding($content, 'UTF-8', true);
|
|
|
|
if ($result === false) {
|
|
|
|
Log::error(sprintf('Cannot detect encoding for uploaded import file "%s".', $file->getClientOriginalName()));
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ($result !== 'ASCII' && $result !== 'UTF-8') {
|
|
|
|
Log::error(sprintf('Uploaded import file is %s instead of UTF8!', var_export($result, true)));
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
2018-05-19 03:44:33 -05:00
|
|
|
Log::debug('Updating configuration...');
|
|
|
|
//Log::debug(sprintf('Incoming config for job "%s" is: ', $job->key), $configuration);
|
2017-12-16 10:19:10 -06:00
|
|
|
$currentConfig = $job->configuration;
|
|
|
|
$newConfig = array_merge($currentConfig, $configuration);
|
|
|
|
$job->configuration = $newConfig;
|
2017-03-19 11:54:21 -05:00
|
|
|
$job->save();
|
2018-06-06 14:23:00 -05:00
|
|
|
|
2018-05-19 03:44:33 -05:00
|
|
|
//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
|
|
|
|
{
|
|
|
|
$currentStatus = $job->extended_status;
|
|
|
|
$newStatus = array_merge($currentStatus, $array);
|
|
|
|
$job->extended_status = $newStatus;
|
|
|
|
$job->save();
|
2018-01-06 13:26:21 -06:00
|
|
|
|
2018-01-05 10:29:42 -06:00
|
|
|
return $job;
|
|
|
|
}
|
|
|
|
|
2018-04-29 23:37:29 -05:00
|
|
|
/**
|
|
|
|
* @param ImportJob $job
|
|
|
|
* @param string $stage
|
|
|
|
*
|
|
|
|
* @return ImportJob
|
|
|
|
*/
|
|
|
|
public function setStage(ImportJob $job, string $stage): ImportJob
|
|
|
|
{
|
|
|
|
$job->stage = $stage;
|
|
|
|
$job->save();
|
|
|
|
|
|
|
|
return $job;
|
|
|
|
}
|
|
|
|
|
2017-03-19 11:54:21 -05:00
|
|
|
/**
|
|
|
|
* @param ImportJob $job
|
|
|
|
* @param string $status
|
|
|
|
*
|
|
|
|
* @return ImportJob
|
|
|
|
*/
|
2018-01-10 09:49:32 -06:00
|
|
|
public function setStatus(ImportJob $job, string $status): ImportJob
|
2017-03-19 11:54:21 -05:00
|
|
|
{
|
2018-05-01 13:47:38 -05:00
|
|
|
Log::debug(sprintf('Set status of job "%s" to "%s"', $job->key, $status));
|
2017-03-19 11:54:21 -05:00
|
|
|
$job->status = $status;
|
|
|
|
$job->save();
|
|
|
|
|
|
|
|
return $job;
|
|
|
|
}
|
2018-01-04 12:33:16 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ImportJob $job
|
2018-01-13 00:36:44 -06:00
|
|
|
* @param int $steps
|
2018-01-04 12:33:16 -06:00
|
|
|
*
|
2018-01-10 09:49:32 -06:00
|
|
|
* @return ImportJob
|
2018-01-04 12:33:16 -06:00
|
|
|
*/
|
2018-01-10 09:49:32 -06:00
|
|
|
public function setStepsDone(ImportJob $job, int $steps): ImportJob
|
2018-01-04 12:33:16 -06:00
|
|
|
{
|
2018-01-13 00:36:44 -06:00
|
|
|
$status = $this->getExtendedStatus($job);
|
|
|
|
$status['done'] = $steps;
|
|
|
|
Log::debug(sprintf('Set steps done for job "%s" to %d', $job->key, $steps));
|
2018-01-10 09:49:32 -06:00
|
|
|
|
2018-01-13 00:36:44 -06:00
|
|
|
return $this->setExtendedStatus($job, $status);
|
2018-01-04 12:33:16 -06:00
|
|
|
}
|
2018-01-08 13:20:45 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ImportJob $job
|
2018-05-10 16:01:21 -05:00
|
|
|
* @param Tag $tag
|
2018-01-08 13:20:45 -06:00
|
|
|
*
|
|
|
|
* @return ImportJob
|
|
|
|
*/
|
2018-05-10 16:01:21 -05:00
|
|
|
public function setTag(ImportJob $job, Tag $tag): ImportJob
|
2018-01-08 13:20:45 -06:00
|
|
|
{
|
2018-05-10 16:01:21 -05:00
|
|
|
$job->tag()->associate($tag);
|
2018-01-10 09:49:32 -06:00
|
|
|
$job->save();
|
2018-01-08 13:20:45 -06:00
|
|
|
|
|
|
|
return $job;
|
|
|
|
}
|
2018-01-10 09:49:32 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ImportJob $job
|
2018-05-10 16:01:21 -05:00
|
|
|
* @param int $count
|
2018-01-10 09:49:32 -06:00
|
|
|
*
|
2018-05-10 16:01:21 -05:00
|
|
|
* @return ImportJob
|
2018-01-10 09:49:32 -06:00
|
|
|
*/
|
2018-05-10 16:01:21 -05:00
|
|
|
public function setTotalSteps(ImportJob $job, int $count): ImportJob
|
2018-01-10 09:49:32 -06:00
|
|
|
{
|
2018-05-10 16:01:21 -05:00
|
|
|
$status = $this->getExtendedStatus($job);
|
|
|
|
$status['steps'] = $count;
|
|
|
|
Log::debug(sprintf('Set total steps for job "%s" to %d', $job->key, $count));
|
|
|
|
|
|
|
|
return $this->setExtendedStatus($job, $status);
|
2018-01-10 09:49:32 -06:00
|
|
|
}
|
2018-05-03 10:23:16 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ImportJob $job
|
|
|
|
* @param array $transactions
|
|
|
|
*
|
|
|
|
* @return ImportJob
|
|
|
|
*/
|
|
|
|
public function setTransactions(ImportJob $job, array $transactions): ImportJob
|
|
|
|
{
|
|
|
|
$job->transactions = $transactions;
|
|
|
|
$job->save();
|
|
|
|
|
|
|
|
return $job;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-10 16:01:21 -05:00
|
|
|
* @param User $user
|
2018-05-06 00:09:08 -05:00
|
|
|
*/
|
2018-05-10 16:01:21 -05:00
|
|
|
public function setUser(User $user)
|
2018-05-06 00:09:08 -05:00
|
|
|
{
|
2018-05-10 16:01:21 -05:00
|
|
|
$this->user = $user;
|
2018-05-06 00:09:08 -05:00
|
|
|
}
|
|
|
|
|
2018-05-12 12:09:34 -05:00
|
|
|
/**
|
|
|
|
* Handle upload for job.
|
|
|
|
*
|
|
|
|
* @param ImportJob $job
|
|
|
|
* @param string $name
|
|
|
|
* @param UploadedFile $file
|
|
|
|
*
|
|
|
|
* @return MessageBag
|
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
|
|
|
public function storeCLIUpload(ImportJob $job, string $name, string $fileName): MessageBag
|
|
|
|
{
|
|
|
|
$messages = new MessageBag;
|
|
|
|
|
|
|
|
if (!file_exists($fileName)) {
|
|
|
|
$messages->add('notfound', sprintf('File not found: %s', $fileName));
|
|
|
|
|
|
|
|
return $messages;
|
|
|
|
}
|
|
|
|
|
|
|
|
$count = $job->attachments()->get()->filter(
|
|
|
|
function (Attachment $att) use ($name) {
|
|
|
|
return $att->filename === $name;
|
|
|
|
}
|
|
|
|
)->count();
|
|
|
|
|
|
|
|
if ($count > 0) {
|
|
|
|
// don't upload, but also don't complain about it.
|
|
|
|
Log::error(sprintf('Detected duplicate upload. Will ignore second "%s" file.', $name));
|
|
|
|
|
|
|
|
return new MessageBag;
|
|
|
|
}
|
|
|
|
$content = file_get_contents($fileName);
|
|
|
|
$attachment = new Attachment; // create Attachment object.
|
|
|
|
$attachment->user()->associate($job->user);
|
|
|
|
$attachment->attachable()->associate($job);
|
|
|
|
$attachment->md5 = md5($content);
|
|
|
|
$attachment->filename = $name;
|
|
|
|
$attachment->mime = 'plain/txt';
|
|
|
|
$attachment->size = \strlen($content);
|
|
|
|
$attachment->uploaded = 0;
|
|
|
|
$attachment->save();
|
|
|
|
$encrypted = Crypt::encrypt($content);
|
|
|
|
|
|
|
|
// store it:
|
|
|
|
$this->uploadDisk->put($attachment->fileName(), $encrypted);
|
|
|
|
$attachment->uploaded = 1; // update attachment
|
|
|
|
$attachment->save();
|
|
|
|
|
|
|
|
// return it.
|
|
|
|
return new MessageBag;
|
|
|
|
}
|
|
|
|
|
2018-05-06 00:09:08 -05:00
|
|
|
/**
|
|
|
|
* Handle upload for job.
|
|
|
|
*
|
|
|
|
* @param ImportJob $job
|
|
|
|
* @param string $name
|
|
|
|
* @param UploadedFile $file
|
|
|
|
*
|
|
|
|
* @return MessageBag
|
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
|
|
|
public function storeFileUpload(ImportJob $job, string $name, UploadedFile $file): MessageBag
|
|
|
|
{
|
|
|
|
$messages = new MessageBag;
|
|
|
|
if ($this->validSize($file)) {
|
|
|
|
$name = e($file->getClientOriginalName());
|
|
|
|
$messages->add('size', (string)trans('validation.file_too_large', ['name' => $name]));
|
|
|
|
|
|
|
|
return $messages;
|
|
|
|
}
|
|
|
|
$count = $job->attachments()->get()->filter(
|
2018-05-10 16:01:21 -05:00
|
|
|
function (Attachment $att) use ($name) {
|
2018-05-06 00:09:08 -05:00
|
|
|
return $att->filename === $name;
|
|
|
|
}
|
|
|
|
)->count();
|
|
|
|
|
|
|
|
if ($count > 0) {
|
|
|
|
// don't upload, but also don't complain about it.
|
|
|
|
Log::error(sprintf('Detected duplicate upload. Will ignore second "%s" file.', $name));
|
|
|
|
|
|
|
|
return new MessageBag;
|
|
|
|
}
|
|
|
|
|
|
|
|
$attachment = new Attachment; // create Attachment object.
|
|
|
|
$attachment->user()->associate($job->user);
|
|
|
|
$attachment->attachable()->associate($job);
|
|
|
|
$attachment->md5 = md5_file($file->getRealPath());
|
|
|
|
$attachment->filename = $name;
|
|
|
|
$attachment->mime = $file->getMimeType();
|
|
|
|
$attachment->size = $file->getSize();
|
|
|
|
$attachment->uploaded = 0;
|
|
|
|
$attachment->save();
|
|
|
|
$fileObject = $file->openFile('r');
|
|
|
|
$fileObject->rewind();
|
|
|
|
$content = $fileObject->fread($file->getSize());
|
|
|
|
$encrypted = Crypt::encrypt($content);
|
|
|
|
|
|
|
|
// store it:
|
|
|
|
$this->uploadDisk->put($attachment->fileName(), $encrypted);
|
|
|
|
$attachment->uploaded = 1; // update attachment
|
|
|
|
$attachment->save();
|
|
|
|
|
|
|
|
// return it.
|
|
|
|
return new MessageBag;
|
|
|
|
}
|
2018-05-10 16:01: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.
|
|
|
|
*
|
|
|
|
* @deprecated
|
|
|
|
*
|
|
|
|
* @param ImportJob $job
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
|
|
|
|
*/
|
|
|
|
public function uploadFileContents(ImportJob $job): string
|
|
|
|
{
|
|
|
|
return $job->uploadFileContents();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
*
|
|
|
|
* @param UploadedFile $file
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function validSize(UploadedFile $file): bool
|
|
|
|
{
|
|
|
|
$size = $file->getSize();
|
|
|
|
|
|
|
|
return $size > $this->maxUploadSize;
|
|
|
|
}
|
2016-08-12 08:10:03 -05:00
|
|
|
}
|