MOBILE-4653 core: Fix race condition when uploading files

Uploading several files in parallel can cause a race condition in the server when trying to insert the directory data in the 'files' table of the DB, in the create_directory function. To avoid that, now files are uploaded 1 by 1 in the same area.
main
Dani Palou 2024-11-07 12:27:23 +01:00
parent 17c387ef4c
commit 14a4b83fb5
1 changed files with 5 additions and 7 deletions

View File

@ -639,7 +639,8 @@ export class CoreFileUploaderProvider {
} }
}); });
await Promise.all(filesToUpload.map(async (file) => { // Upload files 1 by 1 to avoid race conditions in the server.
for (const file of filesToUpload) {
// Make sure the file name is unique in the area. // Make sure the file name is unique in the area.
const name = CoreFile.calculateUniqueName(usedNames, file.name); const name = CoreFile.calculateUniqueName(usedNames, file.name);
usedNames[name] = file; usedNames[name] = file;
@ -649,7 +650,7 @@ export class CoreFileUploaderProvider {
const options = this.getFileUploadOptions(filePath, name, undefined, false, 'draft', itemId); const options = this.getFileUploadOptions(filePath, name, undefined, false, 'draft', itemId);
await this.uploadFile(filePath, options, undefined, siteId); await this.uploadFile(filePath, options, undefined, siteId);
})); }
} }
/** /**
@ -742,15 +743,12 @@ export class CoreFileUploaderProvider {
// Upload only the first file first to get a draft id. // Upload only the first file first to get a draft id.
const itemId = await this.uploadOrReuploadFile(files[0], 0, component, componentId, siteId); const itemId = await this.uploadOrReuploadFile(files[0], 0, component, componentId, siteId);
const promises: Promise<number>[] = []; // Upload files 1 by 1 to avoid race conditions in the server.
for (let i = 1; i < files.length; i++) { for (let i = 1; i < files.length; i++) {
const file = files[i]; const file = files[i];
promises.push(this.uploadOrReuploadFile(file, itemId, component, componentId, siteId)); await this.uploadOrReuploadFile(file, itemId, component, componentId, siteId);
} }
await Promise.all(promises);
return itemId; return itemId;
} }