Skip to content

Instantly share code, notes, and snippets.

@dhaniksahni
Last active June 4, 2024 07:10
Show Gist options
  • Save dhaniksahni/d9595e8c6e60f1f013f365ccca7b71d5 to your computer and use it in GitHub Desktop.
Save dhaniksahni/d9595e8c6e60f1f013f365ccca7b71d5 to your computer and use it in GitHub Desktop.
Reading Files
public without sharing class FileReader {
private static final String BASE_URL = URL.getSalesforceBaseUrl().toExternalForm();
//Reterives all files of one case or account record
@AuraEnabled
public static List<FileData> GetEntityRecordFiles(string recordId)
{
List<ContentDocumentLink> links=[SELECT ContentDocumentId,LinkedEntityId FROM ContentDocumentLink where LinkedEntityId=:recordId];
Set<Id> ids=new Set<Id>();
for(ContentDocumentLink link:links)
{
ids.add(link.ContentDocumentId);
}
List<ContentVersion> versions=[SELECT VersionData,Title,ContentDocumentId,FileExtension FROM ContentVersion WHERE ContentDocumentId = :ids AND IsLatest = true];
List<FileData> files=new List<FileData>();
for(ContentVersion attach:versions)
{
FileData data=new FileData();
if(versions!=null && versions.size()>0)
{
data.Content = EncodingUtil.base64Encode(versions[0].VersionData);
data.ContentType = ContentType(versions[0].FileExtension);
}
data.DownloadUrl =BASE_URL + '/sfc/servlet.shepherd/document/download/'+versions[0].ContentDocumentId;
data.FileUrl =BASE_URL + '/sfc/servlet.shepherd/version/renditionDownload?rendition=THUMB720BY480&versionId='+versions[0].Id;
files.add(data);
}
return files;
}
//Reterives specific file information
@AuraEnabled
public static FileData GetFile(string contentDocumentId)
{
List<ContentVersion> versions=[SELECT Id,VersionData,Title,ContentDocumentId,FileExtension FROM ContentVersion WHERE ContentDocumentId = :contentDocumentId AND IsLatest = true];
FileData data=new FileData();
if(versions!=null && versions.size()>0)
{
data.Content = EncodingUtil.base64Encode(versions[0].VersionData);
data.ContentType = ContentType(versions[0].FileExtension);
}
data.DownloadUrl =BASE_URL + '/sfc/servlet.shepherd/document/download/'+versions[0].ContentDocumentId;
data.FileUrl =BASE_URL + '/sfc/servlet.shepherd/version/renditionDownload?rendition=THUMB720BY480&versionId='+versions[0].Id;
return data;
}
public static string ContentType(string fileType)
{
switch on fileType.toLowerCase()
{
when 'docx'
{
return 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
}
when 'csv'
{
return 'application/vnd.ms-excel';
}
when 'wav'
{
return 'audio/wav';
}
when 'wmv'
{
return 'video/x-ms-wmv';
}
when 'mp3'
{
return 'audio/mpeg';
}
when 'mp4'
{
return 'video/mp4';
}
when 'png'
{
return 'image/png';
}
when 'pdf'
{
return 'application/pdf';
}
when else {
return 'image/jpeg';
}
}
}
public class FileData
{
@AuraEnabled
public string Content{get;set;}
@AuraEnabled
public string ContentType{get;set;}
@AuraEnabled
public string FileUrl{get;set;}
@AuraEnabled
public string DownloadUrl{get;set;}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment