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