Add Backup controller

This commit is contained in:
Giuseppe Criscione 2018-09-18 22:27:44 +02:00
parent eae4460add
commit be5c1220df
4 changed files with 67 additions and 0 deletions

View File

@ -1,6 +1,11 @@
admin.manage: Manage
admin.panel: Administration Panel
admin.view-site: View Site
backup.backup: Backup
backup.error.cannot-download: Cannot download backup. %s
backup.error.cannot-download.invalid-filename: Invalid backup file.
backup.error.cannot-make: Cannot make backup. %s
backup.ready: Backup ready. Starting download...
cache.clear: Clear Cache
cache.cleared: Cache cleared
dashboard.dashboard: Dashboard

View File

@ -1,6 +1,11 @@
admin.manage: Gestione
admin.panel: Pannello di Amministrazione
admin.view-site: Visualizza sito
backup.backup: Esegui backup
backup.error.cannot-download: Impossibile scaricare il backup. %s
backup.error.cannot-download.invalid-filename: File di backup non valido.
backup.error.cannot-make: Impossibile eseguire il backup. %s
backup.ready: Backup pronto. Inizio a scaricare...
cache.clear: Svuota cache
cache.cleared: Cache svuotata
dashboard.dashboard: Riepilogo

View File

@ -165,6 +165,19 @@ class Admin
array(new Controllers\Authentication(), 'logout')
);
// Backup
$this->router->add(
'XHR',
'POST',
'/backup/make/',
array(new Controllers\Backup(), 'make')
);
$this->router->add(
'POST',
'/backup/download/{backup}/',
array(new Controllers\Backup(), 'download')
);
// Cache
$this->router->add(
'XHR',

View File

@ -0,0 +1,44 @@
<?php
namespace Formwork\Admin\Controllers;
use Formwork\Admin\Backupper;
use Formwork\Admin\Utils\JSONResponse;
use Formwork\Core\Formwork;
use Formwork\Router\RouteParams;
use Formwork\Utils\FileSystem;
use Formwork\Utils\HTTPResponse;
use RuntimeException;
class Backup extends AbstractController
{
public function make()
{
$backupper = new Backupper();
try {
$file = $backupper->backup();
$filename = FileSystem::basename($file);
JSONResponse::success($this->label('backup.ready'), 200, array(
'filename' => $filename,
'uri' => $this->uri('/backup/download/' . urlencode(base64_encode($filename)) . '/')
))->send();
} catch (RuntimeException $e) {
JSONResponse::error($this->label('backup.error.cannot-make'), 500)->send();
}
}
public function download(RouteParams $params)
{
$file = Formwork::instance()->option('backup.path') . base64_decode($params->get('backup'));
try {
if (FileSystem::exists($file) && FileSystem::isFile($file)) {
HTTPResponse::download($file);
} else {
throw new RuntimeException($this->label('backup.error.cannot-download.invalid-filename'));
}
} catch (RuntimeException $e) {
$this->notify($this->label('backup.error.cannot-download', $e->getMessage()), 'error');
$this->redirectToReferer(302, true, '/dashboard/');
}
}
}