Skip to content

Commit ab17df6

Browse files
committed
fix: #WB-3352, use import-path configuration value to store zip file being imported
1 parent 63fe1b6 commit ab17df6

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

common/src/main/java/org/entcore/common/folders/FolderManager.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ void addFileWithParent(Optional<JsonObject> parent, JsonObject doc, String owner
105105
* @param handler the handler that emit the file object save or an error if any
106106
*/
107107
default void importFileZip(String zipPath, UserInfos user, Handler<AsyncResult<JsonObject>> handler){
108-
importFileZip(new FolderImporterZip.FolderImporterZipContext(zipPath, user, "invalid"), handler);
108+
importFileZip(new FolderImporterZip.FolderImporterZipContext(zipPath, null, user, "invalid"), handler);
109109
}
110110

111111
/**

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

+34-11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import fr.wseduc.webutils.http.Renders;
66
import io.vertx.core.CompositeFuture;
77
import io.vertx.core.Future;
8+
import io.vertx.core.Promise;
89
import io.vertx.core.Vertx;
910
import io.vertx.core.buffer.Buffer;
1011
import io.vertx.core.file.AsyncFile;
@@ -14,9 +15,9 @@
1415
import io.vertx.core.json.JsonObject;
1516
import io.vertx.core.logging.Logger;
1617
import io.vertx.core.logging.LoggerFactory;
18+
import io.vertx.core.shareddata.LocalMap;
1719
import io.vertx.core.streams.Pump;
1820
import io.vertx.core.streams.ReadStream;
19-
import org.entcore.common.folders.FolderImporter;
2021
import org.entcore.common.folders.FolderManager;
2122
import org.entcore.common.service.impl.MongoDbRepositoryEvents;
2223
import org.entcore.common.storage.AntivirusClient;
@@ -32,6 +33,8 @@
3233
import java.util.*;
3334
import java.util.stream.Collectors;
3435

36+
import static org.apache.commons.lang3.ObjectUtils.isEmpty;
37+
3538
public class FolderImporterZip {
3639
static final Logger logger = LoggerFactory.getLogger(FolderImporterZip.class);
3740
private final Vertx vertx;
@@ -57,7 +60,7 @@ public FolderImporterZip(final Vertx v, final FolderManager aManager) {
5760
}
5861
}
5962
} catch (Exception e) {
60-
63+
logger.warn("An error occurred while initializing importer", e);
6164
}
6265
}
6366

@@ -95,17 +98,18 @@ public static Future<FolderImporterZipContext> createContext(Vertx vertx, UserIn
9598
}
9699

97100
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();
101105
buffer.pause();
102106
vertx.fileSystem().open(zipPath, new OpenOptions().setTruncateExisting(true).setCreate(true).setWrite(true), fileRes -> {
103107
if (fileRes.succeeded()) {
104108
final AsyncFile file = fileRes.result();
105109
final Pump pump = Pump.pump(buffer, file);
106110
buffer.endHandler(r -> {
107111
file.end();
108-
future.complete(new FolderImporterZipContext(zipPath, user, invalidMessage));
112+
future.complete(new FolderImporterZipContext(zipPath, importPath, user, invalidMessage));
109113
});
110114
buffer.exceptionHandler(e -> {
111115
file.end();
@@ -117,7 +121,24 @@ public static Future<FolderImporterZipContext> createContext(Vertx vertx, UserIn
117121
future.fail(fileRes.cause());
118122
}
119123
});
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;
121142
}
122143

123144
public Future<Void> doPrepare(final FolderImporterZipContext context) {
@@ -221,7 +242,7 @@ private Future<List<FileInfo>> copyToTemp(FolderImporterZipContext context, Opti
221242
return future;
222243
}
223244
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();
225246
vertx.fileSystem().mkdir(tempDir.toString(), dir -> {
226247
if (dir.succeeded()) {
227248
context.toClean.add(tempDir.toString());
@@ -374,6 +395,7 @@ public long getRealSize(final FolderImporterZipContext context){
374395

375396
public static class FolderImporterZipContext {
376397
private final String zipPath;
398+
private final String tmpPath;
377399
private final String userId;
378400
private final String userName;
379401
private final Map<String, FileInfo> docToInsertById = new HashMap<>();
@@ -386,15 +408,16 @@ public static class FolderImporterZipContext {
386408
private final String invalidMessage;
387409
private Optional<String> guessedEncodingCache;
388410

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);
391413
}
392414

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) {
394416
this.zipPath = aZipPath;
395417
this.userId = aUserId;
396418
this.userName = aUserName;
397419
this.invalidMessage = invalidMessage;
420+
this.tmpPath = isEmpty(tmpPath) ? System.getProperty("java.io.tmpdir") : tmpPath;
398421
}
399422

400423
public FolderImporterZipContext setRootFolder(final JsonObject parentFolder) {

0 commit comments

Comments
 (0)