8000 Auto import: Honor JSON_CONFIGURATION_DIR by yparitcher · Pull Request #782 · firefly-iii/data-importer · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Auto import: Honor JSON_CONFIGURATION_DIR #782

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 28, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 52 additions & 82 deletions app/Console/AutoImports.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
use GrumpyDictator\FFIIIApiSupport\Request\GetAccountRequest;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;

/**
* Trait AutoImports
Expand Down 8000 Expand Up @@ -80,22 +81,30 @@ private function getFiles(string $directory): array
return [];
}
$files = array_diff($array, $ignore);
$return = [];
$importableFiles = [];
$jsonFiles = [];
foreach ($files as $file) {
// import importable file with JSON companion
// TODO may also need to be able to detect other file types. Or: detect any file with accompanying json file
if ('csv' === $this->getExtension($file) && $this->hasJsonConfiguration($directory, $file)) {
$return[] = $file;
if (in_array($this->getExtension($file), ['csv', 'xml'])) {
$importableFiles[] = $file;
}

if ('xml' === $this->getExtension($file) && $this->hasJsonConfiguration($directory, $file)) {
$return[] = $file;
// import JSON config files.
if ('json' === $this->getExtension($file)) {
$jsonFiles[] = $file;
}

// import JSON with no importable file.
// TODO must detect json files without accompanying camt/csv/whatever file.
if ('json' === $this->getExtension($file) && !$this->hasCsvFile($directory, $file)) {
$return[] = $file;
}
$return = [];
foreach ($importableFiles as $importableFile) {
$jsonFile = $this->getJsonConfiguration($directory, $importableFile);
if($jsonFile) {
$return[$jsonFile] = sprintf('%s/%s', $directory, $importableFile);
}
}
foreach ($jsonFiles as $jsonFile) {
$fullJson = sprintf('%s/%s', $directory, $jsonFile);
if (!array_key_exists($fullJson, $return)) {
$return[$fullJson] = $fullJson;
}
}

Expand All @@ -112,75 +121,64 @@ private function getExtension(string $file): string
return strtolower($parts[count($parts) - 1]);
}

/**
* This method only works on files with an extension with exactly three letters
* (i.e. "csv", "xml").
*/
private function hasJsonConfiguration(string $directory, string $file): bool
private function getExtensionLength(string $file): int
{
$short = substr($file, 0, -4);
$parts = explode('.', $file);
if (1 === count($parts)) {
return 0;
}
return strlen($parts[count($parts) - 1]) + 1;
}

private function getJsonConfiguration(string $directory, string $file): ?string
{
$extensionLength = $this->getExtensionLength($file);
$short = substr($file, 0, -$extensionLength);
$jsonFile = sprintf('%s.json', $short);
$fullJson = sprintf('%s/%s', $directory, $jsonFile);

if (!file_exists($fullJson)) {
$hasFallbackConfig = $this->hasFallbackConfig($directory);
if ($hasFallbackConfig) {
$this->line('Found fallback configuration file, which will be used for this file.');

return true;
}
$this->warn(sprintf('Cannot find JSON file "%s" nor fallback file expected to go with file "%s". This file will be ignored.', $jsonFile, $file));
if (file_exists($fullJson)) {
return $fullJson;
}
if (Storage::disk('configurations')->exists($jsonFile)){
return Storage::disk('configurations')->path($jsonFile);
}
$fallbackConfig = $this->getFallbackConfig($directory);
if ($fallbackConfig) {
$this->line('Found fallback configuration file, which will be used for this file.');

return false;
return $fallbackConfig;
}
$this->warn(sprintf('Cannot find JSON file "%s" nor fallback file expected to go with file "%s". This file will be ignored.', $jsonFile, $file));

return true;
return null;
}

private function hasFallbackConfig(string $directory): bool
private function getFallbackConfig(string $directory): ?string
{
if (false === config('importer.fallback_in_dir')) {
return false;
return null;
}
$configJsonFile = sprintf('%s/%s', $directory, config('importer.fallback_configuration'));
if (file_exists($configJsonFile) && is_readable($configJsonFile)) {
$content = file_get_contents($configJsonFile);

return json_validate($content);
return $configJsonFile;
}

return false;
}

/**
* TODO this function must be more universal.
*/
private function hasCsvFile(string $directory, string $file): bool
{
$short = substr($file, 0, -5);
$csvFile = sprintf('%s.csv', $short);
$fullJson = sprintf('%s/%s', $directory, $csvFile);
if (!file_exists($fullJson)) {
return false;
}

return true;
return null;
}

private function importFiles(string $directory, array $files): array
{
$exitCodes = [];

/** @var string $file */
foreach ($files as $file) {
$key = sprintf('%s/%s', $directory, $file);

foreach ($files as $jsonFile => $importableFile) {
try {
$exitCodes[$key] = $this->importFile($directory, $file);
$exitCodes[$importableFile] = $this->importFile($jsonFile, $importableFile);
} catch (ImporterErrorException $e) {
app('log')->error(sprintf('Could not complete import from file "%s".', $file));
app('log')->error($e->getMessage());
$exitCodes[$key] = 1;
$exitCodes[$importableFile] = 1;
}
// report has already been sent. Reset errors and continue.
$this->conversionErrors = [];
999F Expand All @@ -199,36 +197,8 @@ private function importFiles(string $directory, array $files): array
/**
* @throws ImporterErrorException
*/
private function importFile(string $directory, string $file): int
private function importFile(string $jsonFile, string $importableFile): int
{
app('log')->debug(sprintf('ImportFile: directory "%s"', $directory));
app('log')->debug(sprintf('ImportFile: file "%s"', $file));
$importableFile = sprintf('%s/%s', $directory, $file);
$jsonFile = sprintf('%s/%s.json', $directory, substr($file, 0, -5));
$fallbackJsonFile = sprintf('%s/%s', $directory, config('importer.fallback_configuration'));

// TODO not yet sure why the distinction is necessary.
// TODO this may also be necessary for camt files.
if ('csv' === $this->getExtension($file)) {
$jsonFile = sprintf('%s/%s.json', $directory, substr($file, 0, -4));
}
// same for XML files.
if ('xml' === $this->getExtension($file)) {
$jsonFile = sprintf('%s/%s.json', $directory, substr($file, 0, -4));
}
$jsonFileExists = file_exists($jsonFile);
$hasFallbackConfig = $this->hasFallbackConfig($directory);

// Should not happen
if (!$jsonFileExists && !$hasFallbackConfig) {
$this->error(sprintf('No JSON configuration found. Checked for both "%s" and "%s"', $jsonFile, $fallbackJsonFile));
app('log')->error(sprintf('Exit code is %s.', ExitCode::CANNOT_READ_CONFIG->name));

return ExitCode::CANNOT_READ_CONFIG->value;
}

$jsonFile = $jsonFileExists ? $jsonFile : $fallbackJsonFile;

app('log')->debug(sprintf('ImportFile: importable "%s"', $importableFile));
app('log')->debug(sprintf('ImportFile: JSON "%s"', $jsonFile));

Expand Down
0