Поддержка скачивания нескольких файлов, с правильным неймингом: файл, файл (1), файл (2). Таким образом удалось избежать перезаписи существующих файлов с таким же названием

This commit is contained in:
RoyceDa
2026-02-17 18:27:24 +02:00
parent 89731b8c16
commit d309e011a3
3 changed files with 54 additions and 7 deletions

View File

@@ -30,7 +30,7 @@ export function useAttachment(attachment: Attachment, keyPlain: string) {
const downloadPercentage = useDownloadStatus(attachment.id);
const [downloadStatus, setDownloadStatus] = useMemory("attachment-downloaded-status-" + attachment.id, DownloadStatus.PENDING, true);
const [downloadTag, setDownloadTag] = useState("");
const {readFile, writeFile} = useFileStorage();
const {readFile, writeFile, fileExists, size} = useFileStorage();
const { downloadFile } = useTransport();
const publicKey = usePublicKey();
const privatePlain = usePrivatePlain();
@@ -83,10 +83,12 @@ export function useAttachment(attachment: Attachment, keyPlain: string) {
* а в загрузках
*/
const preview = getPreview();
const filesize = parseInt(preview.split("::")[0]);
const filename = preview.split("::")[1];
let pathInDownloads = window.downloadsPath + "/Rosetta Downloads/" + filename;
const fileData = await readFile(pathInDownloads, false);
if(fileData){
const exists = await fileExists(pathInDownloads, false);
const existsLength = await size(pathInDownloads, false);
if(exists && existsLength == filesize){
setDownloadStatus(DownloadStatus.DOWNLOADED);
return;
}
@@ -150,7 +152,6 @@ export function useAttachment(attachment: Attachment, keyPlain: string) {
}
setDownloadStatus(DownloadStatus.DECRYPTING);
//console.info("Decrypted attachment ", Buffer.from(keyPlain, 'binary').toString('hex'));
console.info("KP", keyPlain);
const decrypted = await decodeWithPassword(keyPlain, downloadedBlob);
setDownloadTag("");
if(attachment.type == AttachmentType.FILE) {
@@ -162,7 +163,17 @@ export function useAttachment(attachment: Attachment, keyPlain: string) {
const filename = preview.split("::")[1];
let buffer = Buffer.from(decrypted.split(",")[1], 'base64');
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);
return;
}