Skip to content

Instantly share code, notes, and snippets.

@Sunil02kumar
Created April 17, 2020 03:01
Show Gist options
  • Save Sunil02kumar/e00ddc3d3198fe1500028fd6a775d62f to your computer and use it in GitHub Desktop.
Save Sunil02kumar/e00ddc3d3198fe1500028fd6a775d62f to your computer and use it in GitHub Desktop.
Get the content (Blob) of file from Box and store/manipulate it in Salesforce
/*
Author: Sunil Kumar
Purpose: To get file content from BOX
*/
public class SK_BoxAPIUtilityClass {
//method to get file details from box
public static string findFileNameFromBox(string boxFileId,string access_token){
string filename='';
string endPointUrl='https://api.box.com/2.0/files/'+boxFileId;
endPointUrl = endPointUrl + '?fields=name,id';
system.debug('***endPointUrl:'+endPointUrl);
Httprequest req1 = new HttpRequest();
req1.setEndpoint(endPointUrl);
req1.setMethod('GET');
req1.setHeader('Authorization', 'Bearer '+access_token);
req1.setHeader('Content-Type','application/json');
HttpResponse response = new HttpResponse();
try{
response = performCallout(req1);
system.debug('response *** '+response.getBody());
system.debug('***response.getStatusCode():'+response.getStatusCode());
//200 will be returned
if(response.getStatusCode() == 200){
string resString =response.getBody();
//{"type":"file","id":"654293628604","etag":"0","name":"ADR_Domain_List__c.csv"}
JSONParser parser = JSON.createParser(resString);
while (parser.nextToken() != null) {
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME)){
String fieldName = parser.getText();
if(fieldName == 'name') {
parser.nextToken();
filename=parser.getText();
}
}
}
}
}catch(exception ex){
system.debug('****some error occured in findFileNameFromBox method:'+ex.getMessage());
}
system.debug('***filename:'+filename);
return filename;
}
//method to get file download URL from box
public static string readFileContentFromBox(string boxFileId,string filename,string libraryName,string access_token){
string resString ='';
string endPointUrl='https://api.box.com/2.0/files/'+boxFileId+'/content';
Httprequest req1 = new HttpRequest();
req1.setEndpoint(endPointUrl);
req1.setMethod('GET');
req1.setHeader('Authorization', 'Bearer '+access_token);
req1.setHeader('Content-Type','application/json');
HttpResponse response = new HttpResponse();
try{
response = performCallout(req1);
system.debug('response *** '+response.getBody());
system.debug('***response.getStatusCode():'+response.getStatusCode());
string fileLocation=response.getHeader('Location');
String extFileURL=fileLocation;
blob fileBlob=fetchFileFromExternalUrl(extFileURL);
Id fileId = addFileToLibrary(fileBlob, libraryName ,filename);
system.debug('*****fileId:'+fileId);
resString = fileId;
}catch(exception ex){
resString = ex.getMessage();
}
return resString;
}
//method to get file content as blob from publically available external URL
public static blob fetchFileFromExternalUrl(String extFileUrl){
Http h = new Http();
HttpRequest req = new HttpRequest();
//Replace any spaces in extFileUrl with %20
extFileUrl = extFileUrl.replace(' ', '%20');
//Set the end point URL
req.setEndpoint(extFileUrl);
req.setMethod('GET');
req.setHeader('Content-Type', 'application/pdf');
req.setCompressed(true);
req.setTimeout(60000);
//Now Send HTTP Request
HttpResponse res = h.send(req);
system.debug('Response from Server: ' + res.getBody());
//getBodyAsBlob method was will convert the response into Blob
blob retFile = res.getBodyAsBlob();
return retFile;
}
//method to upload file in library-filename should include extension
public static string addFileToLibrary(blob contentBlob,string LibraryNameForUpload,string filename){
ContentVersion cv = new ContentVersion();
cv.VersionData = contentBlob;
cv.Title = filename;
cv.PathOnClient = filename;
insert cv;
cv = [SELECT Id, ContentDocumentId FROM ContentVersion WHERE Id = :cv.Id LIMIT 1];
ContentWorkspace ws = [SELECT Id, RootContentFolderId FROM ContentWorkspace WHERE developerName =:LibraryNameForUpload LIMIT 1];
ContentDocumentLink cdl = new ContentDocumentLink();
cdl.ContentDocumentId = cv.ContentDocumentId;
cdl.ShareType = 'I';
cdl.LinkedEntityId = ws.Id;
insert cdl;
return cdl.Id;
}
public static HttpResponse performCallout(Httprequest req){
Http h1 = new Http();
HttpResponse resp = new HttpResponse();
if(!Test.isRunningTest()){
resp= h1.send(req);
}
return resp;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment