Skip to content

Commit aab626b

Browse files
Issue 1315: Store file preview to database when file preview is created on Item Page load. (#1316)
* Issue ufal/clarin-dspace1315: Store file preview to database when file preview is created on item page load * assert text improvement * PR comments: commit context only when any of the file preview is successfully created * chabge variable name
1 parent 04d64f7 commit aab626b

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ public class MetadataBitstreamRestRepository extends DSpaceRestRepository<Metada
6666
@SearchRestMethod(name = "byHandle")
6767
public Page<MetadataBitstreamWrapperRest> findByHandle(@Parameter(value = "handle", required = true) String handle,
6868
@Parameter(value = "fileGrpType") String fileGrpType,
69-
Pageable pageable)
70-
throws Exception {
69+
Pageable pageable) throws Exception {
7170
if (StringUtils.isBlank(handle)) {
7271
throw new DSpaceBadRequestException("handle cannot be null!");
7372
}
@@ -80,6 +79,8 @@ public Page<MetadataBitstreamWrapperRest> findByHandle(@Parameter(value = "handl
8079
List<MetadataBitstreamWrapperRest> rs = new ArrayList<>();
8180
DSpaceObject dso;
8281

82+
boolean previewContentCreated = false;
83+
8384
try {
8485
dso = handleService.resolveToObject(context, handle);
8586
} catch (Exception e) {
@@ -127,6 +128,7 @@ public Page<MetadataBitstreamWrapperRest> findByHandle(@Parameter(value = "handl
127128
for (FileInfo fi : fileInfos) {
128129
previewContentService.createPreviewContent(context, bitstream, fi);
129130
}
131+
previewContentCreated = true;
130132
}
131133
}
132134
} else {
@@ -147,6 +149,11 @@ public Page<MetadataBitstreamWrapperRest> findByHandle(@Parameter(value = "handl
147149
}
148150
}
149151

152+
// commit changes if any preview content was generated
153+
if (previewContentCreated) {
154+
context.commit();
155+
}
156+
150157
return new PageImpl<>(rs, pageable, rs.size());
151158
}
152159

dspace-server-webapp/src/test/java/org/dspace/app/rest/MetadataBitstreamRestRepositoryIT.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import static org.hamcrest.Matchers.is;
1313
import static org.hamcrest.Matchers.notNullValue;
1414
import static org.junit.Assert.assertFalse;
15+
import static org.junit.Assert.assertTrue;
1516
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
1617
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
1718
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
@@ -84,16 +85,16 @@ public void setup() throws Exception {
8485
.build();
8586

8687
// create empty THUMBNAIL bundle
87-
bundleService.create(context, publicItem, "THUMBNAIL");
88-
89-
String bitstreamContent = "ThisIsSomeDummyText";
90-
InputStream is = IOUtils.toInputStream(bitstreamContent, CharEncoding.UTF_8);
91-
bts = BitstreamBuilder.
92-
createBitstream(context, publicItem, is)
93-
.withName("Bitstream")
94-
.withDescription("Description")
95-
.withMimeType("application/x-gzip")
96-
.build();
88+
bundleService.create(context, publicItem, "ORIGINAL");
89+
90+
try (InputStream is = getClass().getResourceAsStream("assetstore/logos.tgz")) {
91+
bts = BitstreamBuilder.
92+
createBitstream(context, publicItem, is)
93+
.withName("Bitstream")
94+
.withDescription("Description")
95+
.withMimeType("application/x-gtar")
96+
.build();
97+
}
9798

9899
// Allow composing of file preview in the config
99100
configurationService.setProperty("create.file-preview.on-item-page-load", true);
@@ -116,6 +117,8 @@ public void findByHandle() throws Exception {
116117
// There is no restriction, so the user could preview the file
117118
boolean canPreview = true;
118119

120+
assertFalse("Expects preview content not created yet.", previewContentService.hasPreview(context, bts));
121+
119122
getClient().perform(get(METADATABITSTREAM_SEARCH_BY_HANDLE_ENDPOINT)
120123
.param("handle", publicItem.getHandle())
121124
.param("fileGrpType", FILE_GRP_TYPE))
@@ -135,13 +138,13 @@ public void findByHandle() throws Exception {
135138
.value(hasItem(is((int) bts.getSizeBytes()))))
136139
.andExpect(jsonPath("$._embedded.metadatabitstreams[*].canPreview")
137140
.value(Matchers.containsInAnyOrder(Matchers.is(canPreview))))
138-
.andExpect(jsonPath("$._embedded.metadatabitstreams[*].fileInfo").exists())
141+
.andExpect(jsonPath("$._embedded.metadatabitstreams[0].fileInfo").value(Matchers.hasSize(2)))
139142
.andExpect(jsonPath("$._embedded.metadatabitstreams[*].checksum")
140143
.value(Matchers.containsInAnyOrder(Matchers.containsString(bts.getChecksum()))))
141144
.andExpect(jsonPath("$._embedded.metadatabitstreams[*].href")
142145
.value(Matchers.containsInAnyOrder(Matchers.containsString(url))));
143146

144-
147+
assertTrue("Expects preview content created and stored.", previewContentService.hasPreview(context, bts));
145148
}
146149

147150
@Test

0 commit comments

Comments
 (0)