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 with sharing class ContentDownloadHandlerFactoryImpl implements Sfc.ContentDownloadHandlerFactory {
3
4    public 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        } else {
11            contentDownloadHandler.isDownloadAllowed = false;
12            contentDownloadHandler.downloadErrorMessage = 'This file needs to be IRM controlled. You are not allowed to download it';
13            contentDownloadHandler.redirectUrl ='/apex/IRMControl?Id='+ids.get(0);
14            return contentDownloadHandler;
15        }
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 with sharing class IRMController {
2    private String downloadEndpoint;
3
4    public IRMController() {
5        downloadEndpoint = '';
6    }
7
8    public void applyIrmControl() {
9        String versionId = ApexPages.currentPage().getParameters().get('id');
10        Http h = new Http();
11
12        //Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
13        HttpRequest req = new HttpRequest();
14        req.setEndpoint('http://irmsystem?versionId=' + versionId);
15        req.setMethod('GET');
16
17        // Send the request, and retrieve a response
18        HttpResponse r = h.send(req);
19        JSONParser parser = JSON.createParser(r.getBody());
20        while (parser.nextToken() != null) {
21            if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&
22                (parser.getText() == 'downloadEndpoint')) {
23                parser.nextToken();
24                downloadEndpoint = parser.getText();
25                break;
26            }
27        }
28    }
29
30    public String getDownloadEndpoint() {
31        return downloadEndpoint;
32    }
33}

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 with sharing class ContentDownloadHandlerFactoryImpl implements Sfc.ContentDownloadHandlerFactory {
3
4    public 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 is not allowed.';
10            return contentDownloadHandler;
11        } else {
12            contentDownloadHandler.isDownloadAllowed = true;
13            return contentDownloadHandler;
14        }
15    }
16}

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 with sharing class ContentDownloadHandlerFactoryImpl implements Sfc.ContentDownloadHandlerFactory {
3
4    public 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 is not allowed.';
14            return contentDownloadHandler;
15        } else {
16            contentDownloadHandler.isDownloadAllowed = false;
17            contentDownloadHandler.downloadErrorMessage = 'This file needs to be IRM controlled. You are not allowed to download it';
18            contentDownloadHandler.redirectUrl ='/apex/IRMControl?Id='+ids.get(0);
19            return contentDownloadHandler;
20        }
21    }
22}