Text Share Online

public with sharing class IFB_ALL_FileDownloadQueueable implements Queueable, Database.AllowsCallouts {

private final String preSignedUrl;
private final String fileName;
private final Id recordId;

public IFB_ALL_FileDownloadQueueable(String preSignedUrl, String fileName, Id recordId) {
this.preSignedUrl = preSignedUrl;
this.fileName = fileName;
this.recordId = recordId;
}

public void execute(QueueableContext context) {
if (String.isBlank(preSignedUrl)) {
throw new IllegalArgumentException(‘preSignedUrl is required.’);
}
if (String.isBlank(fileName)) {
throw new IllegalArgumentException(‘fileName is required.’);
}
if (recordId == null) {
throw new IllegalArgumentException(‘recordId is required.’);
}

HttpRequest req = new HttpRequest();
req.setEndpoint(preSignedUrl);
req.setMethod(‘GET’);
req.setTimeout(120000);

Http http = new Http();

try {
HttpResponse res = http.send(req);
if (res.getStatusCode() != 200) {
throw new CalloutException(‘Failed to download file. Status: ‘ + res.getStatusCode() +’ – ‘ + res.getStatus());
}
ContentVersion cv = new ContentVersion(Title = fileName,PathOnClient = fileName,VersionData = res.getBodyAsBlob());
if (recordId != null) {
cv.FirstPublishLocationId = recordId;
insert cv;
}
System.debug(LoggingLevel.INFO,’ContentVersion created successfully. Id: ‘ + cv.Id);
} catch (Exception ex) {
System.debug(LoggingLevel.ERROR,’File download failed. Error: ‘ + ex.getMessage());
throw ex;
}
}

/**
* Helper method to enqueue the job.
*/
public static Id enqueueJob(String preSignedUrl, String fileName, Id recordId) {
return System.enqueueJob(new IFB_ALL_FileDownloadQueueable(preSignedUrl, fileName, recordId));
}
}

  • none/plain text
  • 1h
Share This: