mirror of
https://github.com/strapi/strapi.git
synced 2025-09-15 19:39:06 +00:00
Merge pull request #12534 from strapi/content-68/fix-public-path
Move public path config to server.js
This commit is contained in:
commit
cf33ce0b63
@ -50,8 +50,8 @@ const LIFECYCLES = {
|
||||
class Strapi {
|
||||
constructor(opts = {}) {
|
||||
destroyOnSignal(this);
|
||||
this.dirs = utils.getDirs(opts.dir || process.cwd());
|
||||
const appConfig = loadConfiguration(this.dirs.root, opts);
|
||||
const rootDir = opts.dir || process.cwd();
|
||||
const appConfig = loadConfiguration(rootDir, opts);
|
||||
this.container = createContainer(this);
|
||||
this.container.register('config', createConfigProvider(appConfig));
|
||||
this.container.register('content-types', contentTypesRegistry(this));
|
||||
@ -65,6 +65,8 @@ class Strapi {
|
||||
this.container.register('apis', apisRegistry(this));
|
||||
this.container.register('auth', createAuth(this));
|
||||
|
||||
this.dirs = utils.getDirs(rootDir, { strapi: this });
|
||||
|
||||
this.isLoaded = false;
|
||||
this.reload = this.reload();
|
||||
this.server = createServer(this);
|
||||
|
@ -8,6 +8,7 @@ const execa = require('execa');
|
||||
const { getOr } = require('lodash/fp');
|
||||
|
||||
const { createLogger } = require('@strapi/logger');
|
||||
const { joinBy } = require('@strapi/utils');
|
||||
const loadConfiguration = require('../core/app-configuration');
|
||||
const strapi = require('../index');
|
||||
const buildAdmin = require('./build');
|
||||
@ -131,6 +132,8 @@ function watchFileChanges({ dir, strapiInstance, watchIgnoreFiles, polling }) {
|
||||
'**/index.html',
|
||||
'**/public',
|
||||
'**/public/**',
|
||||
strapiInstance.dirs.public,
|
||||
joinBy('/', strapiInstance.dirs.public, '**'),
|
||||
'**/*.db*',
|
||||
'**/exports/**',
|
||||
...watchIgnoreFiles,
|
||||
|
@ -21,6 +21,7 @@ const defaultConfig = {
|
||||
proxy: false,
|
||||
cron: { enabled: false },
|
||||
admin: { autoOpen: false },
|
||||
dirs: { public: './public' },
|
||||
},
|
||||
admin: {},
|
||||
api: {
|
||||
|
10
packages/core/strapi/lib/core/bootstrap.js
vendored
10
packages/core/strapi/lib/core/bootstrap.js
vendored
@ -1,8 +1,9 @@
|
||||
'use strict';
|
||||
|
||||
const { getConfigUrls } = require('@strapi/utils');
|
||||
const fse = require('fs-extra');
|
||||
|
||||
module.exports = function({ strapi }) {
|
||||
module.exports = async function({ strapi }) {
|
||||
strapi.config.port = strapi.config.get('server.port') || strapi.config.port;
|
||||
strapi.config.host = strapi.config.get('server.host') || strapi.config.host;
|
||||
|
||||
@ -22,4 +23,11 @@ module.exports = function({ strapi }) {
|
||||
if (!shouldServeAdmin) {
|
||||
strapi.config.serveAdminPanel = false;
|
||||
}
|
||||
|
||||
// ensure public repository exists
|
||||
if (!(await fse.pathExists(strapi.dirs.public))) {
|
||||
throw new Error(
|
||||
`The public folder (${strapi.dirs.public}) doesn't exist or is not accessible. Please make sure it exists.`
|
||||
);
|
||||
}
|
||||
};
|
||||
|
@ -1,8 +1,8 @@
|
||||
'use strict';
|
||||
|
||||
const { join } = require('path');
|
||||
const { join, resolve } = require('path');
|
||||
|
||||
const getDirs = root => ({
|
||||
const getDirs = (root, { strapi }) => ({
|
||||
root,
|
||||
src: join(root, 'src'),
|
||||
api: join(root, 'src', 'api'),
|
||||
@ -11,7 +11,7 @@ const getDirs = root => ({
|
||||
policies: join(root, 'src', 'policies'),
|
||||
middlewares: join(root, 'src', 'middlewares'),
|
||||
config: join(root, 'config'),
|
||||
public: join(root, 'public'),
|
||||
public: resolve(root, strapi.config.get('server.dirs.public')),
|
||||
});
|
||||
|
||||
module.exports = getDirs;
|
||||
|
@ -1,14 +1,18 @@
|
||||
'use strict';
|
||||
|
||||
const { join } = require('path');
|
||||
|
||||
const bootstrap = require('../bootstrap');
|
||||
|
||||
jest.mock('@strapi/provider-upload-local', () => ({ init() {} }));
|
||||
|
||||
describe('Upload plugin bootstrap function', () => {
|
||||
test('Sets default config if id does not exist', async () => {
|
||||
test('Sets default config if it does not exist', async () => {
|
||||
const setStore = jest.fn(() => {});
|
||||
const registerMany = jest.fn(() => {});
|
||||
|
||||
global.strapi = {
|
||||
dirs: { root: process.cwd() },
|
||||
dirs: { root: process.cwd(), public: join(process.cwd(), 'public') },
|
||||
admin: {
|
||||
services: { permission: { actionProvider: { registerMany } } },
|
||||
},
|
||||
@ -16,10 +20,7 @@ describe('Upload plugin bootstrap function', () => {
|
||||
error() {},
|
||||
},
|
||||
config: {
|
||||
get: jest
|
||||
.fn()
|
||||
.mockReturnValueOnce({ provider: 'local' })
|
||||
.mockReturnValueOnce('public'),
|
||||
get: jest.fn().mockReturnValueOnce({ provider: 'local' }),
|
||||
paths: {},
|
||||
info: {
|
||||
dependencies: {},
|
||||
|
@ -7,6 +7,7 @@ const {
|
||||
getCommonBeginning,
|
||||
getCommonPath,
|
||||
toRegressedEnumValue,
|
||||
joinBy,
|
||||
} = require('../string-formatting');
|
||||
|
||||
describe('string-formatting', () => {
|
||||
@ -121,4 +122,25 @@ describe('string-formatting', () => {
|
||||
expect(toRegressedEnumValue(string)).toBe(expectedResult);
|
||||
});
|
||||
});
|
||||
|
||||
describe('joinBy', () => {
|
||||
test.each([
|
||||
[['/', ''], ''],
|
||||
[['/', '/a/'], '/a/'],
|
||||
[['/', 'a', 'b'], 'a/b'],
|
||||
[['/', 'a', '/b'], 'a/b'],
|
||||
[['/', 'a/', '/b'], 'a/b'],
|
||||
[['/', 'a/', 'b'], 'a/b'],
|
||||
[['/', 'a//', 'b'], 'a/b'],
|
||||
[['/', 'a//', '//b'], 'a/b'],
|
||||
[['/', 'a', '//b'], 'a/b'],
|
||||
[['/', '/a//', '//b/'], '/a/b/'],
|
||||
[['/', 'a', 'b', 'c'], 'a/b/c'],
|
||||
[['/', 'a/', '/b/', '/c'], 'a/b/c'],
|
||||
[['/', 'a//', '//b//', '//c'], 'a/b/c'],
|
||||
[['/', '///a///', '///b///', '///c///'], '///a/b/c///'],
|
||||
])('%s => %s', (args, expectedResult) => {
|
||||
expect(joinBy(...args)).toBe(expectedResult);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -21,6 +21,7 @@ const {
|
||||
isCamelCase,
|
||||
toRegressedEnumValue,
|
||||
startsWithANumber,
|
||||
joinBy,
|
||||
} = require('./string-formatting');
|
||||
const { removeUndefined } = require('./object-formatting');
|
||||
const { getConfigUrls, getAbsoluteAdminUrl, getAbsoluteServerUrl } = require('./config');
|
||||
@ -51,6 +52,7 @@ module.exports = {
|
||||
nameToSlug,
|
||||
toRegressedEnumValue,
|
||||
startsWithANumber,
|
||||
joinBy,
|
||||
nameToCollectionName,
|
||||
getCommonBeginning,
|
||||
getConfigUrls,
|
||||
|
@ -1,5 +1,6 @@
|
||||
'use strict';
|
||||
const _ = require('lodash');
|
||||
const { trimChars, trimCharsEnd, trimCharsStart } = require('lodash/fp');
|
||||
const slugify = require('@sindresorhus/slugify');
|
||||
|
||||
const nameToSlug = (name, options = { separator: '-' }) => slugify(name, options);
|
||||
@ -44,6 +45,19 @@ const isCamelCase = value => /^[a-z][a-zA-Z0-9]+$/.test(value);
|
||||
const isKebabCase = value => /^([a-z][a-z0-9]*)(-[a-z0-9]+)*$/.test(value);
|
||||
const startsWithANumber = value => /^[0-9]/.test(value);
|
||||
|
||||
const joinBy = (joint, ...args) => {
|
||||
const trim = trimChars(joint);
|
||||
const trimEnd = trimCharsEnd(joint);
|
||||
const trimStart = trimCharsStart(joint);
|
||||
|
||||
return args.reduce((url, path, index) => {
|
||||
if (args.length === 1) return path;
|
||||
if (index === 0) return trimEnd(path);
|
||||
if (index === args.length - 1) return url + joint + trimStart(path);
|
||||
return url + joint + trim(path);
|
||||
}, '');
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
nameToSlug,
|
||||
nameToCollectionName,
|
||||
@ -56,4 +70,5 @@ module.exports = {
|
||||
isKebabCase,
|
||||
toRegressedEnumValue,
|
||||
startsWithANumber,
|
||||
joinBy,
|
||||
};
|
||||
|
@ -8,8 +8,11 @@
|
||||
const { pipeline } = require('stream');
|
||||
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 => {
|
||||
@ -18,7 +21,13 @@ module.exports = {
|
||||
}
|
||||
};
|
||||
|
||||
const publicDir = strapi.dirs.public;
|
||||
// Ensure uploads folder exists
|
||||
const uploadPath = path.resolve(strapi.dirs.public, UPLOADS_FOLDER_NAME);
|
||||
if (!fse.pathExistsSync(uploadPath)) {
|
||||
throw new Error(
|
||||
`The upload folder (${uploadPath}) doesn't exist or is not accessible. Please make sure it exists.`
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
uploadStream(file) {
|
||||
@ -27,7 +36,7 @@ module.exports = {
|
||||
return new Promise((resolve, reject) => {
|
||||
pipeline(
|
||||
file.stream,
|
||||
fs.createWriteStream(path.join(publicDir, `/uploads/${file.hash}${file.ext}`)),
|
||||
fs.createWriteStream(path.join(uploadPath, `${file.hash}${file.ext}`)),
|
||||
err => {
|
||||
if (err) {
|
||||
return reject(err);
|
||||
@ -44,24 +53,21 @@ module.exports = {
|
||||
verifySize(file);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
fs.writeFile(
|
||||
path.join(publicDir, `/uploads/${file.hash}${file.ext}`),
|
||||
file.buffer,
|
||||
err => {
|
||||
if (err) {
|
||||
return reject(err);
|
||||
}
|
||||
|
||||
file.url = `/uploads/${file.hash}${file.ext}`;
|
||||
|
||||
resolve();
|
||||
// write file in public/assets folder
|
||||
fs.writeFile(path.join(uploadPath, `${file.hash}${file.ext}`), file.buffer, err => {
|
||||
if (err) {
|
||||
return reject(err);
|
||||
}
|
||||
);
|
||||
|
||||
file.url = `/${UPLOADS_FOLDER_NAME}/${file.hash}${file.ext}`;
|
||||
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
},
|
||||
delete(file) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const filePath = path.join(publicDir, `/uploads/${file.hash}${file.ext}`);
|
||||
const filePath = path.join(uploadPath, `${file.hash}${file.ext}`);
|
||||
|
||||
if (!fs.existsSync(filePath)) {
|
||||
return resolve("File doesn't exist");
|
||||
|
@ -35,7 +35,8 @@
|
||||
"test": "echo \"no tests yet\""
|
||||
},
|
||||
"dependencies": {
|
||||
"@strapi/utils": "4.1.5"
|
||||
"@strapi/utils": "4.1.5",
|
||||
"fs-extra": "10.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=12.22.0 <=16.x.x",
|
||||
|
Loading…
x
Reference in New Issue
Block a user