From 391afc4d7a3874832477981499dc265da6ef264e Mon Sep 17 00:00:00 2001 From: NGPixel Date: Mon, 13 Feb 2017 00:59:52 -0500 Subject: [PATCH] Initial work for upgrade feature --- controllers/admin.js | 12 +++++++++++- libs/git.js | 2 +- libs/system.js | 42 ++++++++++++++++++++++++++++++++++++++++++ package.json | 2 ++ 4 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 libs/system.js diff --git a/controllers/admin.js b/controllers/admin.js index e0643e62..6f87ad75 100644 --- a/controllers/admin.js +++ b/controllers/admin.js @@ -6,7 +6,7 @@ const Promise = require('bluebird') const validator = require('validator') const _ = require('lodash') const axios = require('axios') -const moment = require('moment') +const path = require('path') /** * Admin @@ -234,4 +234,14 @@ router.get('/settings', (req, res) => { }) }) +router.get('/settings/install', (req, res) => { + if (!res.locals.rights.manage) { + return res.render('error-forbidden') + } + + let sysLib = require(path.join(ROOTPATH, 'libs/system.js')) + sysLib.install('v1.0-beta.5') + res.status(200).end() +}) + module.exports = router diff --git a/libs/git.js b/libs/git.js index ffb5b299..c519aae7 100644 --- a/libs/git.js +++ b/libs/git.js @@ -186,7 +186,7 @@ module.exports = { }) }) .catch((err) => { - winston.error('[' + PROCNAME + '][GIT] Unable to push changes to remote!') + winston.error('[' + PROCNAME + '.Git] Unable to push changes to remote!') throw err }) }, diff --git a/libs/system.js b/libs/system.js new file mode 100644 index 00000000..75d09b79 --- /dev/null +++ b/libs/system.js @@ -0,0 +1,42 @@ +'use strict' + +const Promise = require('bluebird') +const https = require('follow-redirects').https +const fs = Promise.promisifyAll(require('fs-extra')) +const path = require('path') +const tar = require('tar') +const zlib = require('zlib') +const _ = require('lodash') + +module.exports = { + + _remoteFile: 'https://github.com/Requarks/wiki/releases/download/{0}/wiki-js.tar.gz', + _installDir: '', + + install (targetTag) { + let self = this + + self._installDir = path.resolve(ROOTPATH, appconfig.paths.data, 'install') + + return fs.ensureDirAsync(self._installDir).then(() => { + let remoteURL = _.replace(self._remoteFile, '{0}', targetTag) + + return new Promise((resolve, reject) => { + https.get(remoteURL, resp => { + if (resp.statusCode !== 200) { + return reject(new Error('Remote file not found')) + } + + resp.pipe(zlib.createGunzip()) + .pipe(tar.Extract({ path: self._installDir })) + .on('error', err => reject(err)) + .on('end', () => { + resolve(true) + }) + }) + }).then(() => { + + }) + }) + } +} diff --git a/package.json b/package.json index 227688fb..c011d143 100644 --- a/package.json +++ b/package.json @@ -52,6 +52,7 @@ "farmhash": "^1.2.1", "file-type": "^4.0.0", "filesize.js": "^1.0.2", + "follow-redirects": "^1.2.1", "fs-extra": "^2.0.0", "git-wrapper2-promise": "^0.2.9", "highlight.js": "^9.9.0", @@ -94,6 +95,7 @@ "sticky-js": "^1.0.7", "stopword": "^0.1.1", "stream-to-promise": "^2.2.0", + "tar": "^2.2.1", "validator": "^6.2.0", "validator-as-promised": "^1.0.2", "winston": "^2.3.0"