Newer Version Available

This content describes an older version of this product. View Latest

Custom File Download Examples

You can use Apex to customize the behavior of files upon attempted download. These examples assume that only one file is being downloaded. File download customization is available in API version 39.0 and later.

Example

This example demonstrates a system that requires downloads to go through IRM control for some users. For a Modify All Data (MAD) user who’s allowed to download files, and whose user ID is 005xx:
1// Allow customization of the content Download experience
2public class ContentDownloadHandlerFactoryImpl implements Sfc.ContentDownloadHandlerFactory {
3
4public Sfc.ContentDownloadHandler getContentDownloadHandler(List<ID> ids, Sfc.ContentDownloadContext context) {
5    Sfc.ContentDownloadHandler contentDownloadHandler = new Sfc.ContentDownloadHandler();
6
7    if(UserInfo.getUserId() == '005xx') {
8        contentDownloadHandler.isDownloadAllowed = true;
9        return contentDownloadHandler;
10    }
11    
12    contentDownloadHandler.isDownloadAllowed = false;
13    contentDownloadHandler.downloadErrorMessage = 'This file needs to be IRM controlled. You're not allowed to download it';
14    contentDownloadHandler.redirectUrl ='/apex/IRMControl?Id='+ids.get(0);
15    return contentDownloadHandler;
16}
17}

To refer to a MAD user profile, you can use UserInfo.getProfileId() instead of UserInfo.getUserId().

Note

In this example, IRMControl is a Visualforce page created for displaying a link to download a file from the IRM system. You need a controller for this page that calls your IRM system. As it’s processing the file, it gives an endpoint to download the file when it’s controlled. Your IRM system uses the sObject API to get the VersionData of this ContentVersion. Therefore, the IRM system needs the VersionID and must retrieve the VersionData using the MAD user.

Your IRM system is at http://irmsystem and is expecting the VersionID as a query parameter. The IRM system returns a JSON response with the download endpoint in a downloadEndpoint value.

1public class IRMController {
2    
3private String downloadEndpoint;
4    
5public IRMController() {
6    downloadEndpoint = '';
7}
8    
9public void applyIrmControl() {
10    String versionId = ApexPages.currentPage().getParameters().get('id');
11    Http h = new Http();
12
13    //Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
14    HttpRequest req = new HttpRequest();
15    req.setEndpoint('http://irmsystem?versionId=' + versionId);
16    req.setMethod('GET');
17
18    // Send the request, and retrieve a response
19    HttpResponse r = h.send(req);
20    JSONParser parser = JSON.createParser(r.getBody());
21      while (parser.nextToken() != null) {
22        if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&
23            (parser.getText() == 'downloadEndpoint')) {
24                parser.nextToken();
25                downloadEndpoint = parser.getText();
26                break;
27        }
28    }
29}
30    
31public String getDownloadEndpoint() {
32    return downloadEndpoint;
33}
34    
35}

Example

The following example creates a class that implements the ContentDownloadHandlerFactory interface and returns a download handler that prevents downloading a file to a mobile device.
1// Allow customization of the content Download experience
2public class ContentDownloadHandlerFactoryImpl implements Sfc.ContentDownloadHandlerFactory {
3
4public Sfc.ContentDownloadHandler getContentDownloadHandler(List<ID> ids, Sfc.ContentDownloadContext context) {
5    Sfc.ContentDownloadHandler contentDownloadHandler = new Sfc.ContentDownloadHandler();
6    
7    if(context == Sfc.ContentDownloadContext.MOBILE) {
8        contentDownloadHandler.isDownloadAllowed = false;
9        contentDownloadHandler.downloadErrorMessage = 'Downloading a file from a mobile device isn't allowed.';
10        return contentDownloadHandler;
11    }
12    contentDownloadHandler.isDownloadAllowed = true;
13    return contentDownloadHandler;
14}

Example

You can also prevent downloading a file from a mobile device and require that a file must go through IRM control.
1// Allow customization of the content Download experience
2public class ContentDownloadHandlerFactoryImpl implements Sfc.ContentDownloadHandlerFactory {
3
4public Sfc.ContentDownloadHandler getContentDownloadHandler(List<ID> ids, Sfc.ContentDownloadContext context) {
5    Sfc.ContentDownloadHandler contentDownloadHandler = new Sfc.ContentDownloadHandler();
6
7    if(UserInfo.getUserId() == '005xx000001SvogAAC') {
8        contentDownloadHandler.isDownloadAllowed = true;
9        return contentDownloadHandler;
10    }
11    if(context == Sfc.ContentDownloadContext.MOBILE) {
12        contentDownloadHandler.isDownloadAllowed = false;
13        contentDownloadHandler.downloadErrorMessage = 'Downloading a file from a mobile device isn't allowed.';
14        return contentDownloadHandler;
15    }
16    
17    contentDownloadHandler.isDownloadAllowed = false;
18    contentDownloadHandler.downloadErrorMessage = 'This file needs to be IRM controlled. You're not allowed to download it';
19    contentDownloadHandler.redirectUrl ='/apex/IRMControl?Id='+id.get(0);
20    return contentDownloadHandler;
21}
22}