diff --git a/app/Http/Controllers/ImportController.php b/app/Http/Controllers/ImportController.php index 80577479521a..584bfb90001a 100644 --- a/app/Http/Controllers/ImportController.php +++ b/app/Http/Controllers/ImportController.php @@ -22,6 +22,10 @@ class ImportController extends BaseController public function doImport(Request $request) { + if (! Auth::user()->confirmed) { + return redirect('/settings/' . ACCOUNT_IMPORT_EXPORT)->withError(trans('texts.confirm_account_to_import')); + } + $source = Input::get('source'); $files = []; $timestamp = time(); diff --git a/app/Services/ImportService.php b/app/Services/ImportService.php index 62964a9d2c5a..5e79643adcff 100644 --- a/app/Services/ImportService.php +++ b/app/Services/ImportService.php @@ -148,8 +148,9 @@ class ImportService public function importJSON($file, $includeData, $includeSettings) { $this->initMaps(); - - $file = file_get_contents(storage_path() . '/import/' . $file); + $fileName = storage_path() . '/import/' . $file; + $this->checkForFile($fileName); + $file = file_get_contents($fileName); $json = json_decode($file, true); $json = $this->removeIdFields($json); $transformer = new BaseTransformer($this->maps); @@ -166,7 +167,7 @@ class ImportService $settings[$field] = $value; } } - //dd($settings); + $account = Auth::user()->account; $account->fill($settings); $account->save(); @@ -228,6 +229,8 @@ class ImportService } } + unlink($fileName); + return $this->results; } @@ -284,9 +287,10 @@ class ImportService // Convert the data $row_list = []; - $file = storage_path() . '/import/' . $file; + $fileName = storage_path() . '/import/' . $file; + $this->checkForFile($fileName); - Excel::load($file, function ($reader) use ($source, $entityType, &$row_list, &$results) { + Excel::load($fileName, function ($reader) use ($source, $entityType, &$row_list, &$results) { $this->checkData($entityType, count($reader->all())); $reader->each(function ($row) use ($source, $entityType, &$row_list, &$results) { @@ -317,6 +321,8 @@ class ImportService } } + unlink($fileName); + return $results; } @@ -568,8 +574,6 @@ class ImportService } } - //Session::put("{$entityType}-data", $csv->data); - $data = [ 'entityType' => $entityType, 'data' => $data, @@ -582,12 +586,16 @@ class ImportService return $data; } - private function getCsvData($filename) + private function getCsvData($fileName) { require_once app_path().'/Includes/parsecsv.lib.php'; + + $fileName = storage_path() . '/import/' . $fileName; + $this->checkForFile($fileName); + $csv = new parseCSV(); $csv->heading = false; - $csv->auto(storage_path() . '/import/' . $filename); + $csv->auto($fileName); $data = $csv->data; if (count($data) > 0) { @@ -678,9 +686,8 @@ class ImportService ]; $source = IMPORT_CSV; - //$data = Session::get("{$entityType}-data"); - $filename = sprintf('%s_%s_%s.csv', Auth::user()->account_id, $timestamp, $entityType); - $data = $this->getCsvData($filename); + $fileName = sprintf('%s_%s_%s.csv', Auth::user()->account_id, $timestamp, $entityType); + $data = $this->getCsvData($fileName); $this->checkData($entityType, count($data)); $this->initMaps(); @@ -719,7 +726,7 @@ class ImportService } } - //Session::forget("{$entityType}-data"); + unlink($fileName); return $results; } @@ -934,4 +941,19 @@ class ImportService return $message; } + + private function checkForFile($fileName) + { + $counter = 0; + + while (! file_exists($fileName)) { + $counter++; + if ($counter > 60) { + throw new Exception('File not found: ' . $fileName); + } + sleep(2); + } + + return true; + } } diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index b978b170381c..bd25e077eefa 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -2461,7 +2461,7 @@ $LANG = array( 'reply_to_email_help' => 'Specify the reply-to address for client emails.', 'bcc_email_help' => 'Privately include this address with client emails.', 'import_complete' => 'Your import has successfully completed.', - + 'confirm_account_to_import' => 'Please confirm your account to import data.', );