Replaced Moment with Carbon #84

Merged
Ghost merged 148 commits from develop into beta 2022-09-22 05:53:36 +02:00
4 changed files with 122 additions and 1 deletions
Showing only changes of commit b2493820e9 - Show all commits

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