Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Upload Salesforce Static Resource to Aspose Storage
<apex:page controller="AsposeStorageController">
<apex:form id="theForm">
<apex:pageBlock title="Requirements" rendered="true">
<ol>
<li>Register aspose account.</li>
<li>Fill in the form below and click Run Sample (all fields are required).</li>
</ol>
</apex:pageBlock>
<apex:pageBlock title="Upload Static Resources to Aspose Storage" rendered="true">
<table>
<tr>
<td><b>App SID:</b></td>
<td><apex:inputText value="{!appSID}" size="100"/></td>
</tr>
<tr>
<td><b>App Key:</b></td>
<td><apex:inputText value="{!appKey}" size="100"/></td>
</tr>
<tr>
<td><b>Resource Name:</b></td>
<td><apex:inputText value="{!fileName}" size="100"/></td>
</tr>
<tr>
<td><b>Save Format:</b> (pdf,tiff,html)</td>
<td><apex:inputText value="{!saveFormat}" size="100"/></td>
</tr>
</table>
<div class="container">
<apex:commandButton action="{!runSample}" value="Run Sample" /><br/>
<p>
<span>Output: </span>
<apex:outputLabel >{!output}</apex:outputLabel>
</p>
</div>
</apex:pageBlock>
</apex:form>
</apex:page>
public with sharing class AsposeStorageController {
public string appSID {get; set;}
public string appKey {get; set;}
public string fileName {get; set;}
public string saveFormat {get; set;}
public string output {get; set;}
public PageReference runSample() {
Product.BaseProductUri = 'http://api.aspose.com/v1.1';
AsposeCloudApp.setAppInfo(appKey, appSID);
//build URI
String strURI = Product.BaseProductUri + '/storage/file/' + Filename + '.' + saveFormat;
String signedURI = Utils.Sign(strURI);
Blob contents = [select id,body from StaticResource Where Name = :Filename].body;
String strJSON = Utils.ProcessCommand(signedURI, 'PUT',contents);
this.output = strJSON;
return null;
}
}
public with sharing class Product {
public static String BaseProductUri { get; set; }
}
public with sharing class Utils {
public static String Sign(String data) {
String HMAC_SHA1 = 'HmacSHA1';
try {
data = data.replace(' ', '%20');
URL url = new URL(data);
String path = url.getPath();
path = path.replace(' ', '%20');
// Remove final slash here as it can be added automatically.
path = path.removeEnd('/');
String filePart = url.getPath() + (url.getQuery() == null ? '?' : '?' + url.getQuery()) + '&appSID=' + AsposeCloudApp.AppSID;
url = new URL(url.getProtocol(), url.getHost(), url.getPort(), filePart);
// compute the hmac on input data bytes
Blob mac = Crypto.generateMac(HMAC_SHA1, Blob.valueOf(url.toExternalForm()), Blob.valueOf(AsposeCloudApp.AppKey));
String base64 = EncodingUtil.base64Encode(mac);
system.debug('base64: ' + base64);
// Remove invalid symbols.
String result = base64.substring(0, base64.length() - 1);
result = EncodingUtil.urlEncode(result, 'UTF-8');
system.debug('signature: ' + result);
String encodedUrl = url.toExternalForm() + '&signature=' + result;
system.debug('full URL: ' + encodedUrl);
return encodedUrl;
} catch (Exception ex) {
system.debug(ex.getStackTraceString());
return null;
}
}
public static String ProcessCommand(String strURI, String strHttpCommand, Blob content, String ContentType) {
try {
HttpRequest request = new HttpRequest();
Integer len = 0;
if (strContent != null) {
request.setBodyAsBlob(content);
}
request.setEndpoint(strURI);
request.setMethod(strHttpCommand);
if (ContentType.toLowerCase() == 'xml')
request.setHeader('Content-Type', 'application/xml');
else
request.setHeader('Content-Type', 'application/json');
request.setHeader('Accept', 'application/json');
//request.setTimeout(Timeout);
Http http = new Http();
HttpResponse res = http.send(request);
return res.getBody();
} catch (Exception ex) {
system.debug('HTTP ERROR' + ex.getMessage());
system.debug(ex.getStackTraceString());
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.