mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-03 23:00:14 -05:00
Merge pull request #7699 from kirrg001/1.0.0-dev/read-bytes-storage
✨ add read method to local file storage
This commit is contained in:
commit
928654bd20
4 changed files with 69 additions and 5 deletions
|
@ -3,7 +3,7 @@ var moment = require('moment'),
|
|||
|
||||
function StorageBase() {
|
||||
Object.defineProperty(this, 'requiredFns', {
|
||||
value: ['exists', 'save', 'serve', 'delete'],
|
||||
value: ['exists', 'save', 'serve', 'delete', 'read'],
|
||||
writable: false
|
||||
});
|
||||
}
|
||||
|
|
|
@ -24,7 +24,7 @@ util.inherits(LocalFileStore, BaseStore);
|
|||
// Saves the image to storage (the file system)
|
||||
// - image is the express image object
|
||||
// - returns a promise which ultimately returns the full url to the uploaded image
|
||||
LocalFileStore.prototype.save = function (image, targetDir) {
|
||||
LocalFileStore.prototype.save = function save(image, targetDir) {
|
||||
targetDir = targetDir || this.getTargetDir(config.getContentPath('images'));
|
||||
var targetFilename;
|
||||
|
||||
|
@ -49,7 +49,7 @@ LocalFileStore.prototype.save = function (image, targetDir) {
|
|||
});
|
||||
};
|
||||
|
||||
LocalFileStore.prototype.exists = function (filename) {
|
||||
LocalFileStore.prototype.exists = function exists(filename) {
|
||||
return new Promise(function (resolve) {
|
||||
fs.stat(filename, function (err) {
|
||||
var exists = !err;
|
||||
|
@ -59,7 +59,7 @@ LocalFileStore.prototype.exists = function (filename) {
|
|||
};
|
||||
|
||||
// middleware for serving the files
|
||||
LocalFileStore.prototype.serve = function (options) {
|
||||
LocalFileStore.prototype.serve = function serve(options) {
|
||||
options = options || {};
|
||||
|
||||
// CASE: serve themes
|
||||
|
@ -117,11 +117,37 @@ LocalFileStore.prototype.serve = function (options) {
|
|||
}
|
||||
};
|
||||
|
||||
LocalFileStore.prototype.delete = function (fileName, targetDir) {
|
||||
LocalFileStore.prototype.delete = function deleteFile(fileName, targetDir) {
|
||||
targetDir = targetDir || this.getTargetDir(config.getContentPath('images'));
|
||||
|
||||
var pathToDelete = path.join(targetDir, fileName);
|
||||
return remove(pathToDelete);
|
||||
};
|
||||
|
||||
/**
|
||||
* Reads bytes from disk for a target image
|
||||
* path: path of target image (without content path!)
|
||||
*/
|
||||
LocalFileStore.prototype.read = function read(options) {
|
||||
options = options || {};
|
||||
|
||||
// remove trailing slashes
|
||||
options.path = (options.path || '').replace(/\/$|\\$/, '');
|
||||
|
||||
var targetPath = path.join(config.getContentPath('images'), options.path);
|
||||
|
||||
return new Promise(function (resolve, reject) {
|
||||
fs.readFile(targetPath, function (err, bytes) {
|
||||
if (err) {
|
||||
return reject(new errors.GhostError({
|
||||
err: err,
|
||||
message: 'Could not read image: ' + targetPath
|
||||
}));
|
||||
}
|
||||
|
||||
resolve(bytes);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = LocalFileStore;
|
||||
|
|
|
@ -67,6 +67,7 @@ describe('storage: index_spec', function () {
|
|||
'AnotherAdapter.prototype.save = function (){};' +
|
||||
'AnotherAdapter.prototype.serve = function (){};' +
|
||||
'AnotherAdapter.prototype.delete = function (){};' +
|
||||
'AnotherAdapter.prototype.read = function (){};' +
|
||||
'module.exports = AnotherAdapter', chosenStorage;
|
||||
|
||||
fs.writeFileSync(scope.adapter, jsFile);
|
||||
|
@ -103,6 +104,7 @@ describe('storage: index_spec', function () {
|
|||
'AnotherAdapter.prototype.save = function (){};' +
|
||||
'AnotherAdapter.prototype.serve = function (){};' +
|
||||
'AnotherAdapter.prototype.delete = function (){};' +
|
||||
'AnotherAdapter.prototype.read = function (){};' +
|
||||
'module.exports = AnotherAdapter', adapter;
|
||||
|
||||
fs.writeFileSync(scope.adapter, jsFile);
|
||||
|
|
|
@ -3,6 +3,7 @@ var fs = require('fs-extra'),
|
|||
path = require('path'),
|
||||
should = require('should'),
|
||||
sinon = require('sinon'),
|
||||
errors = require('../../../server/errors'),
|
||||
LocalFileStore = require('../../../server/storage/local-file-store'),
|
||||
localFileStore,
|
||||
|
||||
|
@ -148,6 +149,41 @@ describe('Local File System Storage', function () {
|
|||
}).catch(done);
|
||||
});
|
||||
|
||||
describe('read image', function () {
|
||||
beforeEach(function () {
|
||||
// we have some example images in our test utils folder
|
||||
configUtils.set('paths:contentPath', path.join(__dirname, '../../utils/fixtures'));
|
||||
});
|
||||
|
||||
it('success', function (done) {
|
||||
localFileStore.read({path: 'ghost-logo.png'})
|
||||
.then(function (bytes) {
|
||||
bytes.length.should.eql(8638);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('success', function (done) {
|
||||
localFileStore.read({path: '/ghost-logo.png/'})
|
||||
.then(function (bytes) {
|
||||
bytes.length.should.eql(8638);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('image does not exist', function (done) {
|
||||
localFileStore.read({path: 'does-not-exist.png'})
|
||||
.then(function () {
|
||||
done(new Error('image should not exist'));
|
||||
})
|
||||
.catch(function (err) {
|
||||
(err instanceof errors.GhostError).should.eql(true);
|
||||
err.code.should.eql('ENOENT');
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('validate extentions', function () {
|
||||
it('name contains a .\d as extension', function (done) {
|
||||
localFileStore.save({
|
||||
|
|
Loading…
Add table
Reference in a new issue