From 234453b90069273cc7e444e6d395120a775a4e30 Mon Sep 17 00:00:00 2001 From: ro Date: Mon, 22 Apr 2024 13:11:51 -0600 Subject: [PATCH] update site back up and restore process the back up process did not include saving document and video assets so that needed to be updated to save all of those references along with images once that was done, the restore process had to be updated to look for docs and videos and create the corresponding directories to move them too the script responsible for moving those assets was also streamlined so a seperate process was not needed for each asset type. the last update date in the settings template was displaying incorrectly, so that was fixed as well. --- app/Services/InitService.php | 89 ++++++++++++----------------- app/Services/MaintenanceService.php | 39 ++++++++++--- app/Services/SortingService.php | 2 +- 3 files changed, 70 insertions(+), 60 deletions(-) diff --git a/app/Services/InitService.php b/app/Services/InitService.php index ef06930..21dbf4a 100644 --- a/app/Services/InitService.php +++ b/app/Services/InitService.php @@ -121,62 +121,17 @@ class InitService public function restore($request) { $file = $request->file('backup-upload'); - $type = $file->extension(); - $size = $file->getSize(); - $name = $file->getClientOriginalName(); - $file->move('../content' . '/', $name); + $file->move('../content' . '/', $file->getClientOriginalName()); $zip = new \ZipArchive(); $result = []; $tempDir = '../content/_temp'; - if ($zip->open('../content' . '/' . $name) === true) { + if ($zip->open('../content' . '/' . $file->getClientOriginalName()) === 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++; - } - } + //restore assets from previous site + $this->moveAssets($zip, $request->restore_former_url); $newFolks = []; if (!isset($found['secret'])) { $found['secret'] = self::validSecret(12); @@ -206,8 +161,7 @@ class InitService //clean up temp dir and zip file $this->docs::deleteFolder($tempDir); $zip->close(); - $zipPath = '../content/' . $name; - unlink($zipPath); + unlink('../content/' . $file->getClientOriginalName()); $result = [ 'type' => 'requestGood', 'message' => 'Site Restored! Redirecting', @@ -227,4 +181,37 @@ class InitService }; return $result; } + + private function moveAssets($zip, $url) + { + $assetFail = 0; + $assetList = []; + array_push($assetList, json_decode($zip->getFromName('assets/blog_images.json'), true)); + array_push($assetList, json_decode($zip->getFromName('assets/user_images.json'), true)); + array_push($assetList, json_decode($zip->getFromName('assets/blog_docs.json'), true)); + array_push($assetList, json_decode($zip->getFromName('assets/blog_videos.json'), true)); + foreach ($assetList as $list) { + foreach ($list as $asset) { + $path = explode('/', $asset['path']); + $type = $path[3]; + $section = $path[4]; + $year = $path[5]; + $month = $path[6]; + $blogDir = '../public/assets/' . $type . '/' . $section . '/' . $year . '/' . $month; + if (!is_dir($blogDir)) { + mkdir($blogDir, 0755, true); + } + $externalPath = '/assets/' . $type . '/' . $section . '/' . $year . '/' . $month; + $asset_url = $url . $externalPath . '/' . $asset['file']; + try { + file_put_contents( + $asset['path'] . '/' . $asset['file'], + file_get_contents($asset_url) + ); + } catch (\Throwable $e) { + $assetFail++; + } + } + } + } } diff --git a/app/Services/MaintenanceService.php b/app/Services/MaintenanceService.php index 0297579..0a93145 100644 --- a/app/Services/MaintenanceService.php +++ b/app/Services/MaintenanceService.php @@ -51,7 +51,7 @@ class MaintenanceService $zip->addGlob($months . '/*.md', GLOB_BRACE, $options); } } - + //TODO: add collectors for docs and vids //gather paths for blog images $blogImages = []; $dir = new \RecursiveDirectoryIterator('../public/assets/images/blog'); @@ -70,23 +70,46 @@ class MaintenanceService $userImages[] = ['path' => $file->getPath(), 'file' => $file->getFilename()]; }; + //gather paths for blog documents + $blogDocs = []; + $dir = new \RecursiveDirectoryIterator('../public/assets/docs/blog'); + $flat = new \RecursiveIteratorIterator($dir); + $files = new \RegexIterator($flat, '/\.txt|pdf|rtf$/i'); + foreach ($files as $file) { + $blogDocs[] = ['path' => $file->getPath(), 'file' => $file->getFilename()]; + }; + //gather paths for blog videos + $blogVids = []; + $dir = new \RecursiveDirectoryIterator('../public/assets/video/blog'); + $flat = new \RecursiveIteratorIterator($dir); + $files = new \RegexIterator($flat, '/\.mp4$/i'); + foreach ($files as $file) { + $blogVids[] = ['path' => $file->getPath(), 'file' => $file->getFilename()]; + }; + //add directory for settings and save them $zip->addEmptyDir('config'); - $zip->addEmptyDir('images'); + $zip->addEmptyDir('assets'); $zip->addFile(env('SETTINGS_PATH'), 'config/settings.json'); $zip->addFile(env('FOLKS_PATH'), 'config/folks.json'); $zip->addFile(env('TAGS_PATH'), 'config/tags.json'); // create temp files for image lists - file_put_contents('../content/backups/blog_temp.json', json_encode($blogImages)); - file_put_contents('../content/backups/user_temp.json', json_encode($userImages)); + file_put_contents('../content/backups/blog_images_temp.json', json_encode($blogImages)); + file_put_contents('../content/backups/user_images_temp.json', json_encode($userImages)); + file_put_contents('../content/backups/blog_docs_temp.json', json_encode($blogDocs)); + file_put_contents('../content/backups/blog_vids_temp.json', json_encode($blogVids)); //add to zip - $zip->addFile('../content/backups/blog_temp.json', 'images/blog.json'); - $zip->addFile('../content/backups/user_temp.json', 'images/user.json'); + $zip->addFile('../content/backups/blog_images_temp.json', 'assets/blog_images.json'); + $zip->addFile('../content/backups/user_images_temp.json', 'assets/user_images.json'); + $zip->addFile('../content/backups/blog_docs_temp.json', 'assets/blog_docs.json'); + $zip->addFile('../content/backups/blog_vids_temp.json', 'assets/blog_videos.json'); //save zip file $zip->close(); //clean up temp files - unlink('../content/backups/blog_temp.json'); - unlink('../content/backups/user_temp.json'); + unlink('../content/backups/blog_images_temp.json'); + unlink('../content/backups/user_images_temp.json'); + unlink('../content/backups/blog_docs_temp.json'); + unlink('../content/backups/blog_vids_temp.json'); //update settings file with latest back up date $this->settings->updateGlobalData('last_backup', $stamp); diff --git a/app/Services/SortingService.php b/app/Services/SortingService.php index c2d35a7..19ef8c0 100644 --- a/app/Services/SortingService.php +++ b/app/Services/SortingService.php @@ -274,7 +274,7 @@ class SortingService 'siteTitle' => $global['title'], 'baseUrl' => $global['base_url'], 'desc' => $global['descriptions'], - 'lastBackup' => $updated->format('Y M D G i s'), + 'lastBackup' => $updated->format('Y F d H i s'), 'currentTheme' => $global['theme'], 'themes' => $this->themes->getThemes(), 'apiStatus' => isset($global['externalAPI']) ? $global['externalAPI'] : 'false',