From b2493820e90fa22c366b91c396934c45f6db405b Mon Sep 17 00:00:00 2001 From: ro Date: Thu, 18 Apr 2024 14:36:05 -0600 Subject: [PATCH] activated image transfer for restore process rather than make a massive downloadable archive file for ever image on the site (which still may happen), a method has been added to make copies of imags from an external site and store them on the fresh install based on the image list saved in the created back up file it's clean but some additional error checking is needed so the process does not crash out when a file can't be located and upon completion the user can be notified of what images did not make it over in the process --- .../Controllers/API/InitAPIController.php | 6 + app/Services/InitService.php | 112 ++++++++++++++++++ resources/views/forms/init-restore.blade.php | 4 +- routes/api.php | 1 + 4 files changed, 122 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/API/InitAPIController.php b/app/Http/Controllers/API/InitAPIController.php index 4cc1059..29f4dc2 100644 --- a/app/Http/Controllers/API/InitAPIController.php +++ b/app/Http/Controllers/API/InitAPIController.php @@ -21,4 +21,10 @@ class InitAPIController extends Controller $result = $this->init->fresh(json_decode($request->getContent())); return response()->json($result)->header('Content-Type', 'application/json'); } + + public function setupRestore(Request $request) + { + $result = $this->init->restore($request); + return response()->json($result)->header('Content-Type', 'application/json'); + } } diff --git a/app/Services/InitService.php b/app/Services/InitService.php index 7234ed9..ef06930 100644 --- a/app/Services/InitService.php +++ b/app/Services/InitService.php @@ -6,6 +6,8 @@ use ReallySimpleJWT\Token; use ReallySimpleJWT\Exception\BuildException; use Carbon\Carbon; +use function _\find; + class InitService { protected $docs; @@ -115,4 +117,114 @@ class InitService return $result; } + + public function restore($request) + { + $file = $request->file('backup-upload'); + $type = $file->extension(); + $size = $file->getSize(); + $name = $file->getClientOriginalName(); + $file->move('../content' . '/', $name); + $zip = new \ZipArchive(); + $result = []; + $tempDir = '../content/_temp'; + if ($zip->open('../content' . '/' . $name) === true) { + $folks = json_decode($zip->getFromName('config/folks.json'), true); + $found = find($folks, ['handle' => $request->restore_member_handle]); + if ($found) { + if (password_verify($request->restore_member_pass, $found['password'])) { + //restore blog images by importing from old site + $blogImages = json_decode($zip->getFromName('images/blog.json'), true); + $blogImageFail = 0; + foreach ($blogImages as $image) { + $path = explode('/', $image['path']); + $year = $path[5]; + $month = $path[6]; + $blogDir = '../public/assets/images/blog/' . $year . '/' . $month; + if (!is_dir($blogDir)) { + mkdir($blogDir, 0755, true); + } + $externalPath = '/assets/images/blog/' . $year . '/' . $month; + $image_url = $request->restore_former_url . $externalPath . '/' . $image['file']; + try { + file_put_contents( + $image['path'] . '/' . $image['file'], + file_get_contents($image_url) + ); + } catch (\Throwable $e) { + $blogImageFail++; + } + } + //restore user images by importing from old site + $userImages = json_decode($zip->getFromName('images/user.json'), true); + $userImageFail = 0; + foreach ($userImages as $image) { + $path = explode('/', $image['path']); + $year = $path[5]; + $month = $path[6]; + $userDir = '../public/assets/images/user/' . $year . '/' . $month; + if (!is_dir($userDir)) { + mkdir($userDir, 0755, true); + } + $externalPath = '/assets/images/user/' . $year . '/' . $month; + $image_url = $request->restore_former_url . $externalPath . '/' . $image['file']; + try { + file_put_contents( + $image['path'] . '/' . $image['file'], + file_get_contents($image_url) + ); + } catch (\Throwable $e) { + $userImageFail++; + } + } + $newFolks = []; + if (!isset($found['secret'])) { + $found['secret'] = self::validSecret(12); + } + array_push($newFolks, $found); + //make temp folder and dump file in there + mkdir($tempDir, 0755, true); + $zip->extractTo($tempDir); + //load up old config file + $newConfig = json_decode( + file_get_contents($tempDir . '/config/settings.json'), + true + ); + //check for key, add if not there + if (!isset($newConfig['global']['externalAPI'])) { + $newConfig['global']['externalAPI'] = 'false'; + } + //make dir and write new config files + if (!is_dir('../content/config/')) { + mkdir('../content/config/', 0755, true); + } + $this->docs->writeSettings($newConfig, '../content/config/settings.json'); + $this->docs->writeSettings($newFolks, '../content/config/folks.json'); + rename($tempDir . '/config/tags.json', '../content/config/tags.json'); + //move saved markdown pages + rename($tempDir . '/content/pages/', '../content/pages'); + //clean up temp dir and zip file + $this->docs::deleteFolder($tempDir); + $zip->close(); + $zipPath = '../content/' . $name; + unlink($zipPath); + $result = [ + 'type' => 'requestGood', + 'message' => 'Site Restored! Redirecting', + ]; + } else { + $result = [ + 'type' => 'requestLame', + 'message' => 'Check that password, champ.', + ]; + } + } else { + $result = [ + 'type' => 'requestLame', + 'message' => 'Could not open backup. RATS!', + ]; + } + }; + return $result; + } } diff --git a/resources/views/forms/init-restore.blade.php b/resources/views/forms/init-restore.blade.php index 555bd14..16df2f1 100644 --- a/resources/views/forms/init-restore.blade.php +++ b/resources/views/forms/init-restore.blade.php @@ -4,7 +4,9 @@
- + + +
diff --git a/routes/api.php b/routes/api.php index 9ddc914..d79a31a 100644 --- a/routes/api.php +++ b/routes/api.php @@ -33,3 +33,4 @@ Route::put("/v1/backup/create", [SettingsAPIController::class, 'createBackup']); Route::get("/v1/backup/download", [SettingsAPIController::class, 'downloadBackup']); //init Route::post("/v1/init", [InitAPIController::class, 'setupFresh']); +Route::post("/v1/restore", [InitAPIController::class, 'setupRestore']);