mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-13 03:13:47 +00:00
Project files tweaks (#679)
* chore: fixed issue when loading first time; deleting excel file after upload; only json files from project list * chore: added last loaded project to endpoint response * chore: incremental file name --------- Co-authored-by: Name <Email>
This commit is contained in:
@@ -5,8 +5,8 @@ import type {
|
||||
GetInfo,
|
||||
HttpSettings,
|
||||
ProjectData,
|
||||
ProjectFileList,
|
||||
ErrorResponse,
|
||||
ProjectFileListResponse,
|
||||
} from 'ontime-types';
|
||||
|
||||
import { RequestHandler, Request, Response } from 'express';
|
||||
@@ -19,7 +19,7 @@ import { DataProvider } from '../classes/data-provider/DataProvider.js';
|
||||
import { failEmptyObjects, failIsNotArray } from '../utils/routerUtils.js';
|
||||
import { PlaybackService } from '../services/PlaybackService.js';
|
||||
import { eventStore } from '../stores/EventStore.js';
|
||||
import { getAppDataPath, isDocker, resolveDbPath, resolveStylesPath } from '../setup.js';
|
||||
import { getAppDataPath, isDocker, lastLoadedProjectConfigPath, resolveDbPath, resolveStylesPath } from '../setup.js';
|
||||
import { oscIntegration } from '../services/integration-service/OscIntegration.js';
|
||||
import { httpIntegration } from '../services/integration-service/HttpIntegration.js';
|
||||
import { logger } from '../classes/Logger.js';
|
||||
@@ -28,7 +28,7 @@ import { deepmerge } from 'ontime-utils';
|
||||
import { runtimeCacheStore } from '../stores/cachingStore.js';
|
||||
import { delayedRundownCacheKey } from '../services/rundown-service/delayedRundown.utils.js';
|
||||
import { integrationService } from '../services/integration-service/IntegrationService.js';
|
||||
import { getFileListFromFolder } from '../utils/getFileListFromFolder.js';
|
||||
import { getProjectFiles } from '../utils/getFileListFromFolder.js';
|
||||
|
||||
// Create controller for GET request to '/ontime/poll'
|
||||
// Returns data for current state
|
||||
@@ -471,11 +471,16 @@ export const postNew: RequestHandler = async (req, res) => {
|
||||
* @param req
|
||||
* @param res
|
||||
*/
|
||||
export const listProjects: RequestHandler = (_, res: Response<ProjectFileList | ErrorResponse>) => {
|
||||
export const listProjects: RequestHandler = async (_, res: Response<ProjectFileListResponse | ErrorResponse>) => {
|
||||
try {
|
||||
const uploadsFolderPath = join(getAppDataPath(), 'uploads');
|
||||
const fileList = getFileListFromFolder(uploadsFolderPath);
|
||||
res.status(200).send(fileList);
|
||||
const fileList = await getProjectFiles();
|
||||
|
||||
const lastLoadedProject = JSON.parse(fs.readFileSync(lastLoadedProjectConfigPath, 'utf8')).lastLoadedProject;
|
||||
|
||||
res.status(200).send({
|
||||
files: fileList,
|
||||
lastLoadedProject,
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(500).send({ message: error.toString() });
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ import path, { dirname, join } from 'path';
|
||||
import fs from 'fs';
|
||||
|
||||
import { config } from './config/config.js';
|
||||
import { ensureDirectory } from './utils/fileManagement.js';
|
||||
|
||||
// =================================================
|
||||
// resolve public path
|
||||
@@ -71,7 +72,7 @@ export const currentDirectory = dirname(__dirname);
|
||||
const testDbStartDirectory = isTest ? '../' : getAppDataPath();
|
||||
export const externalsStartDirectory = isProduction ? getAppDataPath() : join(currentDirectory, 'external');
|
||||
|
||||
const lastLoadedProjectConfigPath = join(getAppDataPath(), 'config.json');
|
||||
export const lastLoadedProjectConfigPath = join(getAppDataPath(), 'config.json');
|
||||
|
||||
let lastLoadedProject;
|
||||
|
||||
@@ -79,7 +80,8 @@ try {
|
||||
lastLoadedProject = JSON.parse(fs.readFileSync(lastLoadedProjectConfigPath, 'utf8')).lastLoadedProject;
|
||||
} catch {
|
||||
if (!isTest) {
|
||||
fs.writeFileSync(lastLoadedProjectConfigPath, JSON.stringify({ lastLoadedProject: null }));
|
||||
ensureDirectory(getAppDataPath());
|
||||
fs.writeFileSync(lastLoadedProjectConfigPath, JSON.stringify({ lastLoadedProject: 'default.json' }));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
import { expect, vi } from 'vitest';
|
||||
import { getProjectFiles } from '../getFileListFromFolder.js';
|
||||
|
||||
vi.mock('fs/promises', () => {
|
||||
const mockFiles = ['file1.json', 'file2.json', 'file3.json', 'document.txt', 'image.png'];
|
||||
const mockStats = {
|
||||
birthtime: new Date('2020-01-01'),
|
||||
mtime: new Date('2021-01-01'),
|
||||
};
|
||||
|
||||
return {
|
||||
readdir: vi.fn().mockResolvedValue(mockFiles),
|
||||
stat: vi.fn().mockResolvedValue(mockStats),
|
||||
};
|
||||
});
|
||||
|
||||
describe('getProjectFiles test', () => {
|
||||
it('should return a list of project .json files', async () => {
|
||||
const { readdir, stat } = await import('fs/promises');
|
||||
|
||||
const result = await getProjectFiles();
|
||||
|
||||
const expectedFiles = ['file1.json', 'file2.json', 'file3.json'].map((file) => ({
|
||||
filename: file,
|
||||
createdAt: new Date('2020-01-01').toISOString(),
|
||||
updatedAt: new Date('2021-01-01').toISOString(),
|
||||
}));
|
||||
|
||||
expect(result).toEqual(expectedFiles);
|
||||
expect(readdir).toHaveBeenCalled();
|
||||
expect(stat).toHaveBeenCalledTimes(expectedFiles.length);
|
||||
});
|
||||
});
|
||||
@@ -1,17 +1,55 @@
|
||||
import { ProjectFile } from 'ontime-types';
|
||||
|
||||
import { readdirSync, statSync } from 'fs';
|
||||
import { getAppDataPath } from '../setup.js';
|
||||
|
||||
export const getFileListFromFolder = (folderPath: string): Array<ProjectFile> => {
|
||||
const files = readdirSync(folderPath);
|
||||
return files.map((file) => {
|
||||
const filePath = `${folderPath}/${file}`;
|
||||
const stats = statSync(filePath);
|
||||
import { extname, join } from 'path';
|
||||
import { readdir, stat } from 'fs/promises';
|
||||
|
||||
return {
|
||||
filename: file,
|
||||
createdAt: stats.birthtime.toISOString(),
|
||||
updatedAt: stats.mtime.toISOString(),
|
||||
};
|
||||
const getFilesFromFolder = async (folderPath: string) => {
|
||||
return await readdir(folderPath);
|
||||
};
|
||||
|
||||
const filterProjectFiles = (files: Array<string>): Array<string> => {
|
||||
return files.filter((file) => {
|
||||
const ext = extname(file).toLowerCase();
|
||||
return ext === '.json';
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Asynchronously retrieves and returns an array of project files from the 'uploads' folder.
|
||||
* Each file in the 'uploads' folder is checked, and only those with a '.json' extension are processed.
|
||||
* For each qualifying file, its metadata is retrieved, including filename, creation time, and last modification time.
|
||||
*
|
||||
* @returns {Promise<Array<ProjectFile>>} A promise that resolves to an array of ProjectFile objects,
|
||||
* each representing a file in the 'uploads' folder with its metadata.
|
||||
* The metadata includes the filename, creation time (createdAt),
|
||||
* and last modification time (updatedAt) of each file.
|
||||
*
|
||||
* @throws {Error} Throws an error if there is an issue in reading the directory or fetching file statistics.
|
||||
*/
|
||||
export const getProjectFiles = async (): Promise<ProjectFile[]> => {
|
||||
const uploadsFolderPath = join(getAppDataPath(), 'uploads');
|
||||
|
||||
try {
|
||||
const allFiles = await getFilesFromFolder(uploadsFolderPath);
|
||||
const filteredFiles = filterProjectFiles(allFiles);
|
||||
|
||||
const projectFiles = [];
|
||||
for (const file of filteredFiles) {
|
||||
const filePath = join(uploadsFolderPath, file);
|
||||
const stats = await stat(filePath);
|
||||
|
||||
projectFiles.push({
|
||||
filename: file,
|
||||
createdAt: stats.birthtime.toISOString(),
|
||||
updatedAt: stats.mtime.toISOString(),
|
||||
});
|
||||
}
|
||||
|
||||
return projectFiles;
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
throw err;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -25,7 +25,7 @@ import path from 'path';
|
||||
|
||||
import { event as eventDef } from '../models/eventsDefinition.js';
|
||||
import { dbModel } from '../models/dataModel.js';
|
||||
import { makeString } from './parserUtils.js';
|
||||
import { deleteFile, makeString } from './parserUtils.js';
|
||||
import {
|
||||
parseAliases,
|
||||
parseProject,
|
||||
@@ -379,7 +379,7 @@ export const fileHandler = async (file: string, options: ExcelImportOptions): Pr
|
||||
res.data.project = parseProject(dataFromExcel);
|
||||
res.data.userFields = parseUserFields(dataFromExcel);
|
||||
|
||||
await configService.updateDatabaseConfig(fileName);
|
||||
await deleteFile(file);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import multer from 'multer';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
|
||||
import { EXCEL_MIME, JSON_MIME } from './parser.js';
|
||||
import { ensureDirectory } from './fileManagement.js';
|
||||
@@ -8,17 +9,26 @@ import { getAppDataPath } from '../setup.js';
|
||||
// Define multer storage object
|
||||
const storage = multer.diskStorage({
|
||||
destination: function (req, file, cb) {
|
||||
// get platform path
|
||||
const appDataPath = getAppDataPath();
|
||||
if (appDataPath === '') {
|
||||
throw new Error('Could not resolve public folder for platform');
|
||||
}
|
||||
// append uploads folder
|
||||
const newDestination = path.join(appDataPath, 'uploads');
|
||||
|
||||
// Create directory if not exist
|
||||
ensureDirectory(newDestination);
|
||||
cb(null, newDestination);
|
||||
const uploadsPath = path.join(appDataPath, 'uploads');
|
||||
ensureDirectory(uploadsPath);
|
||||
|
||||
const filePath = path.join(uploadsPath, 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, uploadsPath);
|
||||
} else {
|
||||
// File already exists, handle error
|
||||
return cb(new Error('File already exists'), false);
|
||||
}
|
||||
});
|
||||
},
|
||||
filename: function (_, file, cb) {
|
||||
cb(null, file.originalname);
|
||||
|
||||
Reference in New Issue
Block a user