Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ufal/zip-preview-configurable #475

Merged
merged 3 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion dspace-api/src/test/data/dspaceFolder/config/local.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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/
handle.canonical.prefix = ${dspace.ui.url}/handle/

### File preview ###
# File preview is enabled by default
file.preview.enabled = true
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,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)
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
Loading