From 6e9de547c074ad794b6d272eda9bf9b7d7bef792 Mon Sep 17 00:00:00 2001 From: Konstantin Vorobyev Date: Wed, 15 Feb 2017 17:38:48 +0900 Subject: Fix #3030: Add upload the sqlite database feature --- app/Controller/ConfigController.php | 26 ++++++++++++++++++++++++++ app/Model/ConfigModel.php | 14 ++++++++++++++ app/Template/config/about.php | 3 +++ app/Template/config/upload_db.php | 18 ++++++++++++++++++ 4 files changed, 61 insertions(+) create mode 100644 app/Template/config/upload_db.php diff --git a/app/Controller/ConfigController.php b/app/Controller/ConfigController.php index 8572316e..222549f4 100644 --- a/app/Controller/ConfigController.php +++ b/app/Controller/ConfigController.php @@ -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 * diff --git a/app/Model/ConfigModel.php b/app/Model/ConfigModel.php index 945c5e6f..b45199fd 100644 --- a/app/Model/ConfigModel.php +++ b/app/Model/ConfigModel.php @@ -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 * diff --git a/app/Template/config/about.php b/app/Template/config/about.php index 3f078c3d..23a3e6c0 100644 --- a/app/Template/config/about.php +++ b/app/Template/config/about.php @@ -68,6 +68,9 @@ url->link(t('Download the database'), 'ConfigController', 'downloadDb', array(), true) ?>  +
  • + url->link(t('Upload the database'), 'ConfigController', 'uploadDb', array(), false, 'js-modal-medium') ?> +
  • url->link(t('Optimize the database'), 'ConfigController', 'optimizeDb', array(), true) ?>  diff --git a/app/Template/config/upload_db.php b/app/Template/config/upload_db.php new file mode 100644 index 00000000..4d02c9a8 --- /dev/null +++ b/app/Template/config/upload_db.php @@ -0,0 +1,18 @@ + + +
    +
      +
    • +
    +
    + +
    + form->csrf() ?> + + form->label(t('Database file'), 'file') ?> + form->file('file') ?> + + modal->submitButtons(array('submitLabel' => t('Upload'))) ?> +
    -- cgit v1.2.3