2021-09-28 15:17:13 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const { resolve } = require('path');
|
|
|
|
const range = require('koa-range');
|
|
|
|
const koaStatic = require('koa-static');
|
|
|
|
|
2021-09-29 10:47:10 +02:00
|
|
|
/**
|
|
|
|
* Programmatic upload middleware. We do not want to export it
|
|
|
|
* @param {{ strapi: import('@strapi/strapi').Strapi }}
|
|
|
|
*/
|
|
|
|
module.exports = ({ strapi }) => {
|
2021-09-28 15:17:13 +02:00
|
|
|
const configPublicPath = strapi.config.get(
|
|
|
|
'middleware.settings.public.path',
|
|
|
|
strapi.config.paths.static
|
|
|
|
);
|
2021-09-29 10:47:10 +02:00
|
|
|
|
2021-09-28 15:17:13 +02:00
|
|
|
const staticDir = resolve(strapi.dirs.root, configPublicPath);
|
|
|
|
|
|
|
|
strapi.server.app.on('error', err => {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
strapi.server.app.onerror(err);
|
|
|
|
});
|
|
|
|
|
|
|
|
const localServerConfig = strapi.config.get('plugin.upload.providerOptions.localeServer', {});
|
|
|
|
|
|
|
|
strapi.server.routes([
|
|
|
|
{
|
|
|
|
method: 'GET',
|
|
|
|
path: '/uploads/(.*)',
|
|
|
|
handler: [range, koaStatic(staticDir, { defer: true, ...localServerConfig })],
|
|
|
|
config: { auth: false },
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
};
|