Skip to content

Commit 7448cdc

Browse files
committed
MOBILE-4482 glossary: Avoid reuploading files when editing entry
1 parent 3d51b17 commit 7448cdc

File tree

2 files changed

+75
-11
lines changed

2 files changed

+75
-11
lines changed

src/addons/mod/glossary/pages/edit/edit.ts

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,9 @@ abstract class AddonModGlossaryFormHandler {
286286
* Upload attachments online.
287287
*
288288
* @param glossary Glossary.
289-
* @returns Uploaded attachments item id.
289+
* @returns Uploaded attachments item id, undefined if nothing to upload or change.
290290
*/
291-
protected async uploadAttachments(glossary: AddonModGlossaryGlossary): Promise<number> {
291+
protected async uploadAttachments(glossary: AddonModGlossaryGlossary): Promise<number | undefined> {
292292
const data = this.page.data;
293293
const itemId = await CoreFileUploader.uploadOrReuploadFiles(
294294
data.attachments,
@@ -643,10 +643,7 @@ class AddonModGlossaryOnlineFormHandler extends AddonModGlossaryFormHandler {
643643
data.fullmatch = this.entry.fullmatch;
644644
}
645645

646-
// Treat offline attachments if any.
647-
if (this.entry.attachments) {
648-
data.attachments = this.entry.attachments;
649-
}
646+
data.attachments = (this.entry.attachments ?? []).slice();
650647

651648
this.page.originalData = {
652649
concept: data.concept,
@@ -677,11 +674,7 @@ class AddonModGlossaryOnlineFormHandler extends AddonModGlossaryFormHandler {
677674
const definition = CoreText.formatHtmlLines(data.definition);
678675

679676
// Upload attachments, if any.
680-
let attachmentsId: number | undefined = undefined;
681-
682-
if (data.attachments.length) {
683-
attachmentsId = await this.uploadAttachments(glossary);
684-
}
677+
const attachmentsId = await this.uploadAttachments();
685678

686679
// Save entry data.
687680
await AddonModGlossary.updateEntry(glossary.id, this.entry.id, data.concept, definition, options, attachmentsId);
@@ -694,6 +687,31 @@ class AddonModGlossaryOnlineFormHandler extends AddonModGlossaryFormHandler {
694687
return true;
695688
}
696689

690+
/**
691+
* Upload attachments online.
692+
*
693+
* @returns Uploaded attachments item id, undefined if nothing to upload or change.
694+
*/
695+
protected async uploadAttachments(): Promise<number | undefined> {
696+
const data = this.page.data;
697+
698+
if (!CoreFileUploader.areFileListDifferent(data.attachments, this.entry.attachments ?? [])) {
699+
return;
700+
}
701+
702+
const { attachmentsid: attachmentsId } = await AddonModGlossary.prepareEntryForEdition(this.entry.id);
703+
704+
const removedFiles = CoreFileUploader.getFilesToDelete(this.entry.attachments ?? [], data.attachments);
705+
706+
if (removedFiles.length) {
707+
await CoreFileUploader.deleteDraftFiles(attachmentsId, removedFiles);
708+
}
709+
710+
await CoreFileUploader.uploadFiles(attachmentsId, data.attachments);
711+
712+
return attachmentsId;
713+
}
714+
697715
}
698716

699717
/**

src/addons/mod/glossary/services/glossary.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,7 @@ export class AddonModGlossaryProvider {
626626
*
627627
* @param siteId Site id.
628628
* @returns Whether the site can update entries.
629+
* @since 3.10
629630
*/
630631
async canUpdateEntries(siteId?: string): Promise<boolean> {
631632
const site = await CoreSites.getSite(siteId);
@@ -1097,6 +1098,26 @@ export class AddonModGlossaryProvider {
10971098
await site.getDb().insertRecord(ENTRIES_TABLE_NAME, entry);
10981099
}
10991100

1101+
/**
1102+
* Prepare entry for edition.
1103+
*
1104+
* @param entryId Entry ID.
1105+
* @param siteId Site ID.
1106+
* @returns Data of prepared area.
1107+
*/
1108+
async prepareEntryForEdition(
1109+
entryId: number,
1110+
siteId?: string,
1111+
): Promise<AddonModGlossaryPrepareEntryForEditionWSResponse> {
1112+
const site = await CoreSites.getSite(siteId);
1113+
1114+
const params: AddonModGlossaryPrepareEntryForEditionWSParams = {
1115+
entryid: entryId,
1116+
};
1117+
1118+
return await site.write('mod_glossary_prepare_entry_for_edition', params);
1119+
}
1120+
11001121
}
11011122

11021123
export const AddonModGlossary = makeSingleton(AddonModGlossaryProvider);
@@ -1435,6 +1456,31 @@ export type AddonModGlossaryViewEntryWSParams = {
14351456
id: number; // Glossary entry ID.
14361457
};
14371458

1459+
/**
1460+
* Params of mod_glossary_prepare_entry_for_edition WS.
1461+
*/
1462+
type AddonModGlossaryPrepareEntryForEditionWSParams = {
1463+
entryid: number; // Glossary entry id to update.
1464+
};
1465+
1466+
/**
1467+
* Data returned by mod_glossary_prepare_entry_for_edition WS.
1468+
*/
1469+
export type AddonModGlossaryPrepareEntryForEditionWSResponse = {
1470+
inlineattachmentsid: number; // Draft item id for the text editor.
1471+
attachmentsid: number; // Draft item id for the file manager.
1472+
areas: { // File areas including options.
1473+
area: string; // File area name.
1474+
options: { // Draft file area options.
1475+
name: string; // Name of option.
1476+
value: string; // Value of option.
1477+
}[];
1478+
}[];
1479+
aliases: string[];
1480+
categories: number[];
1481+
warnings?: CoreWSExternalWarning[];
1482+
};
1483+
14381484
/**
14391485
* Options to pass to add entry.
14401486
*/

0 commit comments

Comments
 (0)