From 1b2bb8edaf4f69aeec871ce7ec46e842c97c1c99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Ko=C5=A1arko?= Date: Wed, 23 Oct 2024 17:32:18 +0200 Subject: [PATCH] Add ways to influence the bitstream name --- .../org/dspace/administer/FileDownloader.java | 21 +++++++++++++++++-- .../FileDownloaderConfiguration.java | 3 +++ .../dspace/administer/FileDownloaderIT.java | 11 +++++++++- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/dspace-api/src/main/java/org/dspace/administer/FileDownloader.java b/dspace-api/src/main/java/org/dspace/administer/FileDownloader.java index b6b552c33180..41ef7d5fba91 100644 --- a/dspace-api/src/main/java/org/dspace/administer/FileDownloader.java +++ b/dspace-api/src/main/java/org/dspace/administer/FileDownloader.java @@ -17,6 +17,7 @@ import java.sql.SQLException; import java.util.List; import java.util.UUID; +import java.util.stream.Stream; import org.apache.commons.cli.ParseException; import org.dspace.authorize.AuthorizeException; @@ -46,6 +47,7 @@ public class FileDownloader extends DSpaceRunnable private UUID itemUUID; private URI uri; private String epersonMail; + private String bitstreamName; private EPersonService epersonService; private ItemService itemService; private BitstreamService bitstreamService; @@ -105,6 +107,10 @@ public void setup() throws ParseException { itemUUID = UUID.fromString(commandLine.getOptionValue("i")); epersonMail = commandLine.getOptionValue("e"); + + if (commandLine.hasOption("n")) { + bitstreamName = commandLine.getOptionValue("n"); + } } /** @@ -143,14 +149,24 @@ public void internalRun() throws Exception { throw new IllegalArgumentException("The provided URL returned a status code of " + response.statusCode()); } + //use the provided value, the content-disposition header, the last part of the uri + if (bitstreamName == null) { + bitstreamName = response.headers().firstValue("Content-Disposition") + .filter(value -> value.contains("filename=")).flatMap(value -> Stream.of(value.split(";")) + .filter(v -> v.contains("filename=")) + .findFirst() + .map(fvalue -> fvalue.replaceFirst("filename=", "").replaceAll("\"", ""))) + .orElse(uri.getPath().substring(uri.getPath().lastIndexOf('/') + 1)); + } + try (InputStream is = response.body()) { - saveFileToItem(context, item, is); + saveFileToItem(context, item, is, bitstreamName); } context.commit(); } - private void saveFileToItem(Context context, Item item, InputStream is) + private void saveFileToItem(Context context, Item item, InputStream is, String name) throws SQLException, AuthorizeException, IOException { log.debug("Saving file to item {}", item.getID()); List originals = item.getBundles("ORIGINAL"); @@ -161,6 +177,7 @@ private void saveFileToItem(Context context, Item item, InputStream is) Bundle bundle = originals.get(0); b = bitstreamService.create(context, bundle, is); } + b.setName(context, name); //now guess format of the bitstream BitstreamFormat bf = bitstreamFormatService.guessFormat(context, b); b.setFormat(context, bf); diff --git a/dspace-api/src/main/java/org/dspace/administer/FileDownloaderConfiguration.java b/dspace-api/src/main/java/org/dspace/administer/FileDownloaderConfiguration.java index 3dac14174704..78177e77a7ef 100644 --- a/dspace-api/src/main/java/org/dspace/administer/FileDownloaderConfiguration.java +++ b/dspace-api/src/main/java/org/dspace/administer/FileDownloaderConfiguration.java @@ -56,6 +56,9 @@ public Options getOptions() { options.addOption("e", "eperson", true, "eperson email"); options.getOption("e").setRequired(false); + options.addOption("n", "name", true, "name of the file/bitstream"); + options.getOption("n").setRequired(false); + super.options = options; } return options; diff --git a/dspace-api/src/test/java/org/dspace/administer/FileDownloaderIT.java b/dspace-api/src/test/java/org/dspace/administer/FileDownloaderIT.java index f6216ee55d3a..a946533838df 100644 --- a/dspace-api/src/test/java/org/dspace/administer/FileDownloaderIT.java +++ b/dspace-api/src/test/java/org/dspace/administer/FileDownloaderIT.java @@ -8,13 +8,17 @@ package org.dspace.administer; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.mockserver.model.HttpRequest.request; import static org.mockserver.model.HttpResponse.response; +import java.util.List; + import org.dspace.AbstractIntegrationTestWithDatabase; import org.dspace.builder.CollectionBuilder; import org.dspace.builder.CommunityBuilder; import org.dspace.builder.ItemBuilder; +import org.dspace.content.Bitstream; import org.dspace.content.Collection; import org.dspace.content.Community; import org.dspace.content.Item; @@ -25,6 +29,7 @@ import org.junit.Test; import org.mockserver.junit.MockServerRule; + public class FileDownloaderIT extends AbstractIntegrationTestWithDatabase { @Rule @@ -57,6 +62,7 @@ public void setUp() throws Exception { ).respond( response() .withStatusCode(200) + .withHeader("Content-Disposition", "attachment; filename=\"test.txt\"") .withBody("test") ); } @@ -95,7 +101,10 @@ public void testDownloadFile() throws Exception { assertEquals(1, item.getBundles().size()); - assertEquals(1, item.getBundles().get(0).getBitstreams().size()); + List bs = item.getBundles().get(0).getBitstreams(); + assertEquals(1, bs.size()); + assertNotNull("Expecting name to be defined", bs.get(0).getName()); + } } \ No newline at end of file