refactor: simplify setup of asset paths

This commit is contained in:
Carlos Valente
2024-10-13 13:53:55 +02:00
committed by Carlos Valente
parent e46948772e
commit 5950551da2
17 changed files with 177 additions and 144 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
import { isProduction } from '../setup/index.js';
import { isProduction } from '../externals.js';
import { consoleError } from '../utils/console.js';
/**
+22 -2
View File
@@ -1,5 +1,5 @@
import { existsSync, mkdirSync } from 'fs';
import { readdir } from 'fs/promises';
import { readdir, copyFile } from 'fs/promises';
import { basename, extname, join, parse } from 'path';
/**
@@ -80,8 +80,28 @@ export function getFileNameFromPath(filePath: string): string {
}
/**
* Utility naivly checks for paths on whether it includes directories
* Utility naively checks for paths on whether it includes directories
*/
export function isPath(filePath: string): boolean {
return filePath !== basename(filePath);
}
/**
* Recursively copies a directory and its contents.
* @param {string} src - The source directory.
* @param {string} dest - The destination directory.
*/
export async function copyDirectory(src: string, dest: string) {
const entries = await readdir(src, { withFileTypes: true });
for (const entry of entries) {
const srcPath = join(src, entry.name);
const destPath = join(dest, entry.name);
if (entry.isDirectory()) {
await copyDirectory(srcPath, destPath);
} else {
await copyFile(srcPath, destPath);
}
}
}
+3 -3
View File
@@ -4,7 +4,7 @@ import { join } from 'path';
import { ONTIME_VERSION } from '../ONTIME_VERSION.js';
import { get } from '../services/rundown-service/rundownCache.js';
import { getState } from '../stores/runtimeState.js';
import { resolveCrashReportDirectory } from '../setup/index.js';
import { publicDir } from '../setup/index.js';
import { ensureDirectory } from './fileManagement.js';
/**
@@ -13,8 +13,8 @@ import { ensureDirectory } from './fileManagement.js';
* @param content
*/
function writeToFile(fileName: string, content: object) {
const path = join(resolveCrashReportDirectory, fileName);
ensureDirectory(resolveCrashReportDirectory);
const path = join(publicDir.crashDir, fileName);
ensureDirectory(publicDir.crashDir);
try {
const textContent = JSON.stringify(content, null, 2);
+7 -6
View File
@@ -3,8 +3,9 @@ import path from 'path';
import fs from 'fs';
import { rm } from 'fs/promises';
import { getAppDataPath, publicDir } from '../setup/index.js';
import { ensureDirectory } from './fileManagement.js';
import { getAppDataPath, uploadsFolderPath } from '../setup/index.js';
function generateNewFileName(filePath: string, callback: (newName: string) => void) {
const baseName = path.basename(filePath, path.extname(filePath));
@@ -36,19 +37,19 @@ export const storage = multer.diskStorage({
throw new Error('Could not resolve public folder for platform');
}
ensureDirectory(uploadsFolderPath);
ensureDirectory(publicDir.uploadsDir);
const filePath = path.join(uploadsFolderPath, file.originalname);
const filePath = path.join(publicDir.uploadsDir, file.originalname);
// Check if file already exists
fs.access(filePath, fs.constants.F_OK, (err) => {
if (err) {
// File does not exist, can safely proceed to this destination
cb(null, uploadsFolderPath);
cb(null, publicDir.uploadsDir);
} else {
generateNewFileName(filePath, (newName) => {
file.originalname = newName;
cb(null, uploadsFolderPath);
cb(null, publicDir.uploadsDir);
});
}
});
@@ -63,7 +64,7 @@ export const storage = multer.diskStorage({
*/
export async function clearUploadfolder() {
try {
await rm(uploadsFolderPath, { recursive: true });
await rm(publicDir.uploadsDir, { recursive: true });
} catch (_) {
// we dont care that there was no folder
}