You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.2 KiB

  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Console\AutoImports;
  4. use App\Console\HaveAccess;
  5. use App\Console\StartImport;
  6. use App\Console\VerifyJSON;
  7. use App\Exceptions\ImportException;
  8. use Illuminate\Http\Request;
  9. use Log;
  10. /**
  11. *
  12. */
  13. class AutoImportController extends Controller
  14. {
  15. use HaveAccess, AutoImports, VerifyJSON, StartImport;
  16. private string $directory;
  17. /**
  18. *
  19. */
  20. public function index(Request $request)
  21. {
  22. $access = $this->haveAccess();
  23. if (false === $access) {
  24. throw new ImportException('Could not connect to your local Firefly III instance.');
  25. }
  26. $argument = (string) ($request->get('directory') ?? './');
  27. $this->directory = realpath($argument);
  28. $this->line(sprintf('Going to automatically import everything found in %s (%s)', $this->directory, $argument));
  29. $files = $this->getFiles();
  30. if (0 === count($files)) {
  31. $this->line(sprintf('There are no files in directory %s', $this->directory));
  32. $this->line('To learn more about this process, read the docs:');
  33. $this->line('https://docs.firefly-iii.org/csv/install/docker/');
  34. return ' ';
  35. }
  36. $this->line(sprintf('Found %d CSV + JSON file sets in %s', count($files), $this->directory));
  37. try {
  38. $this->importFiles($files);
  39. } catch (ImportException $e) {
  40. Log::error($e->getMessage());
  41. $this->line(sprintf('Import exception (see the logs): %s', $e->getMessage()));
  42. }
  43. return ' ';
  44. }
  45. /**
  46. * @inheritDoc
  47. */
  48. public function line(string $string)
  49. {
  50. echo sprintf("%s: %s\n", date('Y-m-d H:i:s'), $string);
  51. }
  52. /**
  53. * @inheritDoc
  54. */
  55. public function error($string, $verbosity = null)
  56. {
  57. $this->line($string);
  58. }
  59. /**
  60. * @param $string
  61. * @param null $verbosity
  62. */
  63. public function warn($string, $verbosity = null)
  64. {
  65. $this->line($string);
  66. }
  67. /**
  68. * @param $string
  69. * @param null $verbosity
  70. */
  71. public function info($string, $verbosity = null)
  72. {
  73. $this->line($string);
  74. }
  75. }