Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add support for private upload providers #19863

Merged
merged 3 commits into from Mar 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -5,7 +5,7 @@ import { Duplex, PassThrough, Readable } from 'stream';
import { stat, createReadStream, ReadStream } from 'fs-extra';
import type { LoadedStrapi } from '@strapi/types';

import type { IAsset } from '../../../../types';
import type { IAsset, IFile } from '../../../../types';

const protocolForPath = (filepath: string) => {
return filepath?.startsWith('https') ? https : http;
Expand Down Expand Up @@ -61,6 +61,28 @@ function getFileStats(filepath: string, isLocal = false): Promise<{ size: number
});
});
}

async function signFile(file: IFile) {
const { provider } = strapi.plugins.upload;
const { provider: providerName } = strapi.config.get('plugin.upload') as { provider: string };
const isPrivate = await provider.isPrivate();
if (file?.provider === providerName && isPrivate) {
const signUrl = async (file: IFile) => {
const signedUrl = await provider.getSignedUrl(file);
file.url = signedUrl.url;
};

// Sign the original file
await signUrl(file);
// Sign each file format
if (file.formats) {
for (const format of Object.keys(file.formats)) {
await signUrl(file.formats[format]);
}
}
}
}

/**
* Generate and consume assets streams in order to stream each file individually
*/
Expand All @@ -76,6 +98,9 @@ export const createAssetsStream = (strapi: LoadedStrapi): Duplex => {

for await (const file of stream) {
const isLocalProvider = file.provider === 'local';
if (!isLocalProvider) {
await signFile(file);
}
const filepath = isLocalProvider ? join(strapi.dirs.static.public, file.url) : file.url;
const stats = await getFileStats(filepath, isLocalProvider);
const stream = getFileStream(filepath, isLocalProvider);
Expand Down