16
16
import org .apache .commons .compress .archivers .zip .ZipArchiveEntry ;
17
17
import org .apache .commons .compress .archivers .zip .ZipArchiveOutputStream ;
18
18
import org .apache .commons .io .IOUtils ;
19
+ import org .apache .commons .lang3 .StringUtils ;
19
20
import org .springframework .scheduling .annotation .Async ;
20
21
import org .springframework .stereotype .Service ;
21
22
@@ -40,9 +41,11 @@ public ObjectExportGenerator(FileStorage fileStorage) {
40
41
41
42
@ Async (MainConfiguration .DINA_THREAD_POOL_BEAN_NAME )
42
43
public CompletableFuture <UUID > export (UUID exportUUID , List <AbstractObjectStoreMetadata > objectsToExport ,
44
+ Map <UUID , String > aliases ,
43
45
Map <String , List <UUID >> exportLayout , Path zipFile ) {
44
46
45
47
Map <UUID , String > layoutByFileIdentifier = invertExportLayout (exportLayout );
48
+ Map <UUID , String > filenameAliases = aliases == null ? Map .of () : aliases ;
46
49
Map <String , AtomicInteger > filenamesIncluded = new HashMap <>();
47
50
48
51
try (ArchiveOutputStream <ZipArchiveEntry > o = new ZipArchiveOutputStream (zipFile )) {
@@ -53,7 +56,8 @@ public CompletableFuture<UUID> export(UUID exportUUID, List<AbstractObjectStoreM
53
56
54
57
// Set zipEntry with information from fileStorage
55
58
ZipArchiveEntry entry =
56
- new ZipArchiveEntry (generateExportItemFilename (currObj , layoutByFileIdentifier , filenamesIncluded ));
59
+ new ZipArchiveEntry (generateExportItemFilename (currObj , filenameAliases .get (currObj .getFileIdentifier ()),
60
+ layoutByFileIdentifier .get (currObj .getFileIdentifier ()), filenamesIncluded ));
57
61
entry .setSize (
58
62
fileInfo .orElseThrow (() -> new IllegalStateException ("No FileInfo found" )).getLength ());
59
63
o .putArchiveEntry (entry );
@@ -79,25 +83,26 @@ public CompletableFuture<UUID> export(UUID exportUUID, List<AbstractObjectStoreM
79
83
* Get a unique (withing the export) filename.
80
84
*
81
85
* @param obj the data about the file to add
86
+ * @param filenameAlias use an alternative name for the filename
87
+ * @param folder folder to which the file should be stored under
82
88
* @param usedFilenames filenames that are already used with a counter. Will be modified by this function.
83
89
* @return
84
90
*/
85
91
private static String generateExportItemFilename (AbstractObjectStoreMetadata obj ,
86
- Map < UUID , String > layoutByFileIdentifier ,
92
+ String filenameAlias , String folder ,
87
93
Map <String , AtomicInteger > usedFilenames ) {
88
94
String filename ;
89
-
90
95
if (obj instanceof ObjectStoreMetadata metadata ) {
91
- filename = ObjectFilenameUtils .generateMainObjectFilename (metadata );
96
+ filename = ObjectFilenameUtils .generateMainObjectFilename (metadata , filenameAlias );
92
97
} else if (obj instanceof Derivative derivative ) {
93
- filename = ObjectFilenameUtils .generateDerivativeFilename (derivative );
98
+ filename = ObjectFilenameUtils .generateDerivativeFilename (derivative , filenameAlias );
94
99
} else {
95
100
filename = obj .getFilename ();
96
101
}
97
102
98
103
// Do we have an export layout to consider ?
99
- if (layoutByFileIdentifier . containsKey ( obj . getFileIdentifier () )) {
100
- String folderName = ObjectFilenameUtils .standardizeFolderName (layoutByFileIdentifier . get ( obj . getFileIdentifier ()) );
104
+ if (StringUtils . isNotBlank ( folder )) {
105
+ String folderName = ObjectFilenameUtils .standardizeFolderName (folder );
101
106
filename = folderName + filename ;
102
107
}
103
108
0 commit comments