From fd910e956bcf5af4ad3d1b66f7fa5fcc250f85c8 Mon Sep 17 00:00:00 2001 From: Robert Sehr Date: Thu, 24 Oct 2024 11:08:04 +0200 Subject: [PATCH] try to get file extension from content-disposition --- build.xml | 4 +-- .../DownloadAndVerifyAssetsStepPlugin.java | 25 ++++++++++++++++--- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/build.xml b/build.xml index beddbc8..34c04c2 100644 --- a/build.xml +++ b/build.xml @@ -1,14 +1,14 @@ - + - + diff --git a/module-base/src/main/java/de/intranda/goobi/plugins/DownloadAndVerifyAssetsStepPlugin.java b/module-base/src/main/java/de/intranda/goobi/plugins/DownloadAndVerifyAssetsStepPlugin.java index 67ef6fa..9131a1f 100644 --- a/module-base/src/main/java/de/intranda/goobi/plugins/DownloadAndVerifyAssetsStepPlugin.java +++ b/module-base/src/main/java/de/intranda/goobi/plugins/DownloadAndVerifyAssetsStepPlugin.java @@ -33,11 +33,13 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.stream.Collectors; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.apache.commons.configuration.HierarchicalConfiguration; import org.apache.commons.configuration.SubnodeConfiguration; import org.apache.commons.lang3.StringUtils; +import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; @@ -109,6 +111,8 @@ public class DownloadAndVerifyAssetsStepPlugin implements IStepPluginVersion2 { private String downloadUrl; + private static Pattern filenamePattern = Pattern.compile(".*filename=\\\"(.*)\\\".*"); + @Override public void initialize(Step step, String returnPath) { this.returnPath = returnPath; @@ -266,7 +270,7 @@ private void prepareUrlHashAndFolderMaps() { List hashValues = propertiesMap.get(hashPropertyName) .stream() .map(String::trim) - .collect(Collectors.toList()); + .toList(); List urls = propertiesMap.get(propertyName); log.debug("urls has " + urls.size() + " elements"); @@ -381,12 +385,25 @@ private void processFile(String fileUrl, String hash, String targetFolder) throw method.setHeader("Authorization", authenticationToken); } + String extension = ""; HttpResponse response = httpclient.execute(method); HttpEntity entity = response.getEntity(); + for (Header h : response.getHeaders("content-disposition")) { + String val = h.getValue(); + Matcher m = filenamePattern.matcher(val); + if (m.find()) { + extension = m.group(1); + if (extension.contains(".")) { + extension = extension.substring(extension.indexOf(".")); + } + } + } String contentType = entity.getContentType().getValue(); - String extension = ""; - if (StringUtils.isNotBlank(contentType)) { + if (StringUtils.isBlank(extension) && StringUtils.isNotBlank(contentType)) { extension = "." + contentType.substring(contentType.indexOf("/") + 1); + if (extension.contains(";")) { + extension = extension.substring(0, extension.indexOf(";")); + } } destination = Paths.get(targetFolder, fileName + extension);