3131use App \Console \VerifyJSON ;
3232use App \Exceptions \ImporterErrorException ;
3333use Illuminate \Http \Request ;
34+ use Illuminate \Http \Response ;
3435use Log ;
3536
3637/**
@@ -45,40 +46,45 @@ class AutoImportController extends Controller
4546 /**
4647 *
4748 */
48- public function index (Request $ request )
49+ public function index (Request $ request ): Response
4950 {
50- die ('todo ' . __METHOD__ );
51+ if (false === config ('importer.can_post_autoimport ' )) {
52+ throw new ImporterErrorException ('Disabled, not allowed to import. ' );
53+ }
54+
55+ $ secret = (string ) ($ request ->get ('secret ' ) ?? '' );
56+ $ systemSecret = (string ) config ('importer.auto_import_secret ' );
57+ if ('' === $ secret || '' === $ systemSecret || $ secret !== config ('importer.auto_import_secret ' ) || strlen ($ systemSecret ) < 16 ) {
58+ throw new ImporterErrorException ('Bad secret, not allowed to import. ' );
59+ }
60+
61+ $ argument = (string ) ($ request ->get ('directory ' ) ?? './ ' );
62+ $ directory = realpath ($ argument );
63+
64+ if (!$ this ->isAllowedPath ($ directory )) {
65+ throw new ImporterErrorException ('Not allowed to import from this path. ' );
66+ }
67+
5168 $ access = $ this ->haveAccess ();
5269 if (false === $ access ) {
5370 throw new ImporterErrorException ('Could not connect to your local Firefly III instance. ' );
5471 }
5572
56- $ argument = (string ) ($ request ->get ('directory ' ) ?? './ ' );
57- $ this ->directory = realpath ($ argument );
58- $ this ->line (sprintf ('Going to automatically import everything found in %s (%s) ' , $ this ->directory , $ argument ));
73+ // take code from auto importer.
74+ app ('log ' )->info (sprintf ('Going to automatically import everything found in %s (%s) ' , $ directory , $ argument ));
5975
60- $ files = $ this ->getFiles ();
76+ $ files = $ this ->getFiles ($ directory );
6177 if (0 === count ($ files )) {
62- $ this ->line (sprintf ('There are no files in directory %s ' , $ this ->directory ));
63- $ this ->line ('To learn more about this process, read the docs: ' );
64- $ this ->line ('https://docs.firefly-iii.org/data-importer/ ' );
65-
66- return ' ' ;
78+ return response ('' );
6779 }
68- $ this -> line (sprintf ('Found %d CSV + JSON file sets in %s ' , count ($ files ), $ this -> directory ));
80+ app ( ' log ' )-> info (sprintf ('Found %d ( CSV +) JSON file sets in %s ' , count ($ files ), $ directory ));
6981 try {
70- $ this ->importFiles ($ files );
82+ $ this ->importFiles ($ directory , $ files );
7183 } catch (ImporterErrorException $ e ) {
72- Log:: error ($ e ->getMessage ());
73- $ this -> line (sprintf ('Import exception (see the logs): %s ' , $ e ->getMessage ()));
84+ app ( ' log ' )-> error ($ e ->getMessage ());
85+ throw new ImporterErrorException (sprintf ('Import exception (see the logs): %s ' , $ e ->getMessage ()));
7486 }
75-
76- return ' ' ;
77- }
78-
79- public function line (string $ string )
80- {
81- echo sprintf ("%s: %s \n" , date ('Y-m-d H:i:s ' ), $ string );
87+ return response ('' );
8288 }
8389
8490 /**
@@ -89,6 +95,11 @@ public function error($string, $verbosity = null)
8995 $ this ->line ($ string );
9096 }
9197
98+ public function line (string $ string )
99+ {
100+ echo sprintf ("%s: %s \n" , date ('Y-m-d H:i:s ' ), $ string );
101+ }
102+
92103 /**
93104 * @param $string
94105 * @param null $verbosity
0 commit comments