5
5
import fr .wseduc .webutils .http .Renders ;
6
6
import io .vertx .core .CompositeFuture ;
7
7
import io .vertx .core .Future ;
8
+ import io .vertx .core .Promise ;
8
9
import io .vertx .core .Vertx ;
9
10
import io .vertx .core .buffer .Buffer ;
10
11
import io .vertx .core .file .AsyncFile ;
14
15
import io .vertx .core .json .JsonObject ;
15
16
import io .vertx .core .logging .Logger ;
16
17
import io .vertx .core .logging .LoggerFactory ;
18
+ import io .vertx .core .shareddata .LocalMap ;
17
19
import io .vertx .core .streams .Pump ;
18
20
import io .vertx .core .streams .ReadStream ;
19
- import org .entcore .common .folders .FolderImporter ;
20
21
import org .entcore .common .folders .FolderManager ;
21
22
import org .entcore .common .service .impl .MongoDbRepositoryEvents ;
22
23
import org .entcore .common .storage .AntivirusClient ;
32
33
import java .util .*;
33
34
import java .util .stream .Collectors ;
34
35
36
+ import static org .apache .commons .lang3 .ObjectUtils .isEmpty ;
37
+
35
38
public class FolderImporterZip {
36
39
static final Logger logger = LoggerFactory .getLogger (FolderImporterZip .class );
37
40
private final Vertx vertx ;
@@ -57,7 +60,7 @@ public FolderImporterZip(final Vertx v, final FolderManager aManager) {
57
60
}
58
61
}
59
62
} catch (Exception e ) {
60
-
63
+ logger . warn ( "An error occurred while initializing importer" , e );
61
64
}
62
65
}
63
66
@@ -95,17 +98,18 @@ public static Future<FolderImporterZipContext> createContext(Vertx vertx, UserIn
95
98
}
96
99
97
100
public static Future <FolderImporterZipContext > createContext (Vertx vertx , UserInfos user , ReadStream <Buffer > buffer , String invalidMessage ) {
98
- final Future <FolderImporterZipContext > future = Future .future ();
99
- final String name = UUID .randomUUID ().toString () + ".zip" ;
100
- final String zipPath = Paths .get (System .getProperty ("java.io.tmpdir" ), name ).normalize ().toString ();
101
+ final Promise <FolderImporterZipContext > future = Promise .promise ();
102
+ final String name = UUID .randomUUID () + ".zip" ;
103
+ final String importPath = getImportPath (vertx );
104
+ final String zipPath = Paths .get (importPath , name ).normalize ().toString ();
101
105
buffer .pause ();
102
106
vertx .fileSystem ().open (zipPath , new OpenOptions ().setTruncateExisting (true ).setCreate (true ).setWrite (true ), fileRes -> {
103
107
if (fileRes .succeeded ()) {
104
108
final AsyncFile file = fileRes .result ();
105
109
final Pump pump = Pump .pump (buffer , file );
106
110
buffer .endHandler (r -> {
107
111
file .end ();
108
- future .complete (new FolderImporterZipContext (zipPath , user , invalidMessage ));
112
+ future .complete (new FolderImporterZipContext (zipPath , importPath , user , invalidMessage ));
109
113
});
110
114
buffer .exceptionHandler (e -> {
111
115
file .end ();
@@ -117,7 +121,24 @@ public static Future<FolderImporterZipContext> createContext(Vertx vertx, UserIn
117
121
future .fail (fileRes .cause ());
118
122
}
119
123
});
120
- return future ;
124
+ return future .future ();
125
+ }
126
+
127
+ /**
128
+ * Looks into the configuration to fetch the path to import the archive in the following order :
129
+ * - 'import-path' in the configuration of the module
130
+ * - 'import-path' in the shared configuration of the ENT
131
+ * - 'java.io.tmpdir' environment variable if all else fails
132
+ * @param vertx Vertx instance of the called
133
+ * @return The path to import the zip file
134
+ */
135
+ private static String getImportPath (Vertx vertx ) {
136
+ String importPath = vertx .getOrCreateContext ().config ().getString ("import-path" );
137
+ if (org .apache .commons .lang3 .StringUtils .isEmpty (importPath )) {
138
+ final LocalMap <Object , Object > localMap = vertx .sharedData ().getLocalMap ("server" );
139
+ importPath = (String )localMap .getOrDefault ("import-path" , System .getProperty ("java.io.tmpdir" ));
140
+ }
141
+ return importPath ;
121
142
}
122
143
123
144
public Future <Void > doPrepare (final FolderImporterZipContext context ) {
@@ -221,7 +242,7 @@ private Future<List<FileInfo>> copyToTemp(FolderImporterZipContext context, Opti
221
242
return future ;
222
243
}
223
244
final String fileName = Paths .get (context .zipPath ).getFileName ().toString ().replaceAll (".zip" , "" );
224
- final Path tempDir = Paths .get (System . getProperty ( "java.io.tmpdir" ) , fileName ).normalize ();
245
+ final Path tempDir = Paths .get (context . tmpPath , fileName ).normalize ();
225
246
vertx .fileSystem ().mkdir (tempDir .toString (), dir -> {
226
247
if (dir .succeeded ()) {
227
248
context .toClean .add (tempDir .toString ());
@@ -374,6 +395,7 @@ public long getRealSize(final FolderImporterZipContext context){
374
395
375
396
public static class FolderImporterZipContext {
376
397
private final String zipPath ;
398
+ private final String tmpPath ;
377
399
private final String userId ;
378
400
private final String userName ;
379
401
private final Map <String , FileInfo > docToInsertById = new HashMap <>();
@@ -386,15 +408,16 @@ public static class FolderImporterZipContext {
386
408
private final String invalidMessage ;
387
409
private Optional <String > guessedEncodingCache ;
388
410
389
- public FolderImporterZipContext (final String aZipPath , UserInfos user , String invalidMessage ) {
390
- this (aZipPath , user .getUserId (), user .getUsername (), invalidMessage );
411
+ public FolderImporterZipContext (final String aZipPath , final String tmpPath , UserInfos user , String invalidMessage ) {
412
+ this (aZipPath , tmpPath , user .getUserId (), user .getUsername (), invalidMessage );
391
413
}
392
414
393
- public FolderImporterZipContext (final String aZipPath , final String aUserId , final String aUserName , final String invalidMessage ) {
415
+ public FolderImporterZipContext (final String aZipPath , final String tmpPath , final String aUserId , final String aUserName , final String invalidMessage ) {
394
416
this .zipPath = aZipPath ;
395
417
this .userId = aUserId ;
396
418
this .userName = aUserName ;
397
419
this .invalidMessage = invalidMessage ;
420
+ this .tmpPath = isEmpty (tmpPath ) ? System .getProperty ("java.io.tmpdir" ) : tmpPath ;
398
421
}
399
422
400
423
public FolderImporterZipContext setRootFolder (final JsonObject parentFolder ) {
0 commit comments