Skip to content

Commit

Permalink
try to get file extension from content-disposition
Browse files Browse the repository at this point in the history
  • Loading branch information
rsehr committed Oct 24, 2024
1 parent 9904461 commit fd910e9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
4 changes: 2 additions & 2 deletions build.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<project default="devbuild">

<!-- Simply set the name of the plugin here, e.g. "fileUpload" or "file_upload") instead of "sample" -->
<property name="name" value="download_and_verify_assets" />
<property name="name" value="download-and-verify-assets" />

<!-- Use this task to let ant copy the compiled jar files to the target folders for development -->
<target name="devbuild">
<exec executable="mvn">
<arg value="package"/>
</exec>
<copy file="module-main/target/plugin_intranda_step_${name}.jar" todir="/opt/digiverso/goobi/plugins/step/" overwrite="true"/>
<copy file="module-base/target/plugin-step-${name}-base.jar" todir="/opt/digiverso/goobi/plugins/step/" overwrite="true"/>
</target>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -266,7 +270,7 @@ private void prepareUrlHashAndFolderMaps() {
List<String> hashValues = propertiesMap.get(hashPropertyName)
.stream()
.map(String::trim)
.collect(Collectors.toList());
.toList();

List<String> urls = propertiesMap.get(propertyName);
log.debug("urls has " + urls.size() + " elements");
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit fd910e9

Please sign in to comment.