Guest User

Untitled

a guest
Jan 16th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. // use reluctant regex to match each image tag individually
  2. // https://docs.oracle.com/javase/tutorial/essential/regex/quant.html
  3. Matcher imgMatcher = Pattern.compile( '<img(.+?)>' ).matcher( record.richTextField__c );
  4.  
  5. // iterate each image tag found
  6. while ( imgMatcher.find() ) {
  7.  
  8. // get the image tag html
  9. String imageTag = imgMatcher.group();
  10. System.debug( 'imageTag=' + imageTag );
  11.  
  12. // get the value of the src attribute
  13. // the leading space is significant to avoid other attributes like data-cke-saved-src
  14. String imageURL = imageTag.substringBetween( ' src="', '"' );
  15. System.debug( 'imageURL=' + imageURL );
  16.  
  17. // if url contained parameters they might be html escaped, unescape them
  18. // or, more conservatively, replace '&' with '&'
  19. String decodedURL = imageURL.unescapeHtml4();
  20. System.debug( 'decodedURL=' + decodedURL );
  21.  
  22. // note, as of API 34.0 or later, getContent() is considered an http callout
  23. // so take that into consideration for your unit tests and governor limits
  24. // https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/apex_System_PageReference_getContent.htm
  25. PageReference page = new PageReference( decodedURL );
  26. Blob b = page.getContent();
  27. System.debug( 'blob=' + b );
  28.  
  29. System.debug( 'Enjoy your Blob, save it as a Document, ContentVersion, whatever!' );
  30.  
  31. System.debug(''); // I like blank lines in my logs, easier to scan/read =)
  32.  
  33. }
  34.  
  35. // This location is an example from my org; will need to be different for your org
  36. String location = 'https://c.na15.content.force.com/servlet/rtaImage?eid=500...';
  37. do {
  38. HttpRequest req = new HttpRequest();
  39. req.setEndpoint(location);
  40. req.setMethod('GET');
  41. HttpResponse res = new Http().send(req);
  42. if (res.getStatusCode() == 302) {
  43. location = res.getHeader('Location');
  44. } else if (res.getStatusCode() == 200) {
  45. location = null;
  46. Blob b = res.getBodyAsBlob();
  47. // Return the Blob
  48. } else {
  49. // Error
  50. }
  51. } while (location != null);
Add Comment
Please, Sign In to add comment