mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-14 20:03:52 +00:00
refactor: simplify setup of asset paths
This commit is contained in:
committed by
Carlos Valente
parent
e46948772e
commit
5950551da2
@@ -1,4 +1,4 @@
|
||||
import { isProduction } from '../setup/index.js';
|
||||
import { isProduction } from '../externals.js';
|
||||
import { consoleError } from '../utils/console.js';
|
||||
|
||||
/**
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user