Project Manager: Quick Start (#762)

This commit is contained in:
Ary
2024-02-10 06:30:33 -07:00
committed by GitHub
parent 7f78fac162
commit 12f81c63ce
11 changed files with 219 additions and 263 deletions
+26 -28
View File
@@ -32,7 +32,7 @@ import {
import { oscIntegration } from '../services/integration-service/OscIntegration.js';
import { httpIntegration } from '../services/integration-service/HttpIntegration.js';
import { logger } from '../classes/Logger.js';
import { deleteAllEvents, notifyChanges, setRundown } from '../services/rundown-service/RundownService.js';
import { notifyChanges, setRundown } from '../services/rundown-service/RundownService.js';
import { integrationService } from '../services/integration-service/IntegrationService.js';
import { getProjectFiles } from '../utils/getFileListFromFolder.js';
import { configService } from '../services/ConfigService.js';
@@ -41,6 +41,8 @@ import { validateProjectFiles } from './ontimeController.validate.js';
import { dbModel } from '../models/dataModel.js';
import { sheet } from '../utils/sheetsAuth.js';
import { removeFileExtension } from '../utils/removeFileExtension.js';
import { ensureJsonExtension } from '../utils/ensureJsonExtension.js';
import { generateUniqueFileName } from '../utils/generateUniqueFilename.js';
// Create controller for GET request to '/ontime/poll'
// Returns data for current state
@@ -460,29 +462,6 @@ export async function previewExcel(req, res: Response) {
}
}
/**
* Meant to create a new project file, it will clear only fields which are specific to a project
* @param req
* @param res
*/
export const postNew: RequestHandler = async (req, res) => {
try {
const newProjectData: ProjectData = {
title: req.body?.title ?? '',
description: req.body?.description ?? '',
publicUrl: req.body?.publicUrl ?? '',
publicInfo: req.body?.publicInfo ?? '',
backstageUrl: req.body?.backstageUrl ?? '',
backstageInfo: req.body?.backstageInfo ?? '',
};
const newData = await DataProvider.setProjectData(newProjectData);
await deleteAllEvents();
res.status(201).send(newData);
} catch (error) {
res.status(400).send({ message: String(error) });
}
};
/**
* Retrieves and lists all project files from the uploads directory.
* @param req
@@ -618,20 +597,39 @@ export const renameProjectFile: RequestHandler = async (req: Request, res: Respo
*/
export const createProjectFile: RequestHandler = async (req: Request, res: Response) => {
try {
const { filename } = req.body;
const originalFilename = ensureJsonExtension(req.body.title || 'Untitled');
const filename = generateUniqueFileName(uploadsFolderPath, originalFilename);
const projectFilePath = join(uploadsFolderPath, filename);
const errors = validateProjectFiles({ newFilename: filename });
const newProjectData: ProjectData = {
title: req.body?.title ?? '',
description: req.body?.description ?? '',
publicUrl: req.body?.publicUrl ?? '',
publicInfo: req.body?.publicInfo ?? '',
backstageUrl: req.body?.backstageUrl ?? '',
backstageInfo: req.body?.backstageInfo ?? '',
};
const data = {
...dbModel,
project: {
...dbModel.project,
...newProjectData,
},
};
if (errors.length) {
return res.status(409).send({ message: errors.join(', ') });
return res.status(409).send({ message: 'Project with title already exists' });
}
await writeFile(projectFilePath, JSON.stringify(dbModel));
await writeFile(projectFilePath, JSON.stringify(data));
await parseAndApply(projectFilePath, req, res, {});
res.status(200).send({
message: `Created project ${filename}`,
filename,
});
} catch (error) {
res.status(500).send({ message: String(error) });
+1 -6
View File
@@ -13,7 +13,6 @@ import {
patchPartialProjectFile,
poll,
postAliases,
postNew,
postOSC,
postOscSubscriptions,
postSettings,
@@ -48,7 +47,6 @@ import {
validateProjectDuplicate,
validateLoadProjectFile,
validateProjectRename,
validateProjectCreate,
validateSheetid,
validateWorksheet,
validateSheetOptions,
@@ -115,9 +113,6 @@ router.get('/http', getHTTP);
// create route between controller and '/ontime/http' endpoint
router.post('/http', validateHTTP, postHTTP);
// create route between controller and '/ontime/new' endpoint
router.post('/new', projectSanitiser, postNew);
// create route between controller and '/ontime/projects' endpoint
router.get('/projects', listProjects);
@@ -131,7 +126,7 @@ router.post('/project/:filename/duplicate', validateProjectDuplicate, sanitizePr
router.put('/project/:filename/rename', validateProjectRename, sanitizeProjectFilename, renameProjectFile);
// create route between controller and '/ontime/project' endpoint
router.post('/project', validateProjectCreate, sanitizeProjectFilename, createProjectFile);
router.post('/project', projectSanitiser, createProjectFile);
// create route between controller and '/ontime/project/:filename' endpoint
router.delete('/project/:filename', sanitizeProjectFilename, deleteProjectFile);
@@ -0,0 +1,26 @@
import { existsSync } from 'fs';
import path from 'path';
/**
* Generates a unique file name within the specified directory.
* If a file with the same name already exists, appends a counter to the filename.
*
* @param {string} directory - The directory to check for file existence.
* @param {string} filename - The original filename.
* @return {Promise<string>} A unique filename.
*/
export const generateUniqueFileName = (directory: string, filename: string) => {
const baseName = path.basename(filename, path.extname(filename));
const extension = path.extname(filename);
let counter = 0;
let uniqueFilename = filename;
while (existsSync(path.join(directory, uniqueFilename))) {
counter++;
// Append counter to filename if the file exists.
uniqueFilename = `${baseName} (${counter})${extension}`;
}
return uniqueFilename;
};