More work on export

This commit is contained in:
N-Pex 2025-07-02 15:40:43 +02:00
parent 94bf35875a
commit 2b2c736311
5 changed files with 327 additions and 299 deletions

View file

@ -1,5 +1,5 @@
import { MatrixClient, MatrixEvent, Room } from "matrix-js-sdk";
import { EventAttachment, EventAttachmentLoadSrcOptions, EventAttachmentUrlType, KeanuEvent, KeanuEventExtension } from "./eventAttachment";
import { EventAttachment, EventAttachmentLoadSrcOptions, EventAttachmentUrlData, EventAttachmentUrlPromise, EventAttachmentUrlType, KeanuEvent, KeanuEventExtension } from "./eventAttachment";
import axios, { AxiosRequestConfig, AxiosResponse } from "axios";
import { Counter, ModeOfOperation } from "aes-js";
import { Attachment, AttachmentBatch, AttachmentSendInfo } from "./attachment";
@ -144,22 +144,22 @@ export class AttachmentManager {
autoDownloadable: fileSize <= this.maxSizeAutoDownloads,
loadSrc: () => Promise.reject("Not implemented"),
loadThumbnail: () => Promise.reject("Not implemented"),
loadBlob: () => Promise.reject("Not implemented"),
release: () => Promise.reject("Not implemented"),
});
attachment.loadSrc = (options?: EventAttachmentLoadSrcOptions) => {
if (attachment.src && !options?.asBlob) {
attachment.loadSrc = () => {
if (attachment.src) {
return Promise.resolve({data: attachment.src, type: "src"});
} else if (attachment.srcPromise && !options?.asBlob) {
} else if (attachment.srcPromise) {
return attachment.srcPromise;
}
Implement loadBlob somewhere here!
attachment.srcPromise = this._loadEventAttachmentOrThumbnail(event, false, !!options?.asBlob, (percent) => {
attachment.srcPromise = this._loadEventAttachmentOrThumbnail(event, false, false, (percent) => {
attachment.srcProgress = percent;
}).then((res) => {
attachment.src = res.data as string;
attachment.src = (res as EventAttachmentUrlData).data;
return res;
});
return attachment.srcPromise;
}) as Promise<EventAttachmentUrlData>;
return attachment.srcPromise as Promise<{data:string,type:EventAttachmentUrlType}>;
};
attachment.loadThumbnail = () => {
if (attachment.thumbnail) {
@ -176,9 +176,17 @@ export class AttachmentManager {
attachment.src = res.data as string;
}
return res;
});
}) as Promise<EventAttachmentUrlData>;
return attachment.thumbnailPromise;
};
attachment.loadBlob = () => {
const promise = this._loadEventAttachmentOrThumbnail(event, false, true, (percent) => {
attachment.srcProgress = percent;
}).then((res) => {
return {data: res.data as Blob};
});
return promise;
};
attachment.release = (src: boolean, thumbnail: boolean) => {
// TODO - figure out logic
if (entry) {
@ -201,7 +209,7 @@ export class AttachmentManager {
thumbnail: boolean,
asBlob: boolean,
progress?: (percent: number) => void
): Promise<{data: string | Blob, type: EventAttachmentUrlType}> {
): Promise<EventAttachmentUrlData | {data: Blob, type: EventAttachmentUrlType}> {
await this.matrixClient.decryptEventIfNeeded(event);
let urltype: EventAttachmentUrlType = thumbnail ? "thumbnail" : "src";
@ -293,7 +301,7 @@ export class AttachmentManager {
const response = await axios.get(url, options);
const bytes = decrypt ? await this.decryptData(file, response) : { buffer: response.data };
const blob = new Blob([bytes.buffer], { type: mime });
return {data: asBlob ? blob : URL.createObjectURL(blob), type: urltype};
return asBlob ? {data: blob, type: urltype} : {data: URL.createObjectURL(blob), type: urltype};
}
private b64toBuffer(val: any) {

View file

@ -9,9 +9,7 @@ export type KeanuEventExtension = {
}
export type EventAttachmentUrlType = "src" | "thumbnail";
export type EventAttachmentLoadSrcOptions = {
asBlob?: boolean;
}
export type EventAttachmentUrlData = {data: string, type: EventAttachmentUrlType};
export type EventAttachment = {
event: MatrixEvent & KeanuEventExtension;
@ -19,13 +17,14 @@ export type EventAttachment = {
src?: string;
srcSize: number;
srcProgress: number;
srcPromise?: Promise<{data: string | Blob, type: EventAttachmentUrlType}>;
srcPromise?: Promise<EventAttachmentUrlData>;
thumbnail?: string;
thumbnailProgress: number;
thumbnailPromise?: Promise<{data: string | Blob, type: EventAttachmentUrlType}>;
thumbnailPromise?: Promise<EventAttachmentUrlData>;
autoDownloadable: boolean;
loadSrc: (options?: EventAttachmentLoadSrcOptions) => Promise<{data: string | Blob, type: EventAttachmentUrlType}>;
loadThumbnail: () => Promise<{data: string | Blob, type: EventAttachmentUrlType}>;
loadSrc: () => Promise<EventAttachmentUrlData>;
loadThumbnail: () => Promise<EventAttachmentUrlData>;
loadBlob: () => Promise<{data: Blob}>;
release: (src: boolean, thumbnail: boolean) => void;
};