mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-03-04 02:01:58 -05:00
Move saveRaw method from LocalImagesStorage and LocalStorageBase class (#21810)
ref https://linear.app/ghost/issue/CON-3/external-media-inliner-should-inline-more-file-types The external media inliner requests each file as a buffer. Saving this works well for images, but not media or files as the `saveRaw` method is not available on those storage adapters. This PR moves the `saveRaw` method from `LocalImagesStorage` to the `LocalStorageBase` class so it is available to subclasses.
This commit is contained in:
parent
1feb38442d
commit
2351016647
3 changed files with 31 additions and 28 deletions
|
@ -4,12 +4,19 @@ const config = require('../../../shared/config');
|
|||
const constants = require('@tryghost/constants');
|
||||
const LocalStorageBase = require('./LocalStorageBase');
|
||||
|
||||
const messages = {
|
||||
notFound: 'File not found',
|
||||
notFoundWithRef: 'File not found: {file}',
|
||||
cannotRead: 'Could not read File: {file}'
|
||||
};
|
||||
|
||||
class LocalFilesStorage extends LocalStorageBase {
|
||||
constructor() {
|
||||
super({
|
||||
storagePath: config.getContentPath('files'),
|
||||
siteUrl: config.getSiteUrl(),
|
||||
staticFileURLPrefix: constants.STATIC_FILES_URL_PREFIX
|
||||
staticFileURLPrefix: constants.STATIC_FILES_URL_PREFIX,
|
||||
errorMessages: messages
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,10 +1,6 @@
|
|||
// # Local File System Image Storage module
|
||||
// The (default) module for storing images, using the local file system
|
||||
|
||||
const fs = require('fs-extra');
|
||||
const path = require('path');
|
||||
const config = require('../../../shared/config');
|
||||
|
||||
const urlUtils = require('../../../shared/url-utils');
|
||||
const LocalStorageBase = require('./LocalStorageBase');
|
||||
|
||||
|
@ -23,29 +19,6 @@ class LocalImagesStorage extends LocalStorageBase {
|
|||
errorMessages: messages
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves a buffer in the targetPath
|
||||
* @param {Buffer} buffer is an instance of Buffer
|
||||
* @param {String} targetPath relative path NOT including storage path to which the buffer should be written
|
||||
* @returns {Promise<String>} a URL to retrieve the data
|
||||
*/
|
||||
async saveRaw(buffer, targetPath) {
|
||||
const storagePath = path.join(this.storagePath, targetPath);
|
||||
const targetDir = path.dirname(storagePath);
|
||||
|
||||
await fs.mkdirs(targetDir);
|
||||
await fs.writeFile(storagePath, buffer);
|
||||
|
||||
// For local file system storage can use relative path so add a slash
|
||||
const fullUrl = (
|
||||
urlUtils.urlJoin('/', urlUtils.getSubdir(),
|
||||
this.staticFileURLPrefix,
|
||||
targetPath)
|
||||
).replace(new RegExp(`\\${path.sep}`, 'g'), '/');
|
||||
|
||||
return fullUrl;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = LocalImagesStorage;
|
||||
|
|
|
@ -80,6 +80,29 @@ class LocalStorageBase extends StorageBase {
|
|||
return fullUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves a buffer in the targetPath
|
||||
* @param {Buffer} buffer is an instance of Buffer
|
||||
* @param {String} targetPath relative path NOT including storage path to which the buffer should be written
|
||||
* @returns {Promise<String>} a URL to retrieve the data
|
||||
*/
|
||||
async saveRaw(buffer, targetPath) {
|
||||
const storagePath = path.join(this.storagePath, targetPath);
|
||||
const targetDir = path.dirname(storagePath);
|
||||
|
||||
await fs.mkdirs(targetDir);
|
||||
await fs.writeFile(storagePath, buffer);
|
||||
|
||||
// For local file system storage can use relative path so add a slash
|
||||
const fullUrl = (
|
||||
urlUtils.urlJoin('/', urlUtils.getSubdir(),
|
||||
this.staticFileURLPrefix,
|
||||
targetPath)
|
||||
).replace(new RegExp(`\\${path.sep}`, 'g'), '/');
|
||||
|
||||
return fullUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {String} url full url under which the stored content is served, result of save method
|
||||
|
|
Loading…
Add table
Reference in a new issue