Skip to content

Commit

Permalink
fix: fix logo fetching raising errors in server
Browse files Browse the repository at this point in the history
  • Loading branch information
timothycarambat committed Dec 6, 2023
1 parent ce52f54 commit 79cdb86
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions server/utils/files/logo.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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 {
Expand All @@ -39,22 +50,22 @@ 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;
}

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;
}
Expand Down

0 comments on commit 79cdb86

Please sign in to comment.