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.

74 lines
2.1 KiB

3 years ago
3 years ago
  1. <?php
  2. /*
  3. * FileContentSherlock.php
  4. * Copyright (c) 2023 james@firefly-iii.org
  5. *
  6. * This file is part of the Firefly III Data Importer
  7. * (https://github.com/firefly-iii/data-importer).
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. */
  22. namespace App\Services\Shared\File;
  23. use Genkgo\Camt\Config;
  24. use Genkgo\Camt\Reader;
  25. /**
  26. * Class FileContentSherlock
  27. */
  28. class FileContentSherlock
  29. {
  30. public Reader $camtReader;
  31. public function __construct()
  32. {
  33. $this->camtReader = new Reader(Config::getDefault());
  34. }
  35. public function detectContentType(?string $file): string
  36. {
  37. if (null === $file) {
  38. return 'unknown';
  39. }
  40. try {
  41. $message = $this->camtReader->readFile($file);
  42. app('log')->debug('CAMT.053 Check on file: positive');
  43. return 'camt';
  44. } catch(\Exception $e) {
  45. app('log')->debug('CAMT.053 Check on file: negative');
  46. //app('log')->debug($e->getMessage());
  47. }
  48. return 'csv';
  49. }
  50. public function detectContentTypeFromContent(?string $content): string
  51. {
  52. if (null === $content) {
  53. return 'unknown';
  54. }
  55. try {
  56. $this->camtReader->readString($content);
  57. app('log')->debug('CAMT.053 Check of content: positive');
  58. return 'camt';
  59. } catch(\Exception $e) {
  60. app('log')->debug('CAMT.053 Check of content: negative');
  61. //app('log')->debug($e->getMessage());
  62. }
  63. return 'csv';
  64. }
  65. }