mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Can configure file upload in file imports.
This commit is contained in:
@@ -20,12 +20,12 @@
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Support\Import\Configuration;
|
||||
namespace FireflyIII\Support\Import\Configuration\File;
|
||||
|
||||
use FireflyIII\Models\ImportJob;
|
||||
use Illuminate\Support\MessageBag;
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
* Class ConfigurationInterface.
|
||||
*/
|
||||
interface ConfigurationInterface
|
||||
@@ -35,14 +35,7 @@ interface ConfigurationInterface
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getData(): array;
|
||||
|
||||
/**
|
||||
* Return possible warning to user.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getWarningMessage(): string;
|
||||
public function getNextData(): array;
|
||||
|
||||
/**
|
||||
* @param ImportJob $job
|
||||
@@ -52,11 +45,11 @@ interface ConfigurationInterface
|
||||
public function setJob(ImportJob $job);
|
||||
|
||||
/**
|
||||
* Store the result.
|
||||
* Store data associated with current stage.
|
||||
*
|
||||
* @param array $data
|
||||
*
|
||||
* @return bool
|
||||
* @return MessageBag
|
||||
*/
|
||||
public function storeConfiguration(array $data): bool;
|
||||
public function configureJob(array $data): MessageBag;
|
||||
}
|
167
app/Support/Import/Configuration/File/ConfigureUploadHandler.php
Normal file
167
app/Support/Import/Configuration/File/ConfigureUploadHandler.php
Normal file
@@ -0,0 +1,167 @@
|
||||
<?php
|
||||
/**
|
||||
* ConfigureUploadHandlerphp
|
||||
* Copyright (c) 2018 thegrumpydictator@gmail.com
|
||||
*
|
||||
* This file is part of Firefly III.
|
||||
*
|
||||
* 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
|
||||
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Support\Import\Configuration\File;
|
||||
|
||||
use FireflyIII\Models\ImportJob;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
|
||||
use Illuminate\Support\MessageBag;
|
||||
use Log;
|
||||
|
||||
|
||||
/**
|
||||
* Class ConfigureUploadHandler
|
||||
*
|
||||
* @package FireflyIII\Support\Import\Configuration\File
|
||||
*/
|
||||
class ConfigureUploadHandler implements ConfigurationInterface
|
||||
{
|
||||
/** @var ImportJob */
|
||||
private $importJob;
|
||||
|
||||
/** @var ImportJobRepositoryInterface */
|
||||
private $repository;
|
||||
|
||||
/** @var AccountRepositoryInterface */
|
||||
private $accountRepos;
|
||||
|
||||
/**
|
||||
* Get the data necessary to show the configuration screen.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getNextData(): array
|
||||
{
|
||||
$delimiters = [
|
||||
',' => trans('form.csv_comma'),
|
||||
';' => trans('form.csv_semicolon'),
|
||||
'tab' => trans('form.csv_tab'),
|
||||
];
|
||||
$config = $this->importJob->configuration;
|
||||
$config['date-format'] = $config['date-format'] ?? 'Ymd';
|
||||
$specifics = [];
|
||||
$this->repository->setConfiguration($this->importJob, $config);
|
||||
|
||||
// collect specifics.
|
||||
foreach (config('csv.import_specifics') as $name => $className) {
|
||||
$specifics[$name] = [
|
||||
'name' => $className::getName(),
|
||||
'description' => $className::getDescription(),
|
||||
];
|
||||
}
|
||||
|
||||
$data = [
|
||||
'accounts' => [],
|
||||
'specifix' => [],
|
||||
'delimiters' => $delimiters,
|
||||
'specifics' => $specifics,
|
||||
];
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ImportJob $job
|
||||
*
|
||||
* @return ConfigurationInterface
|
||||
*/
|
||||
public function setJob(ImportJob $job)
|
||||
{
|
||||
$this->importJob = $job;
|
||||
$this->repository = app(ImportJobRepositoryInterface::class);
|
||||
$this->repository->setUser($job->user);
|
||||
$this->accountRepos = app(AccountRepositoryInterface::class);
|
||||
$this->accountRepos->setUser($job->user);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Store data associated with current stage.
|
||||
*
|
||||
* @param array $data
|
||||
*
|
||||
* @return MessageBag
|
||||
*/
|
||||
public function configureJob(array $data): MessageBag
|
||||
{
|
||||
$config = $this->importJob->configuration;
|
||||
$complete = true;
|
||||
|
||||
// collect values:
|
||||
$importId = isset($data['csv_import_account']) ? (int)$data['csv_import_account'] : 0;
|
||||
$delimiter = (string)$data['csv_delimiter'];
|
||||
$config['has-headers'] = (int)($data['has_headers'] ?? 0.0) === 1;
|
||||
$config['date-format'] = (string)$data['date_format'];
|
||||
$config['delimiter'] = 'tab' === $delimiter ? "\t" : $delimiter;
|
||||
$config['apply-rules'] = (int)($data['apply_rules'] ?? 0.0) === 1;
|
||||
$config['specifics'] = $this->getSpecifics($data);
|
||||
// validate values:
|
||||
$account = $this->accountRepos->findNull($importId);
|
||||
|
||||
// respond to invalid account:
|
||||
if (null === $account) {
|
||||
Log::error('Could not find anything for csv_import_account.', ['id' => $importId]);
|
||||
$complete = false;
|
||||
}
|
||||
if (null !== $account) {
|
||||
$config['import-account'] = $account->id;
|
||||
}
|
||||
|
||||
$this->repository->setConfiguration($this->importJob, $config);
|
||||
if ($complete) {
|
||||
$this->repository->setStage($this->importJob, 'roles');
|
||||
}
|
||||
if (!$complete) {
|
||||
$messages = new MessageBag;
|
||||
$messages->add('account', trans('import.invalid_import_account'));
|
||||
|
||||
return $messages;
|
||||
}
|
||||
|
||||
return new MessageBag;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getSpecifics(array $data): array
|
||||
{
|
||||
$return = [];
|
||||
// check if specifics given are correct:
|
||||
if (isset($data['specifics']) && \is_array($data['specifics'])) {
|
||||
|
||||
foreach ($data['specifics'] as $name) {
|
||||
// verify their content.
|
||||
$className = sprintf('FireflyIII\Import\Specifics\%s', $name);
|
||||
if (class_exists($className)) {
|
||||
$return[$name] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
}
|
176
app/Support/Import/Configuration/File/NewFileJobHandler.php
Normal file
176
app/Support/Import/Configuration/File/NewFileJobHandler.php
Normal file
@@ -0,0 +1,176 @@
|
||||
<?php
|
||||
/**
|
||||
* NewFileJobHandler.php
|
||||
* Copyright (c) 2018 thegrumpydictator@gmail.com
|
||||
*
|
||||
* This file is part of Firefly III.
|
||||
*
|
||||
* 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
|
||||
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Support\Import\Configuration\File;
|
||||
|
||||
use Crypt;
|
||||
use FireflyIII\Console\Commands\Import;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Models\Attachment;
|
||||
use FireflyIII\Models\ImportJob;
|
||||
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
|
||||
use Illuminate\Contracts\Encryption\DecryptException;
|
||||
use Illuminate\Contracts\Filesystem\FileNotFoundException;
|
||||
use Illuminate\Support\MessageBag;
|
||||
use Log;
|
||||
use Storage;
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Class NewFileJobHandler
|
||||
*
|
||||
* @package FireflyIII\Support\Import\Configuration\File
|
||||
*/
|
||||
class NewFileJobHandler implements ConfigurationInterface
|
||||
{
|
||||
/** @var ImportJob */
|
||||
private $importJob;
|
||||
|
||||
/** @var ImportJobRepositoryInterface */
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* Get the data necessary to show the configuration screen.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getNextData(): array
|
||||
{
|
||||
$importFileTypes = [];
|
||||
$defaultImportType = config('import.options.file.default_import_format');
|
||||
|
||||
foreach (config('import.options.file.import_formats') as $type) {
|
||||
$importFileTypes[$type] = trans('import.import_file_type_' . $type);
|
||||
}
|
||||
|
||||
return [
|
||||
'default_type' => $defaultImportType,
|
||||
'file_types' => $importFileTypes,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ImportJob $job
|
||||
*/
|
||||
public function setJob(ImportJob $job): void
|
||||
{
|
||||
$this->importJob = $job;
|
||||
$this->repository = app(ImportJobRepositoryInterface::class);
|
||||
$this->repository->setUser($job->user);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Store data associated with current stage.
|
||||
*
|
||||
* @param array $data
|
||||
*
|
||||
* @throws FireflyException
|
||||
* @return MessageBag
|
||||
*/
|
||||
public function configureJob(array $data): MessageBag
|
||||
{
|
||||
// nothing to store, validate upload
|
||||
// and push to next stage.
|
||||
$messages = new MessageBag;
|
||||
$attachments = $this->importJob->attachments;
|
||||
/** @var Attachment $attachment */
|
||||
foreach ($attachments as $attachment) {
|
||||
|
||||
// check if content is UTF8:
|
||||
if (!$this->isUTF8($attachment)) {
|
||||
$message = trans('import.file_not_utf8');
|
||||
Log::error($message);
|
||||
$messages->add('import_file', $message);
|
||||
// delete attachment:
|
||||
try {
|
||||
$attachment->delete();
|
||||
} catch (Exception $e) {
|
||||
throw new FireflyException(sprintf('Could not delete attachment: %s', $e->getMessage()));
|
||||
}
|
||||
|
||||
return $messages;
|
||||
}
|
||||
|
||||
// if file is configuration file, store it into the job.
|
||||
if ($attachment->filename === 'configuration_file') {
|
||||
$this->storeConfig($attachment);
|
||||
}
|
||||
}
|
||||
|
||||
$this->repository->setStage($this->importJob, 'configure-upload');
|
||||
|
||||
return new MessageBag();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Attachment $attachment
|
||||
*
|
||||
* @return bool
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function isUTF8(Attachment $attachment): bool
|
||||
{
|
||||
$disk = Storage::disk('upload');
|
||||
try {
|
||||
$content = $disk->get(sprintf('at-%d.data', $attachment->id));
|
||||
$content = Crypt::decrypt($content);
|
||||
} catch (FileNotFoundException|DecryptException $e) {
|
||||
Log::error($e->getMessage());
|
||||
throw new FireflyException($e->getMessage());
|
||||
}
|
||||
|
||||
$result = mb_detect_encoding($content, 'UTF-8', true);
|
||||
if ($result === false) {
|
||||
return false;
|
||||
}
|
||||
if ($result !== 'ASCII' && $result !== 'UTF-8') {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Attachment $attachment
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function storeConfig(Attachment $attachment): void
|
||||
{
|
||||
$disk = Storage::disk('upload');
|
||||
try {
|
||||
$content = $disk->get(sprintf('at-%d.data', $attachment->id));
|
||||
$content = Crypt::decrypt($content);
|
||||
} catch (FileNotFoundException $e) {
|
||||
Log::error($e->getMessage());
|
||||
throw new FireflyException($e->getMessage());
|
||||
}
|
||||
$json = json_decode($content, true);
|
||||
if (null !== $json) {
|
||||
$this->repository->setConfiguration($this->importJob, $json);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user