diff --git a/dspace-api/src/test/data/dspaceFolder/config/local.cfg b/dspace-api/src/test/data/dspaceFolder/config/local.cfg index c4c61bf77ff6..de6caf8880f7 100644 --- a/dspace-api/src/test/data/dspaceFolder/config/local.cfg +++ b/dspace-api/src/test/data/dspaceFolder/config/local.cfg @@ -272,4 +272,8 @@ lr.pid.community.configurations = community=*, prefix=123456789, type=local, can #### Authority configuration `authority.cfg` authority.controlled.dc.relation = true -handle.canonical.prefix = ${dspace.ui.url}/handle/ \ No newline at end of file +handle.canonical.prefix = ${dspace.ui.url}/handle/ + +### File preview ### +# File preview is enabled by default +file.preview.enabled = true diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java index 8208a662ce79..d2daabe9a4da 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java @@ -55,6 +55,7 @@ import org.dspace.core.Constants; import org.dspace.core.Context; import org.dspace.handle.service.HandleService; +import org.dspace.services.ConfigurationService; import org.dspace.storage.bitstore.service.BitstreamStorageService; import org.dspace.util.FileInfo; import org.dspace.util.FileTreeViewGenerator; @@ -98,6 +99,9 @@ public class MetadataBitstreamRestRepository extends DSpaceRestRepository findByHandle(@Parameter(value = "handle", required = true) String handle, @Parameter(value = "fileGrpType") String fileGrpType, @@ -398,6 +402,13 @@ public String getFileContent(InputStream inputStream) throws IOException { */ private boolean findOutCanPreview(Context context, Bitstream bitstream) throws SQLException, AuthorizeException { try { + // Check it is allowed by configuration + boolean isAllowedByCfg = configurationService.getBooleanProperty("file.preview.enabled", true); + if (!isAllowedByCfg) { + return false; + } + + // Check it is allowed by license authorizeService.authorizeAction(context, bitstream, Constants.READ); return true; } catch (MissingLicenseAgreementException e) { diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/MetadataBitstreamRestRepositoryIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/MetadataBitstreamRestRepositoryIT.java index 94f98998b8d3..59e39285bea1 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/MetadataBitstreamRestRepositoryIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/MetadataBitstreamRestRepositoryIT.java @@ -33,6 +33,7 @@ import org.dspace.content.Item; import org.dspace.content.service.clarin.ClarinLicenseResourceMappingService; import org.dspace.core.Constants; +import org.dspace.services.ConfigurationService; import org.dspace.util.FileTreeViewGenerator; import org.hamcrest.Matchers; import org.junit.Before; @@ -57,6 +58,9 @@ public class MetadataBitstreamRestRepositoryIT extends AbstractControllerIntegra @Autowired AuthorizeService authorizeService; + @Autowired + ConfigurationService configurationService; + @Before public void setup() throws Exception { context.turnOffAuthorisationSystem(); @@ -125,6 +129,40 @@ public void findByHandle() throws Exception { } + @Test + public void previewingIsDisabledByCfg() throws Exception { + boolean canPreview = configurationService.getBooleanProperty("file.preview.enabled", true); + // Disable previewing + configurationService.setProperty("file.preview.enabled", false); + // There is no restriction, so the user could preview the file + getClient().perform(get(METADATABITSTREAM_SEARCH_BY_HANDLE_ENDPOINT) + .param("handle", publicItem.getHandle()) + .param("fileGrpType", FILE_GRP_TYPE)) + .andExpect(status().isOk()) + .andExpect(content().contentType(contentType)) + .andExpect(jsonPath("$._embedded.metadatabitstreams").exists()) + .andExpect(jsonPath("$._embedded.metadatabitstreams").isArray()) + .andExpect(jsonPath("$._embedded.metadatabitstreams[*].name") + .value(Matchers.containsInAnyOrder(Matchers.containsString("Bitstream")))) + .andExpect(jsonPath("$._embedded.metadatabitstreams[*].description") + .value(Matchers.containsInAnyOrder(Matchers.containsString(bts.getFormatDescription(context))))) + .andExpect(jsonPath("$._embedded.metadatabitstreams[*].format") + .value(Matchers.containsInAnyOrder(Matchers.containsString( + bts.getFormat(context).getMIMEType())))) + .andExpect(jsonPath("$._embedded.metadatabitstreams[*].fileSize") + .value(Matchers.containsInAnyOrder(Matchers.containsString( + FileTreeViewGenerator.humanReadableFileSize(bts.getSizeBytes()))))) + .andExpect(jsonPath("$._embedded.metadatabitstreams[*].canPreview") + .value(Matchers.containsInAnyOrder(Matchers.is(false)))) + .andExpect(jsonPath("$._embedded.metadatabitstreams[*].fileInfo").exists()) + .andExpect(jsonPath("$._embedded.metadatabitstreams[*].checksum") + .value(Matchers.containsInAnyOrder(Matchers.containsString(bts.getChecksum())))) + .andExpect(jsonPath("$._embedded.metadatabitstreams[*].href") + .value(Matchers.containsInAnyOrder(Matchers.containsString(url)))); + + configurationService.setProperty("file.preview.enabled", canPreview); + } + @Test public void findByHandleEmptyFileGrpType() throws Exception { getClient().perform(get(METADATABITSTREAM_SEARCH_BY_HANDLE_ENDPOINT) diff --git a/dspace/config/clarin-dspace.cfg b/dspace/config/clarin-dspace.cfg index e51dca06ba8e..0064162af6ad 100644 --- a/dspace/config/clarin-dspace.cfg +++ b/dspace/config/clarin-dspace.cfg @@ -234,3 +234,7 @@ authority.controlled.dc.relation = true #nameConversion shibboleth.name.conversion.inputEncoding = ISO-8859-1 shibboleth.name.conversion.outputEncoding = UTF-8 + +### File preview ### +# File preview is enabled by default +file.preview.enabled = false