ファイルから data URL を生成
そういう関数が用意されていてもよさそうな気もするけど.
MIMEService で MIME type を得ているけど,どうやら拡張子から判断してるだけっぽい.
function toDataURL(file) { // assert(file instanceof Ci.nsIFile) let type = 'plain/text'; try { type = Cc['@mozilla.org/mime;1'].getService(Ci.nsIMIMEService).getTypeFromFile(file); } catch(e) {} let ifstream = Cc['@mozilla.org/network/file-input-stream;1'].createInstance(Ci.nsIFileInputStream); ifstream.init(file, 1 /* RDONLY */, 0600, 0); let stream = Cc['@mozilla.org/binaryinputstream;1'].createInstance(Ci.nsIBinaryInputStream); stream.setInputStream(ifstream); let b64 = btoa(stream.readBytes(stream.available())); stream.close(); ifstream.close(); return 'data:' + type + ';base64,' + b64; }
Vimp から利用するときは :js toDataURL(io.File('/path/to/file')) というように使うと,/path/to/file のときに補完がきくし ~/path/to/file のような指定もできて便利.