mirror of
https://github.com/strapi/strapi.git
synced 2025-07-19 23:16:47 +00:00
Use strapi.plugins.upload.config for provider config
Signed-off-by: Alexandre Bodin <bodin.alex@gmail.com>
This commit is contained in:
parent
d79053c6bc
commit
fd3511b511
8
packages/strapi-plugin-upload/config/config.json
Normal file
8
packages/strapi-plugin-upload/config/config.json
Normal file
@ -0,0 +1,8 @@
|
||||
{
|
||||
"enabled": true,
|
||||
"provider": "local",
|
||||
"providerOptions": {
|
||||
"sizeLimit": 1000
|
||||
},
|
||||
"providers": []
|
||||
}
|
@ -17,13 +17,6 @@ module.exports = async () => {
|
||||
key: 'settings',
|
||||
});
|
||||
|
||||
Object.assign(strapi.plugins.upload.config, {
|
||||
enabled: true,
|
||||
provider: 'local',
|
||||
sizeLimit: 1000000,
|
||||
providers: [],
|
||||
});
|
||||
|
||||
const installedProviders = Object.keys(strapi.config.info.dependencies)
|
||||
.filter(d => d.includes('strapi-provider-upload-'))
|
||||
.concat('strapi-provider-upload-local');
|
||||
|
@ -36,9 +36,7 @@ module.exports = {
|
||||
resolver: async (obj, { file: upload, ...fields }) => {
|
||||
const file = await formatFile(upload, fields);
|
||||
|
||||
const config = strapi.plugins.upload.config;
|
||||
|
||||
const uploadedFiles = await strapi.plugins.upload.services.upload.upload([file], config);
|
||||
const uploadedFiles = await strapi.plugins.upload.services.upload.upload([file]);
|
||||
|
||||
// Return response.
|
||||
return uploadedFiles.length === 1 ? uploadedFiles[0] : uploadedFiles;
|
||||
@ -50,9 +48,7 @@ module.exports = {
|
||||
resolver: async (obj, { files: uploads, ...fields }) => {
|
||||
const files = await Promise.all(uploads.map(upload => formatFile(upload, fields)));
|
||||
|
||||
const config = strapi.plugins.upload.config;
|
||||
|
||||
const uploadedFiles = await strapi.plugins.upload.services.upload.upload(files, config);
|
||||
const uploadedFiles = await strapi.plugins.upload.services.upload.upload(files);
|
||||
|
||||
// Return response.
|
||||
return uploadedFiles;
|
||||
@ -79,7 +75,7 @@ const formatFile = async (upload, fields) => {
|
||||
ext: path.extname(filename),
|
||||
buffer,
|
||||
mime: mimetype,
|
||||
size: (buffer.length / 1000).toFixed(2),
|
||||
size: Math.round((stream.size / 1000) * 100) / 100,
|
||||
};
|
||||
|
||||
const { refId, ref, source, field } = fields;
|
||||
|
@ -13,24 +13,15 @@ module.exports = {
|
||||
const uploadService = strapi.plugins.upload.services.upload;
|
||||
|
||||
// Retrieve provider configuration.
|
||||
const config = strapi.plugins.upload.config;
|
||||
const { enabled } = strapi.plugins.upload.config;
|
||||
|
||||
// Verify if the file upload is enable.
|
||||
if (config.enabled === false) {
|
||||
return ctx.badRequest(
|
||||
null,
|
||||
|
||||
[
|
||||
{
|
||||
messages: [
|
||||
{
|
||||
id: 'Upload.status.disabled',
|
||||
message: 'File upload is disabled',
|
||||
},
|
||||
],
|
||||
},
|
||||
]
|
||||
);
|
||||
if (enabled === false) {
|
||||
throw strapi.errors.badRequest(null, {
|
||||
errors: [
|
||||
{ id: 'Upload.status.disabled', message: 'File upload is disabled' },
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
// Extract optional relational data.
|
||||
@ -38,31 +29,15 @@ module.exports = {
|
||||
const { files = {} } = ctx.request.files || {};
|
||||
|
||||
if (_.isEmpty(files)) {
|
||||
return ctx.badRequest(null, [
|
||||
{
|
||||
messages: [{ id: 'Upload.status.empty', message: 'Files are empty' }],
|
||||
},
|
||||
]);
|
||||
throw strapi.errors.badRequest(null, {
|
||||
errors: [{ id: 'Upload.status.empty', message: 'Files are empty' }],
|
||||
});
|
||||
}
|
||||
|
||||
// Transform stream files to buffer
|
||||
const buffers = await uploadService.bufferize(files);
|
||||
|
||||
const enhancedFiles = buffers.map(file => {
|
||||
if (file.size > config.sizeLimit) {
|
||||
return ctx.badRequest(null, [
|
||||
{
|
||||
messages: [
|
||||
{
|
||||
id: 'Upload.status.sizeLimit',
|
||||
message: `${file.name} file is bigger than limit size!`,
|
||||
values: { file: file.name },
|
||||
},
|
||||
],
|
||||
},
|
||||
]);
|
||||
}
|
||||
|
||||
// Add details to the file to be able to create the relationships.
|
||||
if (refId && ref && field) {
|
||||
Object.assign(file, {
|
||||
@ -92,7 +67,7 @@ module.exports = {
|
||||
return;
|
||||
}
|
||||
|
||||
const uploadedFiles = await uploadService.upload(enhancedFiles, config);
|
||||
const uploadedFiles = await uploadService.upload(enhancedFiles);
|
||||
|
||||
// Send 200 `ok`
|
||||
ctx.send(uploadedFiles);
|
||||
@ -151,7 +126,6 @@ module.exports = {
|
||||
|
||||
async destroy(ctx) {
|
||||
const { id } = ctx.params;
|
||||
const config = strapi.plugins.upload.config;
|
||||
|
||||
const file = await strapi.plugins['upload'].services.upload.fetch({ id });
|
||||
|
||||
@ -159,7 +133,7 @@ module.exports = {
|
||||
return ctx.notFound('file.notFound');
|
||||
}
|
||||
|
||||
await strapi.plugins['upload'].services.upload.remove(file, config);
|
||||
await strapi.plugins['upload'].services.upload.remove(file);
|
||||
|
||||
ctx.send(file);
|
||||
},
|
||||
|
@ -45,7 +45,7 @@ module.exports = {
|
||||
ext: stream.name.split('.').length > 1 ? `.${_.last(stream.name.split('.'))}` : '',
|
||||
buffer,
|
||||
mime: stream.type,
|
||||
size: (stream.size / 1000).toFixed(2),
|
||||
size: Math.round((stream.size / 1000) * 100) / 100,
|
||||
};
|
||||
};
|
||||
|
||||
@ -53,7 +53,9 @@ module.exports = {
|
||||
return Promise.all(files.map(stream => createBuffer(stream)));
|
||||
},
|
||||
|
||||
async upload(files, config) {
|
||||
async upload(files) {
|
||||
const config = strapi.plugins.upload.config;
|
||||
|
||||
// Get upload provider settings to configure the provider to use.
|
||||
const provider = _.find(strapi.plugins.upload.config.providers, {
|
||||
provider: config.provider,
|
||||
@ -65,23 +67,17 @@ module.exports = {
|
||||
);
|
||||
}
|
||||
|
||||
const actions = await provider.init(config);
|
||||
const actions = await provider.init(config.providerOptions);
|
||||
|
||||
// upload a single file
|
||||
const uploadFile = async file => {
|
||||
await actions.upload(file);
|
||||
|
||||
// Remove buffer to don't save it.
|
||||
delete file.buffer;
|
||||
file.provider = provider.provider;
|
||||
|
||||
const res = await this.add(file);
|
||||
|
||||
// Remove temp file
|
||||
if (file.tmpPath) {
|
||||
fs.unlinkSync(file.tmpPath);
|
||||
}
|
||||
|
||||
strapi.eventHub.emit('media.create', { media: res });
|
||||
return res;
|
||||
};
|
||||
@ -108,18 +104,18 @@ module.exports = {
|
||||
return strapi.query('file', 'upload').count(params);
|
||||
},
|
||||
|
||||
async remove(file, config) {
|
||||
async remove(file) {
|
||||
const config = strapi.plugins.upload.config;
|
||||
|
||||
// get upload provider settings to configure the provider to use
|
||||
const provider = _.cloneDeep(
|
||||
_.find(strapi.plugins.upload.config.providers, {
|
||||
provider: config.provider,
|
||||
})
|
||||
);
|
||||
_.assign(provider, config);
|
||||
const actions = provider.init(config);
|
||||
const provider = _.find(strapi.plugins.upload.config.providers, {
|
||||
provider: config.provider,
|
||||
});
|
||||
|
||||
const actions = provider.init(config.providerOptions);
|
||||
|
||||
// execute delete function of the provider
|
||||
if (file.provider === provider.provider) {
|
||||
if (file.provider === config.provider) {
|
||||
await actions.delete(file);
|
||||
}
|
||||
|
||||
@ -133,9 +129,6 @@ module.exports = {
|
||||
},
|
||||
|
||||
async uploadToEntity(params, files, source) {
|
||||
// Retrieve provider settings from database.
|
||||
const config = strapi.plugins.upload.config;
|
||||
|
||||
const model = strapi.getModel(params.model, source);
|
||||
|
||||
// Asynchronous upload.
|
||||
@ -161,7 +154,7 @@ module.exports = {
|
||||
});
|
||||
|
||||
// Make upload async.
|
||||
return this.upload(enhancedFiles, config);
|
||||
return this.upload(enhancedFiles);
|
||||
})
|
||||
);
|
||||
},
|
||||
|
@ -7,44 +7,70 @@
|
||||
// Public node modules.
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
/* eslint-disable no-unused-vars */
|
||||
|
||||
module.exports = {
|
||||
provider: 'local',
|
||||
name: 'Local server',
|
||||
init: (config) => {
|
||||
init: ({ sizeLimit = 1000000 } = {}) => {
|
||||
const verifySize = file => {
|
||||
if (file.size > sizeLimit) {
|
||||
throw strapi.errors.badRequest('FileToBig', {
|
||||
errors: [
|
||||
{
|
||||
id: 'Upload.status.sizeLimit',
|
||||
message: `${file.name} file is bigger than limit size!`,
|
||||
values: { file: file.name },
|
||||
},
|
||||
],
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
upload: (file) => {
|
||||
upload: file => {
|
||||
verifySize(file);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
// write file in public/assets folder
|
||||
fs.writeFile(path.join(strapi.config.public.path, `/uploads/${file.hash}${file.ext}`), file.buffer, (err) => {
|
||||
fs.writeFile(
|
||||
path.join(
|
||||
strapi.config.public.path,
|
||||
`/uploads/${file.hash}${file.ext}`
|
||||
),
|
||||
file.buffer,
|
||||
err => {
|
||||
if (err) {
|
||||
return reject(err);
|
||||
}
|
||||
|
||||
file.url = `/uploads/${file.hash}${file.ext}`;
|
||||
|
||||
resolve();
|
||||
}
|
||||
);
|
||||
});
|
||||
},
|
||||
delete: file => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const filePath = path.join(
|
||||
strapi.config.public.path,
|
||||
`/uploads/${file.hash}${file.ext}`
|
||||
);
|
||||
|
||||
if (!fs.existsSync(filePath)) {
|
||||
return resolve("File doesn't exist");
|
||||
}
|
||||
|
||||
// remove file from public/assets folder
|
||||
fs.unlink(filePath, err => {
|
||||
if (err) {
|
||||
return reject(err);
|
||||
}
|
||||
|
||||
file.url = `/uploads/${file.hash}${file.ext}`;
|
||||
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
},
|
||||
delete: (file) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const filePath = path.join(strapi.config.public.path, `/uploads/${file.hash}${file.ext}`);
|
||||
|
||||
if (!fs.existsSync(filePath)) {
|
||||
return resolve('File doesn\'t exist');
|
||||
}
|
||||
|
||||
// remove file from public/assets folder
|
||||
fs.unlink(filePath, (err) => {
|
||||
if (err) {
|
||||
return reject(err);
|
||||
}
|
||||
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
},
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user