Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine MIME type of copy-pasted image clipboard?

Can I assume that Mac OS X clipboard image data is png?

When I try to MIME-detect the clipboard data, it returns application/octet.

This has the undesirable effect of causing every browser to download the image rather than display it.

If I force the content-type to image/png, everything seems fine, but I wondered if there is a way for me to not have to make the assumption?

$log.debug(e.originalEvent.clipboardData);
for (var i = 0; i < e.originalEvent.clipboardData.items.length; i++) {
var item = e.originalEvent.clipboardData.items[i];
$log.info("Item type: " ,item);
if (item.type.indexOf("image") != -1) {
    $scope.token.images = [];
    $log.debug(item.getAsFile(), {});
...

I use https://github.com/broofa/node-mime to detect MIME types.

like image 858
metalaureate Avatar asked Aug 03 '15 14:08

metalaureate


People also ask

How do I specify a MIME type?

In the Connections pane, go to the site, application, or directory for which you want to add a MIME type. In the Home pane, double-click MIME Types. In the MIME Types pane, click Add... in the Actions pane. In the Add MIME Type dialog box, add the file name extension and MIME type, and then click OK.


1 Answers

At least in theory, no, you can't assume that.

Here is what apple says: "To get a list of types currently available on the clipboard, you can use the types property of the clipboardData object. This property contains an array of strings with the MIME types of the available data." Webkit DOM Programming Topics

At least according to w3c the options are:

  • image/png
  • image/jpg
  • image/jpeg
  • image/gif
  • image/svg+xml

edit:

The reason the MIME-detect returns application/octet is that the library you are using falls back to that when it doesn't know the type as you can see in this line of the test.js file:

assert.equal('fallback', mime.charsets.lookup('application/octet-stream', 'fallback'));

This answer explains in more detail how to deal with this "arbitrary binary data."

like image 162
Sam H. Avatar answered Sep 28 '22 02:09

Sam H.