41 lines
1.2 KiB
JavaScript
Raw Normal View History

'use strict';
const { resolve } = require('path');
const range = require('koa-range');
const koaStatic = require('koa-static');
2021-07-29 16:39:26 +02:00
module.exports = {
defaults: { upload: { enabled: true } },
load: {
initialize() {
const configPublicPath = strapi.config.get(
'middleware.settings.public.path',
strapi.config.paths.static
);
2021-09-17 14:10:48 +02:00
const staticDir = resolve(strapi.dirs.root, configPublicPath);
2021-09-01 19:55:16 +02:00
strapi.server.app.on('error', err => {
2021-07-29 16:39:26 +02:00
if (err.code === 'EPIPE') {
// when serving audio or video the browsers sometimes close the connection to go to range requests instead.
// This causes koa to emit a write EPIPE error. We can ignore it.
// Right now this ignores it globally and we cannot do much more because it is how koa handles it.
return;
}
2021-09-01 19:55:16 +02:00
strapi.server.app.onerror(err);
2021-07-29 16:39:26 +02:00
});
2021-09-01 19:55:16 +02:00
const localServerConfig = strapi.config.get('plugin.upload.providerOptions.localeServer', {});
strapi.server.routes([
{
method: 'GET',
path: '/uploads/(.*)',
handler: [range, koaStatic(staticDir, { defer: true, ...localServerConfig })],
2021-09-08 16:16:16 +02:00
config: { auth: false },
2021-09-01 19:55:16 +02:00
},
]);
2021-07-29 16:39:26 +02:00
},
},
2021-07-29 16:39:26 +02:00
};