Project manager (#697)

* Project manager: Load, Rename, Duplicate (#712)

* Project Manager: Create, Delete (#723)
This commit is contained in:
Ary
2024-01-29 06:06:33 -07:00
committed by GitHub
parent 4c77d26a71
commit 3c83f4d15b
16 changed files with 473 additions and 69 deletions
@@ -42,6 +42,7 @@ import { deleteFile } from '../utils/parserUtils.js';
import { validateProjectFiles } from './ontimeController.validate.js';
import { dbModel } from '../models/dataModel.js';
import { sheet } from '../utils/sheetsAuth.js';
import { removeFileExtension } from '../utils/removeFileExtension.js';
// Create controller for GET request to '/ontime/poll'
// Returns data for current state
@@ -491,9 +492,11 @@ export const listProjects: RequestHandler = async (_, res: Response<ProjectFileL
const lastLoadedProject = JSON.parse(fs.readFileSync(lastLoadedProjectConfigPath, 'utf8')).lastLoadedProject;
const lastLoadedProjectName = removeFileExtension(lastLoadedProject);
res.status(200).send({
files: fileList,
lastLoadedProject,
lastLoadedProject: lastLoadedProjectName,
});
} catch (error) {
res.status(500).send({ message: error.toString() });
+1 -1
View File
@@ -122,7 +122,7 @@ router.post('/new', projectSanitiser, postNew);
router.get('/projects', listProjects);
// create route between controller and '/ontime/load-project' endpoint
router.post('/load-project', validateLoadProjectFile, loadProject);
router.post('/load-project', validateLoadProjectFile, sanitizeProjectFilename, loadProject);
// create route between controller and '/ontime/project/:filename/duplicate' endpoint
router.post('/project/:filename/duplicate', validateProjectDuplicate, sanitizeProjectFilename, duplicateProjectFile);
@@ -20,7 +20,7 @@ describe('getProjectFiles test', () => {
const result = await getProjectFiles();
const expectedFiles = ['file1.json', 'file2.json', 'file3.json'].map((file) => ({
const expectedFiles = ['file1', 'file2', 'file3'].map((file) => ({
filename: file,
createdAt: new Date('2020-01-01').toISOString(),
updatedAt: new Date('2021-01-01').toISOString(),
@@ -4,6 +4,7 @@ import { getAppDataPath } from '../setup.js';
import { extname, join } from 'path';
import { readdir, stat } from 'fs/promises';
import { removeFileExtension } from './removeFileExtension.js';
const getFilesFromFolder = async (folderPath: string) => {
return await readdir(folderPath);
@@ -41,7 +42,7 @@ export const getProjectFiles = async (): Promise<ProjectFile[]> => {
const stats = await stat(filePath);
projectFiles.push({
filename: file,
filename: removeFileExtension(file),
createdAt: stats.birthtime.toISOString(),
updatedAt: stats.mtime.toISOString(),
});
@@ -0,0 +1,9 @@
import { parse } from 'path';
/**
* @description Takes a filename and removes the extension
* @param {string} filename - filename with extension
*/
export const removeFileExtension = (filename: string): string => {
return parse(filename).name;
};
+26 -2
View File
@@ -6,6 +6,28 @@ import { EXCEL_MIME, JSON_MIME } from './parser.js';
import { ensureDirectory } from './fileManagement.js';
import { getAppDataPath } from '../setup.js';
function generateNewFileName(filePath, callback) {
let baseName = path.basename(filePath, path.extname(filePath));
let extension = path.extname(filePath);
let counter = 1;
const checkExistence = (newPath) => {
fs.access(newPath, fs.constants.F_OK, (err) => {
if (err) {
// File with the new name does not exist, use this name
callback(path.basename(newPath));
} else {
// File exists, increment the counter and try again
newPath = path.join(path.dirname(filePath), `${baseName} (${++counter})${extension}`);
checkExistence(newPath);
}
});
};
let newPath = path.join(path.dirname(filePath), `${baseName} (${counter})${extension}`);
checkExistence(newPath);
}
// Define multer storage object
const storage = multer.diskStorage({
destination: function (req, file, cb) {
@@ -25,8 +47,10 @@ const storage = multer.diskStorage({
// 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);
generateNewFileName(filePath, (newName) => {
file.originalname = newName;
cb(null, uploadsPath);
});
}
});
},