0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-11 02:12:21 -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:
Paul Davis 2024-12-11 12:24:01 +00:00 committed by GitHub
parent 1feb38442d
commit 2351016647
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 31 additions and 28 deletions

View file

@ -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
});
}
}

View file

@ -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;

View file

@ -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