fix: fix logo fetching raising errors in server

This commit is contained in:
timothycarambat 2023-12-06 11:56:07 -08:00
parent ce52f54cda
commit 79cdb8631a

View File

@ -15,7 +15,9 @@ function getDefaultFilename() {
async function determineLogoFilepath(defaultFilename = LOGO_FILENAME) {
const currentLogoFilename = await SystemSettings.currentLogoFilename();
const basePath = path.join(__dirname, "../../storage/assets");
const basePath = process.env.STORAGE_DIR
? path.join(process.env.STORAGE_DIR, "assets")
: path.join(__dirname, "../../storage/assets");
const defaultFilepath = path.join(basePath, defaultFilename);
if (currentLogoFilename && validFilename(currentLogoFilename)) {
@ -27,6 +29,15 @@ async function determineLogoFilepath(defaultFilename = LOGO_FILENAME) {
}
function fetchLogo(logoPath) {
if (!fs.existsSync(logoPath)) {
return {
found: false,
buffer: null,
size: 0,
mime: "none/none",
};
}
const mime = getType(logoPath);
const buffer = fs.readFileSync(logoPath);
return {
@ -39,14 +50,12 @@ function fetchLogo(logoPath) {
async function renameLogoFile(originalFilename = null) {
const extname = path.extname(originalFilename) || ".png";
const newFilename = `${v4()}${extname}`;
const originalFilepath = path.join(
__dirname,
`../../storage/assets/${originalFilename}`
);
const outputFilepath = path.join(
__dirname,
`../../storage/assets/${newFilename}`
);
const originalFilepath = process.env.STORAGE_DIR
? path.join(process.env.STORAGE_DIR, "assets", originalFilename)
: path.join(__dirname, `../../storage/assets/${originalFilename}`);
const outputFilepath = process.env.STORAGE_DIR
? path.join(process.env.STORAGE_DIR, "assets", newFilename)
: path.join(__dirname, `../../storage/assets/${newFilename}`);
fs.renameSync(originalFilepath, outputFilepath);
return newFilename;
@ -54,7 +63,9 @@ async function renameLogoFile(originalFilename = null) {
async function removeCustomLogo(logoFilename = LOGO_FILENAME) {
if (!logoFilename || !validFilename(logoFilename)) return false;
const logoPath = path.join(__dirname, `../../storage/assets/${logoFilename}`);
const logoPath = process.env.STORAGE_DIR
? path.join(process.env.STORAGE_DIR, `assets/${logoFilename}`)
: path.join(__dirname, `../../storage/assets/${logoFilename}`);
if (fs.existsSync(logoPath)) fs.unlinkSync(logoPath);
return true;
}