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
This commit is contained in:
ro 2024-04-18 14:36:05 -06:00
parent 2f0e1fdc62
commit b2493820e9
No known key found for this signature in database
GPG key ID: 29B551CDBD4D3B50
4 changed files with 122 additions and 1 deletions

View file

@ -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');
}
}

View file

@ -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;
}
}

View file

@ -4,7 +4,9 @@
</a>
</div>
<form id="init-restore" method="POST">
<input type="text" name="restore_member_handle" id="restore_member_handle" placeholder="handle"/><input type="password" name="restore_member_pass" id="restore_member_pass" placeholder="password"/>
<input type="text" name="restore_former_url" id="restore_former_url" placeholder="previous site url"/>
<input type="text" name="restore_member_handle" id="restore_member_handle" placeholder="handle"/>
<input type="password" name="restore_member_pass" id="restore_member_pass" placeholder="password"/>
<div>
<label>Grab your backup zip</label>
<input id="backup-upload" type="file" name="backup-upload" placeholder="Backup Zip"/>

View file

@ -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']);