Skip to content

Commit

Permalink
Add fileName as requestParam to exportCase endpoint
Browse files Browse the repository at this point in the history
Signed-off-by: Etienne Homer <etiennehomer@gmail.com>
  • Loading branch information
etiennehomer committed Nov 7, 2024
1 parent 8d25d02 commit 80e331a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/powsybl/caseserver/CaseController.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,10 @@ public ResponseEntity<byte[]> downloadCase(@PathVariable("caseUuid") UUID caseUu
public ResponseEntity<byte[]> exportCase(
@PathVariable UUID caseUuid,
@RequestParam String format,
@RequestParam(value = "fileName", required = false) String fileName,
@RequestBody(required = false) Map<String, Object> formatParameters) throws IOException {
LOGGER.debug("exportCase request received with parameter caseUuid = {}", caseUuid);
return caseService.exportCase(caseUuid, format, formatParameters).map(networkInfos -> {
return caseService.exportCase(caseUuid, format, fileName, formatParameters).map(networkInfos -> {
var headers = new HttpHeaders();
headers.setContentDisposition(
ContentDisposition.builder("attachment")
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/powsybl/caseserver/service/CaseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ default byte[] createZipFile(Collection<String> names, MemDataSource dataSource)
}
}

default Optional<ExportCaseInfos> exportCase(UUID caseUuid, String format, Map<String, Object> formatParameters) throws IOException {
default Optional<ExportCaseInfos> exportCase(UUID caseUuid, String format, String fileName, Map<String, Object> formatParameters) throws IOException {
if (!Exporter.getFormats().contains(format)) {
throw CaseException.createUnsupportedFormat(format);
}
Expand All @@ -117,17 +117,17 @@ default Optional<ExportCaseInfos> exportCase(UUID caseUuid, String format, Map<S
network.write(format, exportProperties, memDataSource);

var listNames = memDataSource.listNames(".*");
String networkName = DataSourceUtil.getBaseName(getCaseName(caseUuid));
String fileOrNetworkName = fileName != null ? fileName : DataSourceUtil.getBaseName(getCaseName(caseUuid));
byte[] networkData;
if (listNames.size() == 1) {
String extension = listNames.iterator().next();
networkName += extension;
fileOrNetworkName += extension;
networkData = memDataSource.getData(extension);
} else {
networkName += ".zip";
fileOrNetworkName += ".zip";
networkData = createZipFile(listNames, memDataSource);
}
return Optional.of(new ExportCaseInfos(networkName, networkData));
return Optional.of(new ExportCaseInfos(fileOrNetworkName, networkData));
} else {
return Optional.empty();
}
Expand Down

0 comments on commit 80e331a

Please sign in to comment.