Skip to content

Commit

Permalink
✨ add read method to local file storage
Browse files Browse the repository at this point in the history
refs #7688

- add a read method to our local file storage
- reads the bytes of a target image
- breaking change to storage adapters
  • Loading branch information
kirrg001 committed Nov 9, 2016
1 parent 0a744c2 commit e97d59c
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 5 deletions.
2 changes: 1 addition & 1 deletion core/server/storage/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
}
Expand Down
34 changes: 30 additions & 4 deletions core/server/storage/local-file-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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;
2 changes: 2 additions & 0 deletions core/test/unit/storage/index_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
36 changes: 36 additions & 0 deletions core/test/unit/storage/local-file-store_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down Expand Up @@ -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({
Expand Down

0 comments on commit e97d59c

Please sign in to comment.