mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-10 23:36:14 -05:00
Reworked LocalFileStore methods to use async syntax
refs https://linear.app/tryghost/issue/CORE-1/multiple-adapters-per-type - While digging around this area refactored the code to use more readable async/await syntax instead of chaining then's - Dropped unneeded "catch" block in save method as all it was doing was rethrowing an error with out additional handling
This commit is contained in:
parent
ad23628279
commit
393280b6ae
2 changed files with 34 additions and 38 deletions
|
@ -29,27 +29,25 @@ class LocalFileStore extends StorageBase {
|
|||
|
||||
/**
|
||||
* Saves a buffer in the targetPath
|
||||
* - buffer is an instance of Buffer
|
||||
* - returns a Promise which returns the full URL to retrieve the data
|
||||
* @param {Buffer} buffer is an instance of Buffer
|
||||
* @param {String} targetPath path to which the buffer should be written
|
||||
* @returns {Promise<String>} a URL to retrieve the data
|
||||
*/
|
||||
saveRaw(buffer, targetPath) {
|
||||
async saveRaw(buffer, targetPath) {
|
||||
const storagePath = path.join(this.storagePath, targetPath);
|
||||
const targetDir = path.dirname(storagePath);
|
||||
|
||||
return fs.mkdirs(targetDir)
|
||||
.then(() => {
|
||||
return fs.writeFile(storagePath, buffer);
|
||||
})
|
||||
.then(() => {
|
||||
// For local file system storage can use relative path so add a slash
|
||||
const fullUrl = (
|
||||
urlUtils.urlJoin('/', urlUtils.getSubdir(),
|
||||
urlUtils.STATIC_IMAGE_URL_PREFIX,
|
||||
targetPath)
|
||||
).replace(new RegExp(`\\${path.sep}`, 'g'), '/');
|
||||
await fs.mkdirs(targetDir);
|
||||
await fs.writeFile(storagePath, buffer);
|
||||
|
||||
return fullUrl;
|
||||
});
|
||||
// For local file system storage can use relative path so add a slash
|
||||
const fullUrl = (
|
||||
urlUtils.urlJoin('/', urlUtils.getSubdir(),
|
||||
urlUtils.STATIC_IMAGE_URL_PREFIX,
|
||||
targetPath)
|
||||
).replace(new RegExp(`\\${path.sep}`, 'g'), '/');
|
||||
|
||||
return fullUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -57,34 +55,32 @@ class LocalFileStore extends StorageBase {
|
|||
* - image is the express image object
|
||||
* - returns a promise which ultimately returns the full url to the uploaded image
|
||||
*
|
||||
* @param image
|
||||
* @param targetDir
|
||||
* @returns {*}
|
||||
* @param {StorageBase.Image} image
|
||||
* @param {String} targetDir
|
||||
* @returns {Promise<String>}
|
||||
*/
|
||||
save(image, targetDir) {
|
||||
async save(image, targetDir) {
|
||||
let targetFilename;
|
||||
|
||||
// NOTE: the base implementation of `getTargetDir` returns the format this.storagePath/YYYY/MM
|
||||
targetDir = targetDir || this.getTargetDir(this.storagePath);
|
||||
|
||||
return this.getUniqueFileName(image, targetDir).then((filename) => {
|
||||
targetFilename = filename;
|
||||
return fs.mkdirs(targetDir);
|
||||
}).then(() => {
|
||||
return fs.copy(image.path, targetFilename);
|
||||
}).then(() => {
|
||||
// The src for the image must be in URI format, not a file system path, which in Windows uses \
|
||||
// For local file system storage can use relative path so add a slash
|
||||
const fullUrl = (
|
||||
urlUtils.urlJoin('/', urlUtils.getSubdir(),
|
||||
urlUtils.STATIC_IMAGE_URL_PREFIX,
|
||||
path.relative(this.storagePath, targetFilename))
|
||||
).replace(new RegExp(`\\${path.sep}`, 'g'), '/');
|
||||
const filename = await this.getUniqueFileName(image, targetDir);
|
||||
|
||||
return fullUrl;
|
||||
}).catch((e) => {
|
||||
return Promise.reject(e);
|
||||
});
|
||||
targetFilename = filename;
|
||||
await fs.mkdirs(targetDir);
|
||||
|
||||
await fs.copy(image.path, targetFilename);
|
||||
|
||||
// The src for the image must be in URI format, not a file system path, which in Windows uses \
|
||||
// For local file system storage can use relative path so add a slash
|
||||
const fullUrl = (
|
||||
urlUtils.urlJoin('/', urlUtils.getSubdir(),
|
||||
urlUtils.STATIC_IMAGE_URL_PREFIX,
|
||||
path.relative(this.storagePath, targetFilename))
|
||||
).replace(new RegExp(`\\${path.sep}`, 'g'), '/');
|
||||
|
||||
return fullUrl;
|
||||
}
|
||||
|
||||
exists(fileName, targetDir) {
|
||||
|
|
|
@ -6,12 +6,12 @@ const moment = require('moment');
|
|||
const Promise = require('bluebird');
|
||||
const path = require('path');
|
||||
const LocalFileStore = require('../../../../../core/server/adapters/storage/LocalFileStorage');
|
||||
let localFileStore;
|
||||
const configUtils = require('../../../../utils/configUtils');
|
||||
|
||||
describe('Local File System Storage', function () {
|
||||
let image;
|
||||
let momentStub;
|
||||
let localFileStore;
|
||||
|
||||
function fakeDate(mm, yyyy) {
|
||||
const month = parseInt(mm, 10);
|
||||
|
|
Loading…
Add table
Reference in a new issue