2020-03-14 17:35:25 +01:00
|
|
|
'use strict';
|
|
|
|
|
2020-08-03 09:59:09 +02:00
|
|
|
const { resolve } = require('path');
|
2020-03-14 17:35:25 +01:00
|
|
|
const range = require('koa-range');
|
|
|
|
const koaStatic = require('koa-static');
|
2021-02-15 20:58:59 +07:00
|
|
|
const _ = require('lodash');
|
2020-03-14 17:35:25 +01:00
|
|
|
|
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
|
|
|
|
);
|
|
|
|
const staticDir = resolve(strapi.dir, configPublicPath);
|
2020-03-14 17:35:25 +01:00
|
|
|
|
2021-07-29 16:39:26 +02:00
|
|
|
strapi.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;
|
|
|
|
}
|
2020-03-14 17:35:25 +01:00
|
|
|
|
2021-07-29 16:39:26 +02:00
|
|
|
strapi.app.onerror(err);
|
|
|
|
});
|
2020-03-14 17:35:25 +01:00
|
|
|
|
2021-07-29 16:39:26 +02:00
|
|
|
const localServerConfig =
|
|
|
|
_.get(strapi, 'plugins.upload.config.providerOptions.localServer') || {};
|
|
|
|
strapi.router.get(
|
|
|
|
'/uploads/(.*)',
|
|
|
|
range,
|
|
|
|
koaStatic(staticDir, { defer: true, ...localServerConfig })
|
|
|
|
);
|
|
|
|
},
|
2020-03-14 17:35:25 +01:00
|
|
|
},
|
2021-07-29 16:39:26 +02:00
|
|
|
};
|