Поддержка скачивания нескольких файлов, с правильным неймингом: файл, файл (1), файл (2). Таким образом удалось избежать перезаписи существующих файлов с таким же названием
This commit is contained in:
@@ -2,16 +2,20 @@ 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);
|
||||
console.info("File written to " + fullPath);
|
||||
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);
|
||||
@@ -19,4 +23,26 @@ ipcMain.handle('fileStorage:readFile', async (_, file: string, inWorkingDir : bo
|
||||
}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;
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user