mirror of
https://github.com/cpvalente/ontime.git
synced 2026-07-26 10:38:55 +00:00
Several project files user folder (#617)
* feat: save loaded file across sessions
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
import { Low } from 'lowdb';
|
||||
import { JSONFile } from 'lowdb/node';
|
||||
import { join } from 'path';
|
||||
|
||||
import { getAppDataPath } from '../setup.js';
|
||||
|
||||
interface Config {
|
||||
lastLoadedProject: string;
|
||||
}
|
||||
|
||||
class ConfigService {
|
||||
private config: Low<Config>;
|
||||
private configPath: string;
|
||||
|
||||
constructor() {
|
||||
this.configPath = join(getAppDataPath(), 'config.json');
|
||||
const adapter = new JSONFile<Config>(this.configPath);
|
||||
this.config = new Low<Config>(adapter);
|
||||
|
||||
this.init();
|
||||
}
|
||||
|
||||
private async init() {
|
||||
await this.config.read();
|
||||
await this.config.write();
|
||||
}
|
||||
|
||||
async getConfig(): Promise<Config> {
|
||||
await this.config.read();
|
||||
return this.config.data;
|
||||
}
|
||||
|
||||
async updateDatabaseConfig(filename: string): Promise<void> {
|
||||
if (process.env.IS_TEST) return;
|
||||
|
||||
this.config.data.lastLoadedProject = filename;
|
||||
await this.config.write();
|
||||
}
|
||||
}
|
||||
|
||||
export const configService = new ConfigService();
|
||||
@@ -1,5 +1,7 @@
|
||||
import { fileURLToPath } from 'url';
|
||||
import path, { dirname, join } from 'path';
|
||||
import fs from 'fs';
|
||||
|
||||
import { config } from './config/config.js';
|
||||
|
||||
// =================================================
|
||||
@@ -69,12 +71,23 @@ export const currentDirectory = dirname(__dirname);
|
||||
const testDbStartDirectory = isTest ? '../' : getAppDataPath();
|
||||
export const externalsStartDirectory = isProduction ? getAppDataPath() : join(currentDirectory, 'external');
|
||||
|
||||
const lastLoadedProjectConfigPath = join(getAppDataPath(), 'config.json');
|
||||
|
||||
let lastLoadedProject;
|
||||
|
||||
try {
|
||||
lastLoadedProject = JSON.parse(fs.readFileSync(lastLoadedProjectConfigPath, 'utf8')).lastLoadedProject;
|
||||
} catch {
|
||||
if (!isTest) {
|
||||
fs.writeFileSync(lastLoadedProjectConfigPath, JSON.stringify({ lastLoadedProject: null }));
|
||||
}
|
||||
}
|
||||
|
||||
const configDbDirectory = lastLoadedProject ? 'uploads' : config.database.directory;
|
||||
|
||||
// path to public db
|
||||
export const resolveDbDirectory = join(
|
||||
testDbStartDirectory,
|
||||
isTest ? config.database.testdb : config.database.directory,
|
||||
);
|
||||
export const resolveDbPath = join(resolveDbDirectory, config.database.filename);
|
||||
export const resolveDbDirectory = join(testDbStartDirectory, isTest ? config.database.testdb : configDbDirectory);
|
||||
export const resolveDbPath = join(resolveDbDirectory, lastLoadedProject ? lastLoadedProject : config.database.filename);
|
||||
|
||||
export const pathToStartDb = isTest
|
||||
? join(currentDirectory, '../', config.database.testdb, config.database.filename)
|
||||
|
||||
@@ -21,10 +21,11 @@ import {
|
||||
|
||||
import fs from 'fs';
|
||||
import xlsx from 'node-xlsx';
|
||||
import path from 'path';
|
||||
|
||||
import { event as eventDef } from '../models/eventsDefinition.js';
|
||||
import { dbModel } from '../models/dataModel.js';
|
||||
import { deleteFile, makeString } from './parserUtils.js';
|
||||
import { makeString } from './parserUtils.js';
|
||||
import {
|
||||
parseAliases,
|
||||
parseProject,
|
||||
@@ -36,6 +37,7 @@ import {
|
||||
parseViewSettings,
|
||||
} from './parserFunctions.js';
|
||||
import { parseExcelDate } from './time.js';
|
||||
import { configService } from '../services/ConfigService.js';
|
||||
|
||||
export const EXCEL_MIME = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
|
||||
export const JSON_MIME = 'application/json';
|
||||
@@ -350,6 +352,8 @@ type ResponseOK = {
|
||||
export const fileHandler = async (file: string, options: ExcelImportOptions): Promise<Partial<ResponseOK>> => {
|
||||
const res: Partial<ResponseOK> = {};
|
||||
|
||||
const fileName = path.basename(file);
|
||||
|
||||
// check which file type are we dealing with
|
||||
if (file.endsWith('.xlsx')) {
|
||||
// we need to check that the options are applicable
|
||||
@@ -374,6 +378,9 @@ export const fileHandler = async (file: string, options: ExcelImportOptions): Pr
|
||||
}
|
||||
res.data.project = parseProject(dataFromExcel);
|
||||
res.data.userFields = parseUserFields(dataFromExcel);
|
||||
|
||||
await configService.updateDatabaseConfig(fileName);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -385,8 +392,8 @@ export const fileHandler = async (file: string, options: ExcelImportOptions): Pr
|
||||
uploadedJson = JSON.parse(rawdata);
|
||||
res.data = await parseJson(uploadedJson);
|
||||
|
||||
// delete file
|
||||
await deleteFile(file);
|
||||
await configService.updateDatabaseConfig(fileName);
|
||||
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -20,8 +20,8 @@ const storage = multer.diskStorage({
|
||||
ensureDirectory(newDestination);
|
||||
cb(null, newDestination);
|
||||
},
|
||||
filename: function (req, file, cb) {
|
||||
cb(null, `${Date.now()}--${file.originalname}`);
|
||||
filename: function (_, file, cb) {
|
||||
cb(null, file.originalname);
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user