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