From 03914d5e5d73c8199154baa813b60d0e81a3bba8 Mon Sep 17 00:00:00 2001 From: Clemens Tolboom Date: Thu, 7 Mar 2024 11:02:19 +0100 Subject: [PATCH] Add file download name with timestamp --- .../controller/InsightController.java | 2 +- .../armadillo/metadata/InsightService.java | 22 ++++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/armadillo/src/main/java/org/molgenis/armadillo/controller/InsightController.java b/armadillo/src/main/java/org/molgenis/armadillo/controller/InsightController.java index a1a417266..778bbddbf 100644 --- a/armadillo/src/main/java/org/molgenis/armadillo/controller/InsightController.java +++ b/armadillo/src/main/java/org/molgenis/armadillo/controller/InsightController.java @@ -111,7 +111,7 @@ public ResponseEntity createDownloadFile(String file_id) { HttpHeaders headers = new HttpHeaders(); headers.add( HttpHeaders.CONTENT_DISPOSITION, - "attachment; filename=\"" + insightService.getFileName(file_id) + "\""); + "attachment; filename=\"" + insightService.getDownloadName(file_id) + ".txt\""); headers.add(HttpHeaders.CONTENT_TYPE, "text/text"); return new ResponseEntity<>(inputStreamResource, headers, OK); } diff --git a/armadillo/src/main/java/org/molgenis/armadillo/metadata/InsightService.java b/armadillo/src/main/java/org/molgenis/armadillo/metadata/InsightService.java index 56d8f7585..886008015 100644 --- a/armadillo/src/main/java/org/molgenis/armadillo/metadata/InsightService.java +++ b/armadillo/src/main/java/org/molgenis/armadillo/metadata/InsightService.java @@ -66,6 +66,16 @@ public String getFileName(String file_id) { return file_id; } + public String getDownloadName(String file_id) { + String requestTime = getServerTime().replace(" ", "T").replace(":", "").replace("-", ""); + + InsightServiceFiles c = InsightServiceFiles.getConstantByKey(file_id); + if (!(c == null)) { + return c.getDownloadName() + "-" + requestTime; + } + return file_id; + } + public FileDetails fileDetails(String file_id, int pageNum, int pageSize, String direction) { InsightServiceFiles insightServiceFiles = InsightServiceFiles.getConstantByKey(file_id); if (!(insightServiceFiles == null)) { @@ -92,17 +102,19 @@ public FileInputStream downloadFile(String file_id) { } enum InsightServiceFiles { - AUDIT_FILE("AUDIT_FILE", MediaType.APPLICATION_NDJSON_VALUE, "Audit file"), - LOG_FILE("LOG_FILE", MediaType.TEXT_PLAIN_VALUE, "Log file"); + AUDIT_FILE("AUDIT_FILE", MediaType.APPLICATION_NDJSON_VALUE, "Audit file", "armadillo-audit"), + LOG_FILE("LOG_FILE", MediaType.TEXT_PLAIN_VALUE, "Log file", "armadillo-log"); private final String key; private final String contentType; private final String displayName; + private final String downloadName; - InsightServiceFiles(String key, String contentType, String displayName) { + InsightServiceFiles(String key, String contentType, String displayName, String downloadName) { this.key = key; this.contentType = contentType; this.displayName = displayName; + this.downloadName = downloadName; } public String getKey() { @@ -117,6 +129,10 @@ public String getDisplayName() { return displayName; } + public String getDownloadName() { + return downloadName; + } + public static boolean hasKey(String key) { for (InsightServiceFiles constant : InsightServiceFiles.values()) { if (constant.getKey().equals(key)) {