Skip to content

Commit 5e19797

Browse files
jcbe-odejuniorode
authored andcommitted
fix(common): #WB-3055, FolderManager.copyAllNoFail should not throw an exception when a thumbnail file is missing #490
1 parent 7893c88 commit 5e19797

File tree

2 files changed

+36
-36
lines changed

2 files changed

+36
-36
lines changed

common/src/main/java/org/entcore/common/folders/impl/FolderManagerMongoImpl.java

+1-12
Original file line numberDiff line numberDiff line change
@@ -345,18 +345,7 @@ private Future<List<Optional<JsonObject>>> duplicateFilesNoFail(
345345
// merge shared after reset shared
346346
InheritShareComputer.mergeShared(Optional.empty(), copy, true);
347347
// copy file from storage
348-
StorageHelper.replaceAll(copy, copiedFilesMap);
349-
350-
// Any fileId not copied must be removed from the new doc
351-
final List<String> toRemove = new ArrayList<String>();
352-
for( String fileId : fileIds) {
353-
if(!copiedFilesMap.containsKey(fileId)) {
354-
toRemove.add(fileId);
355-
}
356-
}
357-
if( toRemove.size() > 0 ) {
358-
StorageHelper.removeAll(copy, toRemove);
359-
}
348+
StorageHelper.replaceAll(copy, copiedFilesMap, true);
360349

361350
return Optional.of(copy);
362351
});

common/src/main/java/org/entcore/common/folders/impl/StorageHelper.java

+35-24
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,21 @@ public static Set<String> getThumbnailsFilesIds(JsonObject jsonDocument) {
3535
return thumbIds;
3636
}
3737

38-
static void replaceAll(JsonObject jsonDocument, Map<String, String> oldFileIdForNewFileId) {
38+
public static void replaceAll(JsonObject jsonDocument, Map<String, String> oldFileIdForNewFileId) {
39+
replaceAll(jsonDocument, oldFileIdForNewFileId, false);
40+
}
41+
42+
/**
43+
* Map all files ID in a Document to another "new" ID.
44+
* Useful when cloning a document.
45+
* @param jsonDocument
46+
* @param oldFileIdForNewFileId Map of (old,new) IDs
47+
* @param skipMissingThumbnails When truthy, drop thumbnail files that are not in the map.
48+
*/
49+
public static void replaceAll(
50+
JsonObject jsonDocument, Map<String, String> oldFileIdForNewFileId,
51+
boolean skipMissingThumbnails
52+
) {
3953
// replace file id
4054
Set<String> fileIds = getFileId(jsonDocument);
4155
for (String fileId : fileIds) {
@@ -44,32 +58,17 @@ static void replaceAll(JsonObject jsonDocument, Map<String, String> oldFileIdFor
4458
}
4559
setFileId(jsonDocument, oldFileIdForNewFileId.get(fileId));
4660
}
47-
// replace thumb values
61+
// replace or drop thumb values
4862
fileIds = getThumbnailsFilesIds(jsonDocument);
4963
for (String fileId : fileIds) {
5064
if (!oldFileIdForNewFileId.containsKey(fileId)) {
51-
throw new IllegalStateException("Could not found newFileId of the file:" + fileId);
52-
}
53-
replaceThumbnailFileId(jsonDocument, fileId, oldFileIdForNewFileId.get(fileId));
54-
}
55-
}
56-
57-
/**
58-
* Clean a document, by removing any reference to a file (id is given),
59-
* in the `file` and `thumbnails` fields.
60-
*
61-
* @param jsonDocument document to clean.
62-
* @param fileIds IDs of file to be removed from `file` or `thumbnails` fields.
63-
*/
64-
static void removeAll(JsonObject jsonDocument, Collection<String> fileIds) {
65-
final JsonObject thumbnails = jsonDocument.getJsonObject("thumbnails", null);
66-
final Set<Map.Entry<String,Object>> thumbnailEntries = thumbnails==null ? null : thumbnails.getMap().entrySet();
67-
for (String fileId : fileIds) {
68-
if( fileId == null ) continue;
69-
if(getFileId(jsonDocument).contains(fileId)) {
70-
jsonDocument.remove("file");
71-
} else if(thumbnailEntries!=null) {
72-
thumbnailEntries.removeIf(entry -> entry!=null && entry.getValue()!=null && fileId.equals(entry.getValue().toString()));
65+
if(skipMissingThumbnails) {
66+
dropThumbnailFileId(jsonDocument, fileId);
67+
} else {
68+
throw new IllegalStateException("Could not found newFileId of the file:" + fileId);
69+
}
70+
} else {
71+
replaceThumbnailFileId(jsonDocument, fileId, oldFileIdForNewFileId.get(fileId));
7372
}
7473
}
7574
}
@@ -95,6 +94,18 @@ static void replaceThumbnailFileId(JsonObject jsonDocument, String fileId, Strin
9594
jsonDocument.put("thumbnails", meta);
9695
}
9796

97+
static void dropThumbnailFileId(JsonObject jsonDocument, String fileId) {
98+
JsonObject meta = jsonDocument.getJsonObject("thumbnails", new JsonObject());
99+
for (String key : meta.fieldNames()) {
100+
String value = meta.getString(key);
101+
if (value != null && value.equals(fileId)) {
102+
meta.remove(key);
103+
break;
104+
}
105+
}
106+
jsonDocument.put("thumbnails", meta);
107+
}
108+
98109
public static List<String> getListOfFileIds(JsonObject jsonDocument) {
99110
List<String> listOfFilesIds = new ArrayList<>(getFileId(jsonDocument));
100111
listOfFilesIds.addAll(getThumbnailsFilesIds(jsonDocument));

0 commit comments

Comments
 (0)