From 1635bae730d42d9a90eb309ff3c889a7b24f6617 Mon Sep 17 00:00:00 2001 From: milanmajchrak Date: Thu, 14 Dec 2023 13:09:31 +0100 Subject: [PATCH] Made previewing of the file configurable. --- .../MetadataBitstreamRestRepository.java | 11 ++++++ .../MetadataBitstreamRestRepositoryIT.java | 35 +++++++++++++++++++ dspace/config/clarin-dspace.cfg | 4 +++ 3 files changed, 50 insertions(+) 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..861d7408f58c 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,37 @@ public void findByHandle() throws Exception { } + @Test + public void previewingIsDisabledByCfg() throws Exception { + // 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)))); + } + @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