Поддержка скачивания нескольких файлов, с правильным неймингом: файл, файл (1), файл (2). Таким образом удалось избежать перезаписи существующих файлов с таким же названием
This commit is contained in:
@@ -9,5 +9,15 @@ export function useFileStorage() {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
return {writeFile, readFile};
|
const fileExists = async (file : string, inWorkingDir : boolean = true) => {
|
||||||
|
const result = await window.electron.ipcRenderer.invoke('fileStorage:fileExists', file, inWorkingDir);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
const size = async (file : string, inWorkingDir : boolean = true) => {
|
||||||
|
const result = await window.electron.ipcRenderer.invoke('fileStorage:size', file, inWorkingDir);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {writeFile, readFile, fileExists, size};
|
||||||
}
|
}
|
||||||
@@ -30,7 +30,7 @@ export function useAttachment(attachment: Attachment, keyPlain: string) {
|
|||||||
const downloadPercentage = useDownloadStatus(attachment.id);
|
const downloadPercentage = useDownloadStatus(attachment.id);
|
||||||
const [downloadStatus, setDownloadStatus] = useMemory("attachment-downloaded-status-" + attachment.id, DownloadStatus.PENDING, true);
|
const [downloadStatus, setDownloadStatus] = useMemory("attachment-downloaded-status-" + attachment.id, DownloadStatus.PENDING, true);
|
||||||
const [downloadTag, setDownloadTag] = useState("");
|
const [downloadTag, setDownloadTag] = useState("");
|
||||||
const {readFile, writeFile} = useFileStorage();
|
const {readFile, writeFile, fileExists, size} = useFileStorage();
|
||||||
const { downloadFile } = useTransport();
|
const { downloadFile } = useTransport();
|
||||||
const publicKey = usePublicKey();
|
const publicKey = usePublicKey();
|
||||||
const privatePlain = usePrivatePlain();
|
const privatePlain = usePrivatePlain();
|
||||||
@@ -83,10 +83,12 @@ export function useAttachment(attachment: Attachment, keyPlain: string) {
|
|||||||
* а в загрузках
|
* а в загрузках
|
||||||
*/
|
*/
|
||||||
const preview = getPreview();
|
const preview = getPreview();
|
||||||
|
const filesize = parseInt(preview.split("::")[0]);
|
||||||
const filename = preview.split("::")[1];
|
const filename = preview.split("::")[1];
|
||||||
let pathInDownloads = window.downloadsPath + "/Rosetta Downloads/" + filename;
|
let pathInDownloads = window.downloadsPath + "/Rosetta Downloads/" + filename;
|
||||||
const fileData = await readFile(pathInDownloads, false);
|
const exists = await fileExists(pathInDownloads, false);
|
||||||
if(fileData){
|
const existsLength = await size(pathInDownloads, false);
|
||||||
|
if(exists && existsLength == filesize){
|
||||||
setDownloadStatus(DownloadStatus.DOWNLOADED);
|
setDownloadStatus(DownloadStatus.DOWNLOADED);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -150,7 +152,6 @@ export function useAttachment(attachment: Attachment, keyPlain: string) {
|
|||||||
}
|
}
|
||||||
setDownloadStatus(DownloadStatus.DECRYPTING);
|
setDownloadStatus(DownloadStatus.DECRYPTING);
|
||||||
//console.info("Decrypted attachment ", Buffer.from(keyPlain, 'binary').toString('hex'));
|
//console.info("Decrypted attachment ", Buffer.from(keyPlain, 'binary').toString('hex'));
|
||||||
console.info("KP", keyPlain);
|
|
||||||
const decrypted = await decodeWithPassword(keyPlain, downloadedBlob);
|
const decrypted = await decodeWithPassword(keyPlain, downloadedBlob);
|
||||||
setDownloadTag("");
|
setDownloadTag("");
|
||||||
if(attachment.type == AttachmentType.FILE) {
|
if(attachment.type == AttachmentType.FILE) {
|
||||||
@@ -162,7 +163,17 @@ export function useAttachment(attachment: Attachment, keyPlain: string) {
|
|||||||
const filename = preview.split("::")[1];
|
const filename = preview.split("::")[1];
|
||||||
let buffer = Buffer.from(decrypted.split(",")[1], 'base64');
|
let buffer = Buffer.from(decrypted.split(",")[1], 'base64');
|
||||||
let pathInDownloads = window.downloadsPath + "/Rosetta Downloads/" + filename;
|
let pathInDownloads = window.downloadsPath + "/Rosetta Downloads/" + filename;
|
||||||
await writeFile(pathInDownloads, buffer, false);
|
/**
|
||||||
|
* Пишем файл в загрузки, но перед этим выбираем ему название, если файл в загрузках
|
||||||
|
* уже есть с таким названием то добавляем к названию (1), (2) и так далее, чтобы не перезаписать существующий файл
|
||||||
|
*/
|
||||||
|
let finalPath = pathInDownloads;
|
||||||
|
let fileIndex = 1;
|
||||||
|
while (await fileExists(finalPath, false)) {
|
||||||
|
finalPath = window.downloadsPath + "/Rosetta Downloads/" + filename.split(".").slice(0, -1).join(".") + ` (${fileIndex})` + "." + filename.split(".").slice(-1);
|
||||||
|
fileIndex++;
|
||||||
|
}
|
||||||
|
await writeFile(finalPath, buffer, false);
|
||||||
setDownloadStatus(DownloadStatus.DOWNLOADED);
|
setDownloadStatus(DownloadStatus.DOWNLOADED);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,16 +2,20 @@ import { ipcMain } from "electron";
|
|||||||
import { WORKING_DIR } from "../constants";
|
import { WORKING_DIR } from "../constants";
|
||||||
import fs from 'fs/promises'
|
import fs from 'fs/promises'
|
||||||
import path from 'path'
|
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) => {
|
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);
|
const fullPath = path.join(inWorkingDir ? WORKING_DIR : '', file);
|
||||||
await fs.mkdir(path.dirname(fullPath), { recursive: true });
|
await fs.mkdir(path.dirname(fullPath), { recursive: true });
|
||||||
await fs.writeFile(fullPath, data);
|
await fs.writeFile(fullPath, data);
|
||||||
console.info("File written to " + fullPath);
|
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
|
|
||||||
ipcMain.handle('fileStorage:readFile', async (_, file: string, inWorkingDir : boolean = true) => {
|
ipcMain.handle('fileStorage:readFile', async (_, file: string, inWorkingDir : boolean = true) => {
|
||||||
|
logger.log(`System call fileStorage:readFile with file=${file} inWorkingDir=${inWorkingDir}`);
|
||||||
try{
|
try{
|
||||||
const fullPath = path.join(inWorkingDir ? WORKING_DIR : '', file);
|
const fullPath = path.join(inWorkingDir ? WORKING_DIR : '', file);
|
||||||
const data = await fs.readFile(fullPath);
|
const data = await fs.readFile(fullPath);
|
||||||
@@ -20,3 +24,25 @@ ipcMain.handle('fileStorage:readFile', async (_, file: string, inWorkingDir : bo
|
|||||||
return null;
|
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