More send media optimization

This commit is contained in:
N-Pex 2025-07-17 11:22:26 +02:00
parent 7b4b35762a
commit 9b0a6ae225
5 changed files with 54 additions and 55 deletions

View file

@ -70,7 +70,7 @@ export type AttachmentBatch = {
attachmentsSentCount: ComputedRef<number>;
attachmentsSending: ComputedRef<Attachment[]>;
attachmentsSent: ComputedRef<Attachment[]>;
addAttachment: (attachment: Attachment) => void;
addAttachments: (attachments: Attachment[]) => void;
removeAttachment: (attachment: Attachment) => void;
isTooLarge: (attachment: Attachment) => boolean;
canSend: ComputedRef<boolean>;

View file

@ -57,7 +57,6 @@ export class AttachmentManager {
private async prepareUpload(attachment: Attachment): Promise<Attachment> {
const file = attachment.file;
if (file.type.startsWith("image/")) {
attachment.proof = await proofmode.proofCheckFile(file);
let url = URL.createObjectURL(file);
attachment.src = url;
if (attachment.src) {
@ -66,8 +65,6 @@ export class AttachmentManager {
img.src = url;
attachment.dimensions = await new Promise((response) => {
img.onload = (event) => {
console.log("height: " + img.height);
console.log("width: " + img.width);
response({ width: img.width, height: img.height });
};
img.onerror = (event) => {
@ -82,38 +79,38 @@ export class AttachmentManager {
var aspect = w / h;
var newWidth = parseInt((w > h ? 640 : 640 * aspect).toFixed());
var newHeight = parseInt((w > h ? 640 / aspect : 640).toFixed());
imageResize(file, {
const img = await imageResize(file, {
format: "webp",
width: newWidth,
height: newHeight,
outputType: "blob",
})
.then((img) => {
attachment.scaledFile = new File([img as BlobPart], file.name, {
type: "image/webp",
lastModified: Date.now(),
});
attachment.scaledDimensions = {
width: newWidth,
height: newHeight,
};
// Default to scaled version if the image does not contain Content Credentials
//
attachment.useScaled =
attachment.scaledFile !== undefined &&
(attachment.proof === undefined ||
attachment.proof.integrity === undefined ||
attachment.proof.integrity.c2pa === undefined);
})
.catch((err) => {
console.error("Resize failed:", err);
});
});
attachment.scaledFile = new File([img as BlobPart], file.name, {
type: "image/webp",
lastModified: Date.now(),
});
attachment.scaledDimensions = {
width: newWidth,
height: newHeight,
};
}
} catch (error) {
console.error("Failed to get image dimensions: " + error);
}
}
try {
attachment.proof = await proofmode.proofCheckFile(file);
// Default to scaled version if the image does not contain Content Credentials
//
attachment.useScaled =
attachment.scaledFile !== undefined &&
(attachment.proof === undefined ||
attachment.proof.integrity === undefined ||
attachment.proof.integrity.c2pa === undefined);
} catch (error) {
console.error("Failed to get content credentials: " + error);
}
} else if (file.type.startsWith("video/")) {
let url = URL.createObjectURL(file);
const thumb: AttachmentThumbnail | undefined = await new Promise((resolve) => {
@ -421,9 +418,9 @@ export const createUploadBatch = (
attachments.value.sort((a, b) => (b.sendInfo?.statusDate ?? 0) - (a.sendInfo?.statusDate ?? 0));
};
const addAttachment = (attachment: Attachment) => {
const addAttachments = (attachmentsToAdd: Attachment[]) => {
if (sendingStatus.value == "initial") {
attachments.value.push(attachment);
attachments.value.push(...attachmentsToAdd);
}
};
@ -439,7 +436,9 @@ export const createUploadBatch = (
};
const canSend = computed(() => {
return attachments.value.length > 0 && !attachments.value.some((a: Attachment) => isTooLarge(a));
return (
attachments.value.length > 0 && !attachments.value.some((a: Attachment) => isTooLarge(a) || a.status !== "loaded")
);
});
const cancel = () => {
@ -603,7 +602,7 @@ export const createUploadBatch = (
attachmentsSentCount,
attachmentsSending,
attachmentsSent,
addAttachment,
addAttachments,
removeAttachment,
isTooLarge,
canSend,