Новая система вложений

This commit is contained in:
RoyceDa
2026-03-26 22:29:03 +02:00
parent fd3fac54f6
commit 8d6090e632
12 changed files with 192 additions and 145 deletions

View File

@@ -27,11 +27,10 @@ export enum DownloadStatus {
}
export function useAttachment(attachment: Attachment, parentMessage: MessageProps) {
const uuidRegex = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/;
const uploadedPercentage = useUploadStatus(attachment.id);
const downloadPercentage = useDownloadStatus(attachment.id);
const [downloadStatus, setDownloadStatus] = useMemory("attachment-downloaded-status-" + attachment.id, DownloadStatus.PENDING, true);
const [downloadTag, setDownloadTag] = useState("");
const [downloadTag, setDownloadTag] = useState(attachment.transport_tag || "");
const {readFile, writeFile, fileExists, size} = useFileStorage();
const { downloadFile } = useTransport();
const publicKey = usePublicKey();
@@ -50,30 +49,18 @@ export function useAttachment(attachment: Attachment, parentMessage: MessageProp
}, []);
const getPreview = () => {
if(attachment.preview.split("::")[0].match(uuidRegex)){
/**
* Это тег загрузки
*/
return attachment.preview.split("::").splice(1).join("::");
}
return attachment.preview;
}
const calcDownloadStatus = async () => {
if(attachment.preview.split("::")[0].match(uuidRegex)){
/**
* Это тег загрузки
*/
setDownloadTag(attachment.preview.split("::")[0]);
}
if(!attachment.preview.split("::")[0].match(uuidRegex)){
/**
* Там не тег загрузки, значит это наш файл
*/
setDownloadStatus(DownloadStatus.DOWNLOADED);
if (downloadStatus == DownloadStatus.DOWNLOADED) {
return;
}
if (downloadStatus == DownloadStatus.DOWNLOADED) {
if(attachment.transport_tag == ""){
/**
* Транспортного тега нет только у сообщений отправленных нами, значит он точно наш
*/
setDownloadStatus(DownloadStatus.DOWNLOADED);
return;
}
if(attachment.type == AttachmentType.FILE){
@@ -143,7 +130,7 @@ export function useAttachment(attachment: Attachment, parentMessage: MessageProp
let downloadedBlob = '';
try {
downloadedBlob = await downloadFile(attachment.id,
downloadTag);
downloadTag, attachment.transport_server);
} catch (e) {
console.info(e);
info("Error downloading attachment: " + attachment.id);

View File

@@ -10,6 +10,7 @@ import { useDatabase } from "../DatabaseProvider/useDatabase";
import { useConsoleLogger } from "@/app/hooks/useConsoleLogger";
import { useDialogsCache } from "../DialogProvider/useDialogsCache";
import { DialogContext } from "../DialogProvider/DialogProvider";
import { useTransportServer } from "../TransportProvider/useTransportServer";
export function usePrepareAttachment() {
const intervalsRef = useRef<NodeJS.Timeout>(null);
@@ -19,6 +20,7 @@ export function usePrepareAttachment() {
const {info} = useConsoleLogger('usePrepareAttachment');
const {getDialogCache} = useDialogsCache();
const context = useContext(DialogContext);
const transportServer = useTransportServer();
const updateTimestampInDialogCache = (dialog : string, message_id: string) => {
const dialogCache = getDialogCache(dialog);
@@ -74,18 +76,6 @@ export function usePrepareAttachment() {
}, (MESSAGE_MAX_TIME_TO_DELEVERED_S / 2) * 1000);
}
/**
* Удаляет старый тег если вложения были подготовлены заново
* например при пересылке сообщений
*/
const removeOldTagIfAttachemtnsRePreapred = (preview : string) => {
if(preview.indexOf("::") == -1){
return preview;
}
let parts = preview.split("::");
return parts.slice(1).join("::");
}
/**
* Подготавливает вложения для отправки. Подготовка
* состоит в загрузке файлов на транспортный сервер, мы не делаем
@@ -127,6 +117,16 @@ export function usePrepareAttachment() {
const blurhash = await base64ImageToBlurhash(attachment.blob);
attachment.preview = blurhash;
}
if(rePrepared && attachment.encoded_for == dialog){
/**
* Это пересланное сообщение и оно уже закодировано для этого диалога, значит не нужно его кодировать и загружать заново
*/
prepared.push({
...attachment,
blob: ""
});
continue;
}
doTimestampUpdateImMessageWhileAttachmentsSend(message_id, dialog);
const content = await encodeWithPassword(password, attachment.blob);
const upid = attachment.id;
@@ -139,7 +139,10 @@ export function usePrepareAttachment() {
}
prepared.push({
...attachment,
preview: tag + "::" + (rePrepared ? removeOldTagIfAttachemtnsRePreapred(attachment.preview) : attachment.preview),
transport_server: transportServer || "",
transport_tag: tag,
encoded_for: dialog,
preview: attachment.preview,
blob: ""
});
}