Skip to content

Commit

Permalink
Add ways to influence the bitstream name
Browse files Browse the repository at this point in the history
  • Loading branch information
kosarko committed Nov 26, 2024
1 parent 01013eb commit 1b2bb8e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
21 changes: 19 additions & 2 deletions dspace-api/src/main/java/org/dspace/administer/FileDownloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -46,6 +47,7 @@ public class FileDownloader extends DSpaceRunnable<FileDownloaderConfiguration>
private UUID itemUUID;
private URI uri;
private String epersonMail;
private String bitstreamName;
private EPersonService epersonService;
private ItemService itemService;
private BitstreamService bitstreamService;
Expand Down Expand Up @@ -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");
}
}

/**
Expand Down Expand Up @@ -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<Bundle> originals = item.getBundles("ORIGINAL");
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,6 +29,7 @@
import org.junit.Test;
import org.mockserver.junit.MockServerRule;


public class FileDownloaderIT extends AbstractIntegrationTestWithDatabase {

@Rule
Expand Down Expand Up @@ -57,6 +62,7 @@ public void setUp() throws Exception {
).respond(
response()
.withStatusCode(200)
.withHeader("Content-Disposition", "attachment; filename=\"test.txt\"")
.withBody("test")
);
}
Expand Down Expand Up @@ -95,7 +101,10 @@ public void testDownloadFile() throws Exception {


assertEquals(1, item.getBundles().size());
assertEquals(1, item.getBundles().get(0).getBitstreams().size());
List<Bitstream> bs = item.getBundles().get(0).getBitstreams();
assertEquals(1, bs.size());
assertNotNull("Expecting name to be defined", bs.get(0).getName());

}

}

0 comments on commit 1b2bb8e

Please sign in to comment.