🐛 Fixed uploading images with custom storage adapters

fixes https://github.com/TryGhost/Ghost/issues/16278

`urlToPath` is not a method that is defined on the StorageBase, and thus is not implemented on any custom storage adapter.

We need this method to prevent uploading a file in two different directories. Currently this is an edge case:
- If you upload a file at midnight between a month shift, it is possible that we upload one file at `/2023/02/file.png` and the original at `/2023/03/file_o.png`
- To make sure we upload the second at the same directory, we need the `urlToPath` implementation. The save method sadly only returns the URL, which could be different than the stored file path, so we cannot use that return value.

This fix only uses `urlToPath` if available, to prevent above issue.
This commit is contained in:
Simon Backx 2023-02-16 11:49:37 +01:00
parent 8dfa490638
commit f8e77ad012

View File

@ -37,13 +37,20 @@ module.exports = {
...frame.file,
path: out
});
const processedImagePath = store.urlToPath(processedImageUrl);
// Get the path and name of the processed image
// We want to store the original image on the same name + _o
// So we need to wait for the first store to finish before generating the name of the original image
const processedImageName = path.basename(processedImagePath);
const processedImageDir = path.dirname(processedImagePath);
let processedImageName = path.basename(processedImageUrl);
let processedImageDir = undefined;
if (store.urlToPath) {
// Currently urlToPath is not part of StorageBase, so not all storage provider have implemented it
const processedImagePath = store.urlToPath(processedImageUrl);
// Get the path and name of the processed image
// We want to store the original image on the same name + _o
// So we need to wait for the first store to finish before generating the name of the original image
processedImageName = path.basename(processedImagePath);
processedImageDir = path.dirname(processedImagePath);
}
// Store the original image
await store.save({