move public config to server

This commit is contained in:
Pierre Noël 2022-02-15 14:20:25 +01:00
parent 7576b81d1f
commit fb843f44f6
11 changed files with 6072 additions and 941 deletions

View File

@ -50,7 +50,7 @@ const LIFECYCLES = {
class Strapi {
constructor(opts = {}) {
destroyOnSignal(this);
this.dirs = utils.getDirs(opts.dir || process.cwd());
this.dirs = utils.getDirs(opts.dir || process.cwd(), { strapi: this });
const appConfig = loadConfiguration(this.dirs.root, opts);
this.container = createContainer(this);
this.container.register('config', createConfigProvider(appConfig));

View File

@ -6,6 +6,7 @@ const fs = require('fs-extra');
const chokidar = require('chokidar');
const execa = require('execa');
const { getOr } = require('lodash/fp');
const urlJoin = require('url-join');
const { createLogger } = require('@strapi/logger');
const loadConfiguration = require('../core/app-configuration');
@ -131,6 +132,8 @@ function watchFileChanges({ dir, strapiInstance, watchIgnoreFiles, polling }) {
'**/index.html',
'**/public',
'**/public/**',
strapiInstance.config.get('server.public.path'),
urlJoin(strapiInstance.config.get('server.public.path'), '**'),
'**/*.db*',
'**/exports/**',
...watchIgnoreFiles,

View File

@ -21,6 +21,7 @@ const defaultConfig = {
proxy: false,
cron: { enabled: false },
admin: { autoOpen: false },
public: { path: './public' },
},
admin: {},
api: {
@ -55,5 +56,9 @@ module.exports = (dir, initialConfig = {}) => {
const envDir = path.resolve(configDir, 'env', process.env.NODE_ENV);
const envConfig = loadConfigDir(envDir);
return _.merge(rootConfig, defaultConfig, baseConfig, envConfig);
const config = _.merge(rootConfig, defaultConfig, baseConfig, envConfig);
config.server.public.path = path.resolve(dir, config.server.public.path);
return config;
};

View File

@ -1,6 +1,7 @@
'use strict';
const { getConfigUrls } = require('@strapi/utils');
const fse = require('fs-extra');
module.exports = function({ strapi }) {
strapi.config.port = strapi.config.get('server.port') || strapi.config.port;
@ -22,4 +23,7 @@ module.exports = function({ strapi }) {
if (!shouldServeAdmin) {
strapi.config.serveAdminPanel = false;
}
// ensure public repository exists
fse.ensureDir(strapi.config.get('server.public.path'));
};

View File

@ -80,7 +80,7 @@ module.exports = (config, { strapi }) => {
{
method: 'GET',
path: '/(.*)',
handler: koaStatic(strapi.dirs.public, {
handler: koaStatic(strapi.config.get('server.public.path'), {
maxage: maxAge,
defer: true,
}),

View File

@ -2,7 +2,7 @@
const { join } = require('path');
const getDirs = root => ({
const getDirs = (root, { strapi }) => ({
root,
src: join(root, 'src'),
api: join(root, 'src', 'api'),
@ -11,7 +11,13 @@ const getDirs = root => ({
policies: join(root, 'src', 'policies'),
middlewares: join(root, 'src', 'middlewares'),
config: join(root, 'config'),
public: join(root, 'public'),
get public() {
// TODO V5: to be removed
process.emitWarning(
`[Deprecated] strapi.dirs.public will not exist in future versions. Prefere using strapi.config.get('server.public.path') instead.`
);
return strapi.config.get('server.public.path');
},
});
module.exports = getDirs;

View File

@ -127,6 +127,7 @@
"resolve-cwd": "3.0.0",
"semver": "7.3.5",
"statuses": "2.0.1",
"url-join": "4.0.1",
"uuid": "^3.3.2"
},
"devDependencies": {

View File

@ -25,7 +25,10 @@ module.exports = ({ strapi }) => {
{
method: 'GET',
path: '/uploads/(.*)',
handler: [range, koaStatic(strapi.dirs.public, { defer: true, ...localServerConfig })],
handler: [
range,
koaStatic(strapi.config.get('server.public.path'), { defer: true, ...localServerConfig }),
],
config: { auth: false },
},
]);

View File

@ -7,8 +7,11 @@
// Public node modules.
const fs = require('fs');
const path = require('path');
const fse = require('fs-extra');
const { PayloadTooLargeError } = require('@strapi/utils').errors;
const UPLOADS_FOLDER_NAME = 'uploads';
module.exports = {
init({ sizeLimit = 1000000 } = {}) {
const verifySize = file => {
@ -17,7 +20,11 @@ module.exports = {
}
};
const publicDir = strapi.dirs.public;
const publicDir = strapi.config.get('server.public.path');
// Ensure uploads folder exists
const uploadPath = path.resolve(publicDir, UPLOADS_FOLDER_NAME);
fse.ensureDirSync(uploadPath);
return {
upload(file) {
@ -25,19 +32,15 @@ module.exports = {
return new Promise((resolve, reject) => {
// write file in public/assets folder
fs.writeFile(
path.join(publicDir, `/uploads/${file.hash}${file.ext}`),
file.buffer,
err => {
fs.writeFile(path.join(uploadPath, `${file.hash}${file.ext}`), file.buffer, err => {
if (err) {
return reject(err);
}
file.url = `/uploads/${file.hash}${file.ext}`;
file.url = `/${UPLOADS_FOLDER_NAME}/${file.hash}${file.ext}`;
resolve();
}
);
});
});
},
delete(file) {

View File

@ -35,7 +35,8 @@
"test": "echo \"no tests yet\""
},
"dependencies": {
"@strapi/utils": "4.1.0"
"@strapi/utils": "4.1.0",
"fs-extra": "10.0.0"
},
"engines": {
"node": ">=12.22.0 <=16.x.x",

6947
yarn.lock

File diff suppressed because it is too large Load Diff