mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-24 19:33:02 +03:00
817b8d09ca
refs #7687 There are four main changes in this PR: we have outsourced the base storage adapter to npm, because for storage developers it's annoying to inherit from a script within Ghost we hacked theme storage handling into the default local storage adapter - this was reverted, instead we have added a static theme storage here use classes instead of prototyping optimise the storage adapter in general - everything is explained in each commit ---- * rename local-file-store to LocalFileStorage I would like to keep the name pattern i have used for scheduling. If a file is a class, the file name reflects the class name. We can discuss this, if concerns are raised. * Transform LocalFileStorage to class and inherit from new base - inherit from npm ghost-storage-base - rewrite to class - no further refactoring, happens later * Rename core/test/unit/storage/local-file-store_spec.js -> core/test/unit/storage/LocalFileStorage_spec.js * Fix wrong require in core/test/unit/storage/LocalFileStorage_spec.js * remove base storage and test - see https://github.com/kirrg001/Ghost-Storage-Base - the test has moved to this repo as well * Use npm ghost-storage-base in storage/index.js * remove the concept of getStorage('themes') This concept was added when we added themes as a feature. Back then, we have changed the local storage adapter to support images and themes. This has added some hacks into the local storage adapters. We want to revert this change and add a simple static theme storage. Will adapt the api/themes layer in the next commits. * Revert LocalFileStorage - revert serve - revert delete * add storagePath as property to LocalFileStorage - define one property which holds the storage path - could be considered to pass from outside, but found that not helpful, as other storage adapters do not need this property - IMPORTANT: save has no longer a targetDir option, because this was used to pass the alternative theme storage path - IMPORTANT: exists has now an alternative targetDir, this makes sense, because - you can either ask the storage exists('my-file') and it will look in the base storage path - or you pass a specific path where to look exists('my-file', /path/to/dir) * LocalFileStorage: get rid of store pattern - getUniqueFileName(THIS) - this doesn't make sense, instances always have access to this by default * Add static theme storage - inherits from the local file storage, because they both operate on the file system - IMPORTANT: added a TODO to consider a merge of themes/loader and themes/storage - but will be definitely not part of this PR * Use new static theme storage in api/themes - storage functions are simplified! * Add https://github.com/kirrg001/Ghost-Storage-Base as dependency - tarball for now, as i am still testing - will release if PR review get's accepted * Adapt tests and jscs/jshint * 🐛 fix storage.read in favicon utility - wrong implementation of error handling * 🎨 optimise error messages for custom storage adapter errors * little renaming in the storage utlity - purpose is to have access to the custom storage instance and to the custom storage class - see next commit why * optimise instanceof base storage - instanceof is always tricky in javascript - if multiple modules exist, it can happen that instanceof is false * fix getTargetDir - the importer uses the `targetDir` option to ensure that images land in the correct folder * ghost-storage-base@0.0.1 package.json dependency
69 lines
2.0 KiB
JavaScript
69 lines
2.0 KiB
JavaScript
// jscs:disable requireMultipleVarDecl
|
|
|
|
'use strict';
|
|
|
|
var fs = require('fs-extra'),
|
|
os = require('os'),
|
|
path = require('path'),
|
|
Promise = require('bluebird'),
|
|
config = require('../config'),
|
|
utils = require('../utils'),
|
|
LocalFileStorage = require('../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;
|