Ghost/core/frontend/services/themes/ThemeStorage.js
Naz Gargol f3ec2fb2f7
Cleaned up theme service (#10884)
refs #10790

- Following TODO in theme index file was waiting for 2 years, and today is the day to cross it out:
- "Reduced the amount of things we expose to the outside world"
- "Made this a nice clean sensible API we can all understand!" - by @ErisDS
- Cleaned exposed methods from themes module
- Removed unused storage getter
- Removed list method
- Removed validate method
- Renamed Storage to ThemeStorage
  - Named the file the same way the class defined inside of it is named
  - Naming was conflicting with coming rename of  `settings` -> `storage`
- Renamed theme settings to storage
2019-07-09 16:35:18 +02:00

65 lines
2.0 KiB
JavaScript

var fs = require('fs-extra'),
os = require('os'),
path = require('path'),
Promise = require('bluebird'),
config = require('../../../server/config'),
security = require('../../../server/lib/security'),
fsLib = require('../../../server/lib/fs'),
LocalFileStorage = require('../../../server/adapters/storage/LocalFileStorage');
/**
* @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(), security.identifier.uid(10)),
zipPath = path.join(zipBasePath, zipName),
stream;
fs.ensureDir(zipBasePath)
.then(function () {
return Promise.promisify(fsLib.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 () {
return fs.remove(zipBasePath);
});
};
}
delete(fileName) {
return fs.remove(path.join(this.storagePath, fileName));
}
}
module.exports = ThemeStorage;