feat(views): delete duplicate attachments

This commit is contained in:
Elian Doran 2025-07-06 20:16:47 +03:00
parent 7b1c058d29
commit ce33dfb003
No known key found for this signature in database

View file

@ -26,17 +26,18 @@ export default class ViewModeStorage<T extends object> {
}
async restore() {
const existingAttachments = await this.note.getAttachmentsByRole(ATTACHMENT_ROLE);
const existingAttachments = (await this.note.getAttachmentsByRole(ATTACHMENT_ROLE))
.filter(a => a.title === this.attachmentName);
if (existingAttachments.length === 0) {
return undefined;
}
const attachment = existingAttachments
.find(a => a.title === this.attachmentName);
if (!attachment) {
return undefined;
if (existingAttachments.length > 1) {
// Clean up duplicates.
await Promise.all(existingAttachments.slice(1).map(async a => await server.remove(`attachments/${a.attachmentId}`)));
}
const attachment = existingAttachments[0];
const attachmentData = await server.get<{ content: string } | null>(`attachments/${attachment.attachmentId}/blob`);
return JSON.parse(attachmentData?.content ?? "{}") as T;
}