48 lines
1.8 KiB
TypeScript
48 lines
1.8 KiB
TypeScript
import { ipcMain } from "electron";
|
|
import { WORKING_DIR } from "../constants";
|
|
import fs from 'fs/promises'
|
|
import path from 'path'
|
|
import { Logger } from "../logger";
|
|
|
|
const logger = Logger('ipcFilestorage');
|
|
|
|
ipcMain.handle('fileStorage:writeFile', async (_, file: string, data: string | Buffer, inWorkingDir : boolean = true) => {
|
|
logger.log(`System call fileStorage:writeFile with file=${file} inWorkingDir=${inWorkingDir}`);
|
|
const fullPath = path.join(inWorkingDir ? WORKING_DIR : '', file);
|
|
await fs.mkdir(path.dirname(fullPath), { recursive: true });
|
|
await fs.writeFile(fullPath, data);
|
|
return true;
|
|
});
|
|
|
|
ipcMain.handle('fileStorage:readFile', async (_, file: string, inWorkingDir : boolean = true) => {
|
|
logger.log(`System call fileStorage:readFile with file=${file} inWorkingDir=${inWorkingDir}`);
|
|
try{
|
|
const fullPath = path.join(inWorkingDir ? WORKING_DIR : '', file);
|
|
const data = await fs.readFile(fullPath);
|
|
return data;
|
|
}catch(e){
|
|
return null;
|
|
}
|
|
});
|
|
|
|
ipcMain.handle('fileStorage:size', async (_, file: string, inWorkingDir : boolean = true) => {
|
|
logger.log(`System call fileStorage:size with file=${file} inWorkingDir=${inWorkingDir}`);
|
|
try{
|
|
const fullPath = path.join(inWorkingDir ? WORKING_DIR : '', file);
|
|
const stats = await fs.stat(fullPath);
|
|
return stats.size;
|
|
}catch(e){
|
|
return 0;
|
|
}
|
|
});
|
|
|
|
ipcMain.handle('fileStorage:fileExists', async (_, file: string, inWorkingDir : boolean = true) => {
|
|
logger.log(`System call fileStorage:fileExists with file=${file} inWorkingDir=${inWorkingDir}`);
|
|
try{
|
|
const fullPath = path.join(inWorkingDir ? WORKING_DIR : '', file);
|
|
await fs.access(fullPath);
|
|
return true;
|
|
}catch(e){
|
|
return false;
|
|
}
|
|
}); |