Skip to content

Commit ff0427e

Browse files
UFAL/Issue 1313: fixed error when file preview is not generated for bitstream with store_number = 77 (ufal#1318) (#1240)
* Issue ufal#1313: fixed error when file preview is not generated for bitstream with store number = 77 * resolve MR comments (cherry picked from commit 04d64f7) Co-authored-by: Milan Kuchtiak <kuchtiak@ufal.mff.cuni.cz>
1 parent 475b25a commit ff0427e

File tree

4 files changed

+101
-19
lines changed

4 files changed

+101
-19
lines changed

dspace-api/src/main/java/org/dspace/storage/bitstore/SyncBitstreamStorageServiceImpl.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
package org.dspace.storage.bitstore;
99

10+
import java.io.File;
1011
import java.io.IOException;
1112
import java.io.InputStream;
1213
import java.sql.SQLException;
@@ -185,12 +186,17 @@ public Map computeChecksumSpecStore(Context context, Bitstream bitstream, int st
185186
}
186187

187188
@Override
188-
public InputStream retrieve(Context context, Bitstream bitstream)
189-
throws SQLException, IOException {
189+
public InputStream retrieve(Context context, Bitstream bitstream) throws SQLException, IOException {
190190
int storeNumber = this.whichStoreNumber(bitstream);
191191
return this.getStore(storeNumber).get(bitstream);
192192
}
193193

194+
@Override
195+
public File retrieveFile(Context context, Bitstream bitstream) throws IOException {
196+
int storeNumber = whichStoreNumber(bitstream);
197+
return this.getStore(storeNumber).getFile(bitstream);
198+
}
199+
194200
@Override
195201
public void cleanup(boolean deleteDbRecords, boolean verbose) throws SQLException, IOException, AuthorizeException {
196202
Context context = new Context(Context.Mode.BATCH_EDIT);

dspace-api/src/test/java/org/dspace/builder/WorkspaceItemBuilder.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,29 @@ public WorkspaceItemBuilder withFulltext(String name, String source, InputStream
249249
return this;
250250
}
251251

252+
/**
253+
* Add bitstream with specific store number.
254+
*
255+
* @param name bitstream name
256+
* @param source bitstream test source location
257+
* @param is input stream of the bitstream
258+
* @param storeNumber store number
259+
*
260+
* @return this WorkspaceItemBuilder
261+
*/
262+
public WorkspaceItemBuilder withBitstream(String name, String source, InputStream is, int storeNumber) {
263+
try {
264+
Item item = workspaceItem.getItem();
265+
Bitstream b = itemService.createSingleBitstream(context, is, item);
266+
b.setStoreNumber(storeNumber);
267+
b.setName(context, name);
268+
b.setSource(context, source);
269+
} catch (Exception e) {
270+
handleException(e);
271+
}
272+
return this;
273+
}
274+
252275
/**
253276
* Create workspaceItem with any metadata
254277
* @param schema metadataSchema name e.g. `dc`

dspace-api/src/test/java/org/dspace/scripts/filepreview/FilePreviewIT.java

Lines changed: 70 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import static org.hamcrest.Matchers.hasItem;
1414
import static org.hamcrest.Matchers.hasSize;
1515
import static org.junit.Assert.assertEquals;
16+
import static org.junit.Assert.assertNotNull;
17+
import static org.junit.Assert.assertTrue;
1618

1719
import java.io.InputStream;
1820
import java.sql.SQLException;
@@ -36,7 +38,11 @@
3638
import org.dspace.content.factory.ContentServiceFactory;
3739
import org.dspace.content.service.BitstreamFormatService;
3840
import org.dspace.content.service.BitstreamService;
41+
import org.dspace.content.service.PreviewContentService;
3942
import org.dspace.eperson.EPerson;
43+
import org.dspace.services.ConfigurationService;
44+
import org.dspace.services.factory.DSpaceServicesFactory;
45+
import org.dspace.storage.bitstore.SyncBitstreamStorageServiceImpl;
4046
import org.junit.Before;
4147
import org.junit.Test;
4248

@@ -45,10 +51,14 @@
4551
* @author Milan Majchrak (milan.majchrak at dataquest.sk)
4652
*/
4753
public class FilePreviewIT extends AbstractIntegrationTestWithDatabase {
48-
BitstreamService bitstreamService = ContentServiceFactory.getInstance().getBitstreamService();
54+
private static final int SYNC_STORE_NUMBER = SyncBitstreamStorageServiceImpl.SYNCHRONIZED_STORES_NUMBER;
4955

56+
BitstreamService bitstreamService = ContentServiceFactory.getInstance().getBitstreamService();
5057
BitstreamFormatService bitstreamFormatService = ContentServiceFactory.getInstance().getBitstreamFormatService();
58+
PreviewContentService previewContentService = ContentServiceFactory.getInstance().getPreviewContentService();
59+
ConfigurationService configurationService = DSpaceServicesFactory.getInstance().getConfigurationService();
5160

61+
Collection collection;
5262
Item item;
5363
EPerson eperson;
5464
String PASSWORD = "test";
@@ -61,7 +71,7 @@ public void setup() throws SQLException, AuthorizeException {
6171
eperson = EPersonBuilder.createEPerson(context)
6272
.withEmail("test@test.edu").withPassword(PASSWORD).build();
6373
Community community = CommunityBuilder.createCommunity(context).withName("Com").build();
64-
Collection collection = CollectionBuilder.createCollection(context, community).withName("Col").build();
74+
collection = CollectionBuilder.createCollection(context, community).withName("Col").build();
6575
WorkspaceItem wItem = WorkspaceItemBuilder.createWorkspaceItem(context, collection)
6676
.withFulltext("preview-file-test.zip", "/local/path/preview-file-test.zip", previewZipIs)
6777
.build();
@@ -116,22 +126,45 @@ public void testWhenNoFilesRun() throws Exception {
116126
@Test
117127
public void testForSpecificItem() throws Exception {
118128
// Run the script
119-
TestDSpaceRunnableHandler testDSpaceRunnableHandler = new TestDSpaceRunnableHandler();
120-
String[] args = new String[] { "file-preview", "-u", item.getID().toString(),
121-
"-e", eperson.getEmail(), "-p", PASSWORD};
122-
int run = ScriptLauncher.handleScript(args, ScriptLauncher.getConfig(kernelImpl),
123-
testDSpaceRunnableHandler, kernelImpl);
124-
assertEquals(0, run);
125-
// There should be no errors or warnings
126-
checkNoError(testDSpaceRunnableHandler);
129+
runScriptForItemWithBitstreams(item);
130+
}
127131

128-
// There should be an info message about generating the file previews for the specified item
129-
List<String> messages = testDSpaceRunnableHandler.getInfoMessages();
130-
assertThat(messages, hasSize(2));
131-
assertThat(messages, hasItem(containsString("Generate the file previews for the specified item with " +
132-
"the given UUID: " + item.getID())));
133-
assertThat(messages,
134-
hasItem(containsString("Authentication by user: " + eperson.getEmail())));
132+
@Test
133+
public void testPreviewWithSyncStorage() throws Exception {
134+
configurationService.setProperty("sync.storage.service.enabled", true);
135+
136+
context.turnOffAuthorisationSystem();
137+
138+
WorkspaceItem wItem2;
139+
try (InputStream tgzFile = getClass().getResourceAsStream("logos.tgz")) {
140+
wItem2 = WorkspaceItemBuilder.createWorkspaceItem(context, collection)
141+
.withBitstream("logos.tgz", "/local/path/logos.tgz", tgzFile, SYNC_STORE_NUMBER)
142+
.build();
143+
}
144+
145+
context.restoreAuthSystemState();
146+
147+
// Get the item and its bitstream
148+
Item item2 = wItem2.getItem();
149+
List<Bundle> bundles = item2.getBundles();
150+
Bitstream bitstream2 = bundles.get(0).getBitstreams().get(0);
151+
152+
// Set the bitstream format to application/zip
153+
BitstreamFormat bitstreamFormat = bitstreamFormatService.findByMIMEType(context, "application/x-gtar");
154+
bitstream2.setFormat(context, bitstreamFormat);
155+
bitstreamService.update(context, bitstream2);
156+
context.commit();
157+
context.reloadEntity(bitstream2);
158+
context.reloadEntity(item2);
159+
160+
runScriptForItemWithBitstreams(item2);
161+
162+
Bitstream b2 = bitstreamService.findAll(context).stream()
163+
.filter(b -> b.getStoreNumber() == SYNC_STORE_NUMBER)
164+
.findFirst().orElse(null);
165+
166+
assertNotNull(b2);
167+
assertTrue("Expects preview content created and stored.", previewContentService.hasPreview(context, b2));
135168
}
136169

137170
@Test
@@ -150,4 +183,24 @@ private void checkNoError(TestDSpaceRunnableHandler testDSpaceRunnableHandler) {
150183
assertThat(testDSpaceRunnableHandler.getErrorMessages(), empty());
151184
assertThat(testDSpaceRunnableHandler.getWarningMessages(), empty());
152185
}
186+
187+
private void runScriptForItemWithBitstreams(Item item) throws Exception {
188+
// Run the script
189+
TestDSpaceRunnableHandler testDSpaceRunnableHandler = new TestDSpaceRunnableHandler();
190+
String[] args = new String[] { "file-preview", "-u", item.getID().toString(),
191+
"-e", eperson.getEmail(), "-p", PASSWORD};
192+
int run = ScriptLauncher.handleScript(args, ScriptLauncher.getConfig(kernelImpl),
193+
testDSpaceRunnableHandler, kernelImpl);
194+
assertEquals(0, run);
195+
// There should be no errors or warnings
196+
checkNoError(testDSpaceRunnableHandler);
197+
198+
// There should be an info message about generating the file previews for the specified item
199+
List<String> messages = testDSpaceRunnableHandler.getInfoMessages();
200+
assertThat(messages, hasSize(2));
201+
assertThat(messages, hasItem(containsString("Generate the file previews for the specified item with " +
202+
"the given UUID: " + item.getID())));
203+
assertThat(messages,
204+
hasItem(containsString("Authentication by user: " + eperson.getEmail())));
205+
}
153206
}
Binary file not shown.

0 commit comments

Comments
 (0)