MIME type is required to be added dynamically when attaching a document.

imkhan
Tera Contributor

Hi Everyone,

We are getting attachment data from a 3rd party in base 64, so we had to decode it. It is working fine, and a MIME ID is needed to set it based on the attachment. If it is a PNG type attachment, then the MIME type should be image/png. How to achieve this.

 

fetchAndAddAttachments: function(docID, reqNo, accesstoken, fileName, mimeType) {
try {

var r = new sn_ws.RESTMessageV2('Integration-D', 'Download');
r.setStringParameterNoEscape('access_token', accesstoken);
r.setStringParameterNoEscape('DocumentID', docID);
var response = r.execute();
var responseBodyText = response.getBody();
var httpStatus = response.getStatusCode();
gs.info("fetchAndAddAttachments: Response Status" + httpStatus);
gs.info("fetchAndAddAttachments: Response Body" + responseBodyText);

if (httpStatus != 200) {
gs.error('fetchAndAddAttachments: Failed to fetch' + docID);
return;
}

var responseBody;
try {
responseBody = JSON.parse(responseBodyText);
} catch (e) {
gs.error("fetchAndAddAttachments: JSON Parse Error " + e.message);
return;
}


var base64Content = responseBody.Data;
var grAttachment = new GlideSysAttachment();
var targetTable = 'incident';
var target = new GlideRecord(targetTable);
target.addQuery('number', reqNo);
target.query();

if (target.next()) {
 grAttachment.write(target, fileName, mimeType, GlideStringUtil.base64DecodeAsBytes(base64Content));

}

Thanks!

5 REPLIES 5

Robert H
Mega Sage

Hello @imkhan ,

 

Ideally the MIME type should be passed back by whatever API you are calling here. Is it not mentioned somewhere in the response headers or body?

Else you would have to implement a guessing algorithm based on so called "magic numbers". Here are some details about that.

 

Regards,

Robert

Robert H
Mega Sage

Hello @imkhan ,

 

Ideally the MIME type should be passed back by whatever API you are calling here. Is it not mentioned somewhere in the response headers or body?

Else you would have to implement a guessing algorithm based on so called "magic numbers". Please google for "how to get mime type from base 64 string" as I am not allowed to paste external links.

 

Here is a quick example:

var signatures = {
  JVBERi0: "application/pdf",
  R0lGODdh: "image/gif",
  R0lGODlh: "image/gif",
  iVBORw0KGgo: "image/png",
  "/9j/": "image/jpg"
};

function detectMimeType(b64) {
  for (var s in signatures) {
    if (b64.indexOf(s) === 0) {
      return signatures[s];
    }
  }
}

gs.info(detectMimeType('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR42mP4z8AAAAMBAQD3A0FDAAAAAElFTkSuQmCC'));

Result:

image/png

 

Regards,

Robert

imkhan
Tera Contributor

Hi @Robert H ,

 

Thanks for reply!

Do we need to define the MIME type in our script, where the script checks the attachment and adds the MIME types accordingly? Or is there an alternative way to achieve this without defining the MIME types in the script, such as using a ServiceNow system property or another method?

Hello @imkhan ,

 

From a re-usability perspective I would recommend putting the MIME type detection code into a Script Include.

 

In your fetchAndAddAttachments() function you would then add something like this to have the MIME type guessed:

let mimeType = new YourScriptIncludeName().detectMimeType(base64Content);

But as I had mentioned before, it would be best if the API that you are calling returns the MIME type itself. Have you checked that?

 

Regards,

Robert