FEATURE: full asset restore from backup archive

the current restore process only restored images and not the additional
file types that are allowed, so that has been added

also tweaked the reset request to include the correct token so the
request does not fail API authorization
This commit is contained in:
ro 2024-07-09 16:26:01 -06:00
parent d51071b2d3
commit 458b076f73
No known key found for this signature in database
GPG key ID: 29B551CDBD4D3B50
4 changed files with 38 additions and 15 deletions

View file

@ -12,10 +12,14 @@ use function _\find;
class InitService
{
protected $docs;
protected $filePaths = [];
public function __construct(DocService $docService)
{
$this->docs = $docService;
$this->filePaths = ['../public/assets/images/blog',
'../public/assets/images/user', '../public/assets/video/blog',
'../public/assets/docs/blog', '../public/assets/sound/blog'];
}
private static function validSecret($length)
@ -125,6 +129,7 @@ class InitService
public function restore($request)
{
//content required, so check it
$result = [];
$contentArchive = $request->file('backup-content-upload');
$fileArchive = $request->file('backup-files-upload');
@ -135,6 +140,10 @@ class InitService
];
}
$result = $this->restoreContent($contentArchive, $request);
if ($result['type'] == 'requestLame') {
return $result;
}
//file upload is optional, so if it's present, restore it
if ($fileArchive != null || $fileArchive != '') {
$result = $this->restoreFiles($fileArchive);
@ -200,10 +209,11 @@ class InitService
} else {
$result = [
'type' => 'requestLame',
'message' => 'Could not open backup. RATS!',
'message' => 'Uh Oh. Check that handle',
];
}
};
return $result;
}
private function restoreFiles($filesUpload)
@ -212,19 +222,23 @@ class InitService
$filesZip = new \ZipArchive();
$tempDir = env('FIPAMO_DIR') . '/_file_temp';
$result = [];
//images path for blog and user
$blogImagePath = '../public/assets/images/blog';
$userImagePath = '../public/assets/images/user';
if ($filesZip->open(env('FIPAMO_DIR') . '/' . $filesUpload->getClientOriginalName()) === true) {
$filesZip->extractTo($tempDir);
//clear and move dir if present
delete_directory($blogImagePath, false);
if (is_dir($tempDir . '/public/assets/images/blog')) {
rename($tempDir . '/public/assets/images/blog', $blogImagePath);
foreach ($this->filePaths as $path) {
delete_directory($path, false);
//non image directories don't exist, so they need to be created
$pathing = explode("/", $path);
if ($pathing[3] != 'images') {
if (!is_dir('../public/assets/' . $pathing[3])) {
mkdir('../public/assets/' . $pathing[3]);
}
}
$tempPath = $tempDir . '/' . substr($path, 3);
if (is_dir($tempPath)) {
rename($tempPath, $path);
}
delete_directory($userImagePath, false);
if (is_dir($tempDir . '/public/assets/images/user')) {
rename($tempDir . '/public/assets/images/user', $userImagePath);
}
$result = [
'type' => 'requestGood',

View file

@ -44,6 +44,7 @@ class ResetService
{
delete_directory('../public/assets/docs', false);
delete_directory('../public/assets/video', false);
delete_directory('../public/assets/sound', false);
delete_directory('../public/assets/css/theme', false);
delete_directory('../public/assets/scripts/theme', false);
delete_directory('../public/assets/images/blog', false);

View file

@ -281,8 +281,14 @@ class MaintenanceManager {
}
};
if (requestType == REQUEST_TYPE_PUT || requestType == REQUEST_TYPE_POST) {
if (eventType === TASK_UPLOAD_FILES || eventType === TASK_BACKUP_CREATE)
if (
eventType === TASK_UPLOAD_FILES ||
eventType === TASK_BACKUP_CREATE ||
eventType === TASK_SITE_INIT
) {
request.setRequestHeader('fipamo-access-token', self.token);
}
switch (contentType) {
case CONTENT_TYPE_JSON:
request.setRequestHeader(

View file

@ -22,6 +22,10 @@ use App\Http\Controllers\API\MailAPIController;
//check if session is active
Route::get("/v1/status", [AuthAPIController::class, 'status']);
//site setup
Route::post("/v1/init", [InitAPIController::class, 'setupFresh']);
Route::post("/v1/restore", [InitAPIController::class, 'setupRestore']);
//handle page editing actions
Route::group(['prefix' => '/v1/page', 'middleware' => 'validate.token'], function () {
Route::put("/write", [PageAPIController::class, 'write']);
@ -45,8 +49,6 @@ Route::group(['prefix' => '/v1/backup', 'middleware' => 'validate.token'], funct
//other
Route::group(['prefix' => '/v1', 'middleware' => 'validate.token'], function () {
Route::post("/files", [FileUploadAPIController::class, 'upload']);
Route::post("/init", [InitAPIController::class, 'setupFresh']);
Route::post("/restore", [InitAPIController::class, 'setupRestore']);
Route::post("/reset", [InitAPIController::class, 'setupReset']);
Route::post("/mailer", [MailAPIController::class, 'sendNotify']);
});