Skip to content

Commit

Permalink
Made previewing of the file configurable.
Browse files Browse the repository at this point in the history
  • Loading branch information
milanmajchrak committed Dec 14, 2023
1 parent 989e6e8 commit 1635bae
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -98,6 +99,9 @@ public class MetadataBitstreamRestRepository extends DSpaceRestRepository<Metada
@Autowired
AuthorizationBitstreamUtils authorizationBitstreamUtils;

@Autowired
ConfigurationService configurationService;

@SearchRestMethod(name = "byHandle")
public Page<MetadataBitstreamWrapperRest> findByHandle(@Parameter(value = "handle", required = true) String handle,
@Parameter(value = "fileGrpType") String fileGrpType,
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -57,6 +58,9 @@ public class MetadataBitstreamRestRepositoryIT extends AbstractControllerIntegra
@Autowired
AuthorizeService authorizeService;

@Autowired
ConfigurationService configurationService;

@Before
public void setup() throws Exception {
context.turnOffAuthorisationSystem();
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions dspace/config/clarin-dspace.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 1635bae

Please sign in to comment.