mirror of
https://github.com/strapi/strapi.git
synced 2025-09-20 22:10:06 +00:00

* chore: convert upload-local * chore: convert upload-cloudinary * chore: convert upload-aws-s3 * chore: convert email-sendmail * chore: convert email-sendgrid * chore: convert email-nodemailer * chore: convert email-mailgun * chore: convert email-amazon-ses * chore: convert audit-logs-local * chore(utils): fix eslint error
141 lines
3.8 KiB
TypeScript
141 lines
3.8 KiB
TypeScript
import { pipeline } from 'stream';
|
|
import fs, { ReadStream } from 'fs';
|
|
import path from 'path';
|
|
import fse from 'fs-extra';
|
|
import * as utils from '@strapi/utils';
|
|
|
|
interface File {
|
|
name: string;
|
|
alternativeText?: string;
|
|
caption?: string;
|
|
width?: number;
|
|
height?: number;
|
|
formats?: Record<string, unknown>;
|
|
hash: string;
|
|
ext?: string;
|
|
mime: string;
|
|
size: number;
|
|
url: string;
|
|
previewUrl?: string;
|
|
path?: string;
|
|
provider?: string;
|
|
provider_metadata?: Record<string, unknown>;
|
|
stream?: ReadStream;
|
|
buffer?: Buffer;
|
|
}
|
|
|
|
const { PayloadTooLargeError } = utils.errors;
|
|
const { kbytesToBytes, bytesToHumanReadable } = utils.file;
|
|
|
|
const UPLOADS_FOLDER_NAME = 'uploads';
|
|
|
|
interface InitOptions {
|
|
sizeLimit?: number;
|
|
}
|
|
|
|
interface CheckFileSizeOptions {
|
|
sizeLimit?: number;
|
|
}
|
|
|
|
export default {
|
|
init({ sizeLimit: providerOptionsSizeLimit }: InitOptions = {}) {
|
|
// TODO V5: remove providerOptions sizeLimit
|
|
if (providerOptionsSizeLimit) {
|
|
process.emitWarning(
|
|
'[deprecated] In future versions, "sizeLimit" argument will be ignored from upload.config.providerOptions. Move it to upload.config'
|
|
);
|
|
}
|
|
|
|
// Ensure uploads folder exists
|
|
const uploadPath = path.resolve(strapi.dirs.static.public, UPLOADS_FOLDER_NAME);
|
|
if (!fse.pathExistsSync(uploadPath)) {
|
|
throw new Error(
|
|
`The upload folder (${uploadPath}) doesn't exist or is not accessible. Please make sure it exists.`
|
|
);
|
|
}
|
|
|
|
return {
|
|
checkFileSize(file: File, options: CheckFileSizeOptions) {
|
|
const { sizeLimit } = options ?? {};
|
|
|
|
// TODO V5: remove providerOptions sizeLimit
|
|
if (providerOptionsSizeLimit) {
|
|
if (kbytesToBytes(file.size) > providerOptionsSizeLimit)
|
|
throw new PayloadTooLargeError(
|
|
`${file.name} exceeds size limit of ${bytesToHumanReadable(
|
|
providerOptionsSizeLimit
|
|
)}.`
|
|
);
|
|
} else if (sizeLimit) {
|
|
if (kbytesToBytes(file.size) > sizeLimit)
|
|
throw new PayloadTooLargeError(
|
|
`${file.name} exceeds size limit of ${bytesToHumanReadable(sizeLimit)}.`
|
|
);
|
|
}
|
|
},
|
|
uploadStream(file: File): Promise<void> {
|
|
if (!file.stream) {
|
|
return Promise.reject(new Error('Missing file stream'));
|
|
}
|
|
|
|
const { stream } = file;
|
|
|
|
return new Promise((resolve, reject) => {
|
|
pipeline(
|
|
stream,
|
|
fs.createWriteStream(path.join(uploadPath, `${file.hash}${file.ext}`)),
|
|
(err) => {
|
|
if (err) {
|
|
return reject(err);
|
|
}
|
|
|
|
file.url = `/${UPLOADS_FOLDER_NAME}/${file.hash}${file.ext}`;
|
|
|
|
resolve();
|
|
}
|
|
);
|
|
});
|
|
},
|
|
upload(file: File): Promise<void> {
|
|
if (!file.buffer) {
|
|
return Promise.reject(new Error('Missing file buffer'));
|
|
}
|
|
|
|
const { buffer } = file;
|
|
|
|
return new Promise((resolve, reject) => {
|
|
// write file in public/assets folder
|
|
fs.writeFile(path.join(uploadPath, `${file.hash}${file.ext}`), buffer, (err) => {
|
|
if (err) {
|
|
return reject(err);
|
|
}
|
|
|
|
file.url = `/${UPLOADS_FOLDER_NAME}/${file.hash}${file.ext}`;
|
|
|
|
resolve();
|
|
});
|
|
});
|
|
},
|
|
delete(file: File): Promise<string | void> {
|
|
return new Promise((resolve, reject) => {
|
|
const filePath = path.join(uploadPath, `${file.hash}${file.ext}`);
|
|
|
|
if (!fs.existsSync(filePath)) {
|
|
resolve("File doesn't exist");
|
|
return;
|
|
}
|
|
|
|
// remove file from public/assets folder
|
|
fs.unlink(filePath, (err) => {
|
|
if (err) {
|
|
return reject(err);
|
|
}
|
|
|
|
resolve();
|
|
});
|
|
});
|
|
},
|
|
};
|
|
},
|
|
};
|