Merge pull request #9113 from uniba-ub/fix-9112-bitstream

fix not resolved thumbnail due to filename issues
This commit is contained in:
Tim Donohue
2023-10-26 11:10:00 -05:00
committed by GitHub
2 changed files with 55 additions and 1 deletions

View File

@@ -403,7 +403,7 @@ public class BitstreamServiceImpl extends DSpaceObjectServiceImpl<Bitstream> imp
@Override @Override
public Bitstream getThumbnail(Context context, Bitstream bitstream) throws SQLException { 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 (Bundle bundle : bitstream.getBundles()) {
for (Item item : bundle.getItems()) { for (Item item : bundle.getItems()) {
@@ -420,6 +420,13 @@ public class BitstreamServiceImpl extends DSpaceObjectServiceImpl<Bitstream> imp
return null; return null;
} }
protected Pattern getBitstreamNamePattern(Bitstream bitstream) {
if (bitstream.getName() != null) {
return Pattern.compile("^" + Pattern.quote(bitstream.getName()) + ".([^.]+)$");
}
return Pattern.compile("^" + bitstream.getName() + ".([^.]+)$");
}
@Override @Override
public BitstreamFormat getFormat(Context context, Bitstream bitstream) throws SQLException { public BitstreamFormat getFormat(Context context, Bitstream bitstream) throws SQLException {
if (bitstream.getBitstreamFormat() == null) { if (bitstream.getBitstreamFormat() == null) {

View File

@@ -1695,6 +1695,53 @@ public class BitstreamRestRepositoryIT extends AbstractControllerIntegrationTest
.andExpect(jsonPath("$.type", is("bitstream"))); .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 @Test
public void thumbnailEndpointMultipleThumbnailsWithPrimaryBitstreamTest() throws Exception { public void thumbnailEndpointMultipleThumbnailsWithPrimaryBitstreamTest() throws Exception {
// Given an Item // Given an Item