diff --git a/app/Http/Controllers/ImportJsonController.php b/app/Http/Controllers/ImportJsonController.php index ff72e57ef72b..bfdf1cfe29ed 100644 --- a/app/Http/Controllers/ImportJsonController.php +++ b/app/Http/Controllers/ImportJsonController.php @@ -21,6 +21,7 @@ use Illuminate\Http\Response; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Str; use ZipArchive; +use Illuminate\Support\Facades\Storage; class ImportJsonController extends BaseController { @@ -62,38 +63,19 @@ class ImportJsonController extends BaseController public function import(ImportJsonRequest $request) { - $import_file = $request->file('files'); + $file_location = $request->file('files') + ->storeAs( + 'migrations', + $request->file('files')->getClientOriginalName() + ); - $file_location = $this->unzipFile($import_file->getPathname()); - - // if(Ninja::isHosted()) - // CompanyImport::dispatch(auth()->user()->getCompany(), auth()->user(), $file_location, $request->except('files'))->onQueue('himem'); - // else - CompanyImport::dispatch(auth()->user()->getCompany(), auth()->user(), $file_location, $request->except('files')); + if(Ninja::isHosted()) + CompanyImport::dispatch(auth()->user()->getCompany(), auth()->user(), $file_location, $request->except('files'))->onQueue('migration'); + else + CompanyImport::dispatch(auth()->user()->getCompany(), auth()->user(), $file_location, $request->except('files')); return response()->json(['message' => 'Processing'], 200); } - private function unzipFile($file_contents) - { - $zip = new ZipArchive(); - $archive = $zip->open($file_contents); - - $filename = pathinfo($file_contents, PATHINFO_FILENAME); - $zip->extractTo(public_path("storage/backups/{$filename}")); - $zip->close(); - $file_location = public_path("storage/backups/$filename/backup.json"); - - if (! file_exists($file_location)) - throw new NonExistingMigrationFile('Backup file does not exist, or is corrupted.'); - - //$data = file_get_contents($file_location); - - return $file_location; - //unlink($file_contents); - //unlink($file_location); - - //return $data; - } } diff --git a/app/Http/Controllers/MigrationController.php b/app/Http/Controllers/MigrationController.php index 5e47e7a6dfc8..370dd080d403 100644 --- a/app/Http/Controllers/MigrationController.php +++ b/app/Http/Controllers/MigrationController.php @@ -387,7 +387,6 @@ class MigrationController extends BaseController else StartMigration::dispatch($migration_file, $user, $fresh_company); - } } diff --git a/app/Jobs/Company/CompanyImport.php b/app/Jobs/Company/CompanyImport.php index c07ade77c403..2d9cdbed8d79 100644 --- a/app/Jobs/Company/CompanyImport.php +++ b/app/Jobs/Company/CompanyImport.php @@ -56,6 +56,7 @@ use App\Models\Vendor; use App\Models\VendorContact; use App\Models\Webhook; use App\Utils\Ninja; +use App\Utils\TempFile; use App\Utils\Traits\MakesHash; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; @@ -171,7 +172,10 @@ class CompanyImport implements ShouldQueue if ( empty( $this->file_location ) ) throw new \Exception('No import data found, has the cache expired?'); - $this->backup_file = json_decode(file_get_contents($this->file_location)); + // $this->backup_file = json_decode(file_get_contents($this->file_location)); + $tmp_file = $this->unzipFile(); + + $this->backup_file = json_decode(file_get_contents($tmp_file)); // nlog($this->backup_file); $this->checkUserCount(); @@ -198,10 +202,31 @@ class CompanyImport implements ShouldQueue } - unlink($this->file_location); + unlink($tmp_file); } + private function unzipFile() + { + $path = TempFile::filePath(Storage::get($this->file_location), basename($this->file_location)); + + $zip = new ZipArchive(); + $archive = $zip->open($path); + + $file_path = sys_get_temp_dir().'/'.sha1(microtime()); + + $zip->extractTo($file_path); + $zip->close(); + $file_location = "{$file_path}/backup.json"; + + if (! file_exists($file_location)) + throw new NonExistingMigrationFile('Backup file does not exist, or is corrupted.'); + + return $file_location; + + } + + /** * On the hosted platform we cannot allow the * import to start if there are users > plan number @@ -360,7 +385,7 @@ class CompanyImport implements ShouldQueue foreach($this->importables as $import){ - $method = "import_{$import}"; + $method = "import_{$import} "; nlog($method); @@ -1248,6 +1273,11 @@ class CompanyImport implements ShouldQueue if (! array_key_exists("{$old}", $this->ids[$resource])) { // nlog($this->ids[$resource]); nlog("searching for {$old} in {$resource}"); + + nlog("If we are missing a user - default to the company owner") + if($resource == 'users') + return $this->company_owner->id; + throw new \Exception("Missing {$resource} key: {$old}"); } diff --git a/app/Jobs/Util/StartMigration.php b/app/Jobs/Util/StartMigration.php index 36cbe4798449..d3c13c5995c7 100644 --- a/app/Jobs/Util/StartMigration.php +++ b/app/Jobs/Util/StartMigration.php @@ -92,16 +92,6 @@ class StartMigration implements ShouldQueue $archive = $zip->open(public_path("storage/{$this->filepath}")); $filename = pathinfo($this->filepath, PATHINFO_FILENAME); - // if($this->company->id == $this->company->account->default_company_id) - // { - // $new_default_company = $this->company->account->companies->first(); - - // if ($new_default_company) { - // $this->company->account->default_company_id = $new_default_company->id; - // $this->company->account->save(); - // } - // } - $update_product_flag = $this->company->update_products; $this->company->update_products = false; @@ -129,9 +119,6 @@ class StartMigration implements ShouldQueue Storage::deleteDirectory(public_path("storage/migrations/{$filename}")); - // $this->company->account->default_company_id = $this->company->id; - // $this->company->account->save(); - $this->company->update_products = $update_product_flag; $this->company->save();