0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00

Changed location of built public content to temporary dir (#21857)

refs
[ONC-662](https://linear.app/ghost/issue/ONC-662/fix-file-write-issues-in-ghost-application-related-to-asset-generation)

Changed location of built public content to temporary dir to circumvent
file writing issues in some environments (i.e gluster). This is a
stop-gap measure until we get to refactoring the generation of the built
public content.
This commit is contained in:
Michael Barrett 2024-12-11 11:34:37 +00:00
parent 21616745e5
commit adacb4f451
No known key found for this signature in database
3 changed files with 52 additions and 2 deletions

View file

@ -1,3 +1,5 @@
const crypto = require('crypto');
const os = require('os');
const path = require('path');
const {URL} = require('url');
@ -64,6 +66,24 @@ const isPrivacyDisabled = function isPrivacyDisabled(privacyFlag) {
return this.get('privacy')[privacyFlag] === false;
};
/** @type {string|null} */
let processTmpDirPath = null;
/**
* Get a tmp dir path for the current process
*
* @returns {string} - tmp dir path for the current process
*/
function getProcessTmpDirPath() {
// Memoize the computed path to avoid re-computing it on each call - The
// value should not change during the lifetime of the process.
if (processTmpDirPath === null) {
processTmpDirPath = path.join(os.tmpdir(), `ghost_${crypto.randomUUID()}`);
}
return processTmpDirPath;
}
/**
* @callback getContentPathFn
* @param {string} type - the type of context you want the path for
@ -88,7 +108,7 @@ const getContentPath = function getContentPath(type) {
case 'settings':
return path.join(this.get('paths:contentPath'), 'settings/');
case 'public':
return path.join(this.get('paths:contentPath'), 'public/');
return path.join(getProcessTmpDirPath(this), 'public/');
default:
// new Error is allowed here, as we do not want config to depend on @tryghost/error
// @TODO: revisit this decision when @tryghost/error is no longer dependent on all of ghost-ignition

View file

@ -187,6 +187,6 @@ describe('servePublicFile', function () {
res.writeHead.called.should.be.true();
res.writeHead.args[0][0].should.equal(200);
fileStub.firstCall.args[0].should.endWith('content/public/something.css');
fileStub.firstCall.args[0].should.endWith('/public/something.css');
});
});

View file

@ -1,6 +1,10 @@
const os = require('os');
const should = require('should');
const configUtils = require('../../../utils/configUtils');
const UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
describe('vhost utils', function () {
beforeEach(function () {
configUtils.set('url', 'http://ghost.blog');
@ -52,3 +56,29 @@ describe('vhost utils', function () {
});
});
});
describe('getContentPath', function () {
it('should return the correct path for type: public', function () {
const publicPath = configUtils.config.getContentPath('public');
// Path should be in the tmpdir
const tmpdir = os.tmpdir();
publicPath.startsWith(tmpdir).should.be.true();
// Path should end with /public/
publicPath.endsWith('/public/').should.be.true();
// Path should include /ghost_
publicPath.includes('/ghost_').should.be.true();
// Path should contain a uuid at the correct location
const publicPathParts = publicPath.split('/');
const uuidPart = publicPathParts[publicPathParts.length - 3].replace('ghost_', '');
UUID_REGEX.test(uuidPart).should.be.true();
// Path should be memoized
configUtils.config.getContentPath('public').should.eql(publicPath);
});
});