通过Thrift从Chrome扩展保存到Evernote的二进制文件在Unicode转换后出现乱码



EDIT:这个问题归结为UTF-8转换,请在这里讨论:UTF-8:它应该保持字符串=编码(解码(字符串))吗?如果没有,如何实现?

我正在尝试使用非官方的Kanda软件的API将一个PDF文件从Chrome扩展保存到Evernote。本质上,它使用Thrift,并提供了根据Evernote API创建和发送数据结构的方法。为了进行测试,我使用了一个字节长度为2898的示例PDF文件。

当调用CreateNote()方法时,数据最终会进入SDK的fract.js,在那里进行UTF编码并放入适当的数据结构中。这些函数被称为:

writeString:function(str) {
var result = Array.prototype.map.call(this.encode(str).split(""), this.stringToHex); // <----- I tried to skip this encoding for the data body
this.writeI32(result.length);
for (var i = 0; i < result.length; i++) {
this.ra.push(result[i]);
}
},
...
encode:function(string) {
string = string.replace(/rn/g, "n");
var utftext = "";
for (var n = 0; n < string.length; n++) {
var c = string.charCodeAt(n);
if (c < 128) {
utftext += String.fromCharCode(c);
}
else if ((c > 127) && (c < 2048)) {
utftext += String.fromCharCode((c >> 6) | 192);
utftext += String.fromCharCode((c & 63) | 128);
}
else {
utftext += String.fromCharCode((c >> 12) | 224);
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
utftext += String.fromCharCode((c & 63) | 128);
}
}
return utftext;
},
...
writeI32:function(i32) {
var i32out = new Array();
i32out[0] = 0xff & i32 >> 24;
i32out[1] = 0xff & i32 >> 16;
i32out[2] = 0xff & i32 >> 8;
i32out[3] = 0xff & i32;
var result = Array.prototype.map.call(i32out, this.byteToHex);
for (var i = 0; i < 4; i++) {
this.ra.push(result[i]);
}
},

我真的不明白Thrift在这个级别上是如何工作的,也不明白为什么它需要对包括二进制附件主体在内的所有数据进行编码,但正如你所看到的,它导致PDF的数据(以及所有其他要传输的字符串数据)被UTF编码,所以.长度现在是3018字节。这一切都通过API,文件显示在Evernote前端(见图片),但它没有被解码回来,大小为3018字节,所以PDF被搞砸了。

Evernote前端上的结果图像

我试图通过仅对数据体跳过encoder()调用来强行执行解决方案,但这导致文件以某种方式被丢弃。

你能告诉我问题是我的误解、SDK还是Evernote后端,以及如何解决吗?我为此失眠了


参考:我通过XMLHttpRequest获得原始文件,如下所示:

function getLink(url, callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', document.getElementById('url1').value, true);
xhr.responseType = 'text';
xhr.overrideMimeType('text/plain; charset=x-user-defined');
xhr.onload = function(e) {
if (this.status == 200) {
// Note: .response instead of .responseText
alert("String(this.response) " + String(this.response).length);
alert("String(this.responseText) " + String(this.responseText).length);
blob = String(this.response);
AddNote1(url, blob, function(response) {
document.getElementById('result').innerHTML += String(response).replace(/n/g, "<br/>") + "<br/>";
document.getElementById('result').innerHTML += blob.slice(1, 20);
} );
}
};
xhr.send();
}

结果数据看起来不错,字符串长度为2898。然后,我继续添加示例中所述的注释。同样,它检查得很好,字节字符串原封不动地进入CreateNote(),所以这再次仅供参考:

function AddNote1(Name, ContentFile, callback)
{
var noteStore = get_NoteStore();
var note = new Note();
note.title = Name;
var na = new NoteAttributes();
//na.author = "someone";
na.sourceURL = Name;
na.source = "web.clip";
note.attributes = na;
var data = new Data();
var resource = new Resource();
binaryData = ContentFile;
data.size = binaryData.length;
alert(binaryData.length + '*' + ContentFile.length);
data.bodyHash = raw_md5(binaryData);
data.body = binaryData;
resource.mime = "application/pdf";
resource.data = data;
var resAttributes = new ResourceAttributes();
resAttributes.fileName = String(Name).replace(/^.*[/\]/g, '');
resource.attributes = resAttributes;
note.resources = Array(resource);
//important to set correct content
var content = "<?xml version="1.0" encoding="UTF-8"?>"
+ "<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd"><en-note>";
content += String("Oi Wei").replace(/n/g, "<br/>") + "<br/>";
content += "<en-media type="application/pdf" hash="" + md5(binaryData) + ""/>";
content += "</en-note>";
note.content = content;
//response is a created note
//callback function process response
var response = noteStore.createNote(Eventnote.Auth.get_auth_token(), note);
if (callback !== undefined) {
callback(response);
}

是否有任何Javascript utf8_decode()实现没有这个问题?

取自此处:

function encode_utf8( s )
{
return unescape( encodeURIComponent( s ) );
}
function decode_utf8( s )
{
return decodeURIComponent( escape( s ) );
}

最新更新