Fix #3030: Add upload the sqlite database feature

This commit is contained in:
Konstantin Vorobyev 2017-02-15 17:38:48 +09:00
parent 5376bb9e0c
commit 6e9de547c0
4 changed files with 61 additions and 0 deletions

View File

@ -200,6 +200,32 @@ class ConfigController extends BaseController
$this->response->redirect($this->helper->url->to('ConfigController', 'index'));
}
/**
* Display the Sqlite database upload page
*
* @access public
*/
public function uploadDb()
{
$this->response->html($this->helper->layout->config('config/upload_db', array()));
}
/**
* Replace current Sqlite db with uploaded file
*
* @access public
*/
public function uploadDbSave()
{
$filename = $this->request->getFilePath('file');
if (!file_exists($filename) || !$this->configModel->uploadDatabase($filename)) {
$this->flash->failure(t('Unable to read your file'));
} else {
$this->flash->success(t('Database upload done.'));
}
$this->response->redirect($this->helper->url->to('ConfigController', 'index'));
}
/**
* Regenerate webhook token
*

View File

@ -48,6 +48,20 @@ class ConfigModel extends SettingModel
return gzencode(file_get_contents(DB_FILENAME));
}
/**
* Replace database file with uploaded one
*
* @access public
* @return boolean
*/
public function uploadDatabase($file)
{
$this->db->closeConnection();
$result = file_put_contents(DB_FILENAME, gzdecode(file_get_contents($file)));
return $result == false? false: true;
}
/**
* Get the Sqlite database size in bytes
*

View File

@ -68,6 +68,9 @@
<?= $this->url->link(t('Download the database'), 'ConfigController', 'downloadDb', array(), true) ?>&nbsp;
<?= t('(Gzip compressed Sqlite file)') ?>
</li>
<li>
<?= $this->url->link(t('Upload the database'), 'ConfigController', 'uploadDb', array(), false, 'js-modal-medium') ?>
</li>
<li>
<?= $this->url->link(t('Optimize the database'), 'ConfigController', 'optimizeDb', array(), true) ?>&nbsp;
<?= t('(VACUUM command)') ?>

View File

@ -0,0 +1,18 @@
<div class="page-header">
<h2><?= t('Upload the Sqlite database') ?></h2>
</div>
<div class="alert">
<ul>
<li><?= t('You can upload Gzip compressed Sqlite database you previously downloaded') ?></li>
</ul>
</div>
<form action="<?= $this->url->href('ConfigController', 'uploadDbSave') ?>" method="post" enctype="multipart/form-data">
<?= $this->form->csrf() ?>
<?= $this->form->label(t('Database file'), 'file') ?>
<?= $this->form->file('file') ?>
<?= $this->modal->submitButtons(array('submitLabel' => t('Upload'))) ?>
</form>