Skip to content

Commit

Permalink
Merge pull request DSpace#9113 from uniba-ub/fix-9112-bitstream
Browse files Browse the repository at this point in the history
fix not resolved thumbnail due to filename issues
  • Loading branch information
tdonohue authored Oct 26, 2023
2 parents be8547e + c5466c2 commit e0ece4a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ public Bitstream getFirstBitstream(Item item, String bundleName) throws SQLExcep

@Override
public Bitstream getThumbnail(Context context, Bitstream bitstream) throws SQLException {
Pattern pattern = Pattern.compile("^" + bitstream.getName() + ".([^.]+)$");
Pattern pattern = getBitstreamNamePattern(bitstream);

for (Bundle bundle : bitstream.getBundles()) {
for (Item item : bundle.getItems()) {
Expand All @@ -420,6 +420,13 @@ public Bitstream getThumbnail(Context context, Bitstream bitstream) throws SQLEx
return null;
}

protected Pattern getBitstreamNamePattern(Bitstream bitstream) {
if (bitstream.getName() != null) {
return Pattern.compile("^" + Pattern.quote(bitstream.getName()) + ".([^.]+)$");
}
return Pattern.compile("^" + bitstream.getName() + ".([^.]+)$");
}

@Override
public BitstreamFormat getFormat(Context context, Bitstream bitstream) throws SQLException {
if (bitstream.getBitstreamFormat() == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1695,6 +1695,53 @@ public void thumbnailEndpointTest() throws Exception {
.andExpect(jsonPath("$.type", is("bitstream")));
}

@Test
public void thumbnailEndpointTestWithSpecialCharactersInFileName() throws Exception {
// Given an Item
context.turnOffAuthorisationSystem();

parentCommunity = CommunityBuilder.createCommunity(context)
.withName("Parent Community")
.build();

Collection col1 = CollectionBuilder.createCollection(context, parentCommunity)
.withName("Collection 1").build();

Item item = ItemBuilder.createItem(context, col1)
.withTitle("Test item -- thumbnail")
.withIssueDate("2017-10-17")
.withAuthor("Smith, Donald").withAuthor("Doe, John")
.build();

Bundle originalBundle = BundleBuilder.createBundle(context, item)
.withName(Constants.DEFAULT_BUNDLE_NAME)
.build();
Bundle thumbnailBundle = BundleBuilder.createBundle(context, item)
.withName("THUMBNAIL")
.build();

InputStream is = IOUtils.toInputStream("dummy", "utf-8");

// With an ORIGINAL Bitstream & matching THUMBNAIL Bitstream containing special characters in filenames
Bitstream bitstream = BitstreamBuilder.createBitstream(context, originalBundle, is)
.withName("test (2023) file.pdf")
.withMimeType("application/pdf")
.build();
Bitstream thumbnail = BitstreamBuilder.createBitstream(context, thumbnailBundle, is)
.withName("test (2023) file.pdf.jpg")
.withMimeType("image/jpeg")
.build();

context.restoreAuthSystemState();

String tokenAdmin = getAuthToken(admin.getEmail(), password);

getClient(tokenAdmin).perform(get("/api/core/bitstreams/" + bitstream.getID() + "/thumbnail"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.uuid", Matchers.is(thumbnail.getID().toString())))
.andExpect(jsonPath("$.type", is("bitstream")));
}

@Test
public void thumbnailEndpointMultipleThumbnailsWithPrimaryBitstreamTest() throws Exception {
// Given an Item
Expand Down

0 comments on commit e0ece4a

Please sign in to comment.