0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-27 22:49:56 -05:00
ghost/core/server/themes/Storage.js
Hannah Wolfe bcf5a1bc34
Switch to Eslint (#9197)
refs #9178

* Add eslint deps, remove old lint deps
* Add eslint config, remove old lint configs
* Config for server and tests are different
* Tweaked rules to suit us
* Fix linting in codebase - lots of indent changes.
* Fix a real broken test
2017-11-01 13:44:54 +00:00

66 lines
2 KiB
JavaScript

'use strict';
var fs = require('fs-extra'),
os = require('os'),
path = require('path'),
Promise = require('bluebird'),
config = require('../config'),
utils = require('../utils'),
LocalFileStorage = require('../adapters/storage/LocalFileStorage'),
remove = Promise.promisify(fs.remove);
/**
* @TODO: combine with loader.js?
*/
class ThemeStorage extends LocalFileStorage {
constructor() {
super();
this.storagePath = config.getContentPath('themes');
}
getTargetDir() {
return this.storagePath;
}
serve(options) {
var self = this;
return function downloadTheme(req, res, next) {
var themeName = options.name,
themePath = path.join(self.storagePath, themeName),
zipName = themeName + '.zip',
// store this in a unique temporary folder
zipBasePath = path.join(os.tmpdir(), utils.uid(10)),
zipPath = path.join(zipBasePath, zipName),
stream;
Promise.promisify(fs.ensureDir)(zipBasePath)
.then(function () {
return Promise.promisify(utils.zipFolder)(themePath, zipPath);
})
.then(function (length) {
res.set({
'Content-disposition': 'attachment; filename={themeName}.zip'.replace('{themeName}', themeName),
'Content-Type': 'application/zip',
'Content-Length': length
});
stream = fs.createReadStream(zipPath);
stream.pipe(res);
})
.catch(function (err) {
next(err);
})
.finally(function () {
remove(zipBasePath);
});
};
}
delete(fileName) {
return remove(path.join(this.storagePath, fileName));
}
}
module.exports = ThemeStorage;