From a09ff973e6fb4fa9e0d24993f63d26dac2ede52a Mon Sep 17 00:00:00 2001 From: life Date: Mon, 26 Oct 2015 19:48:00 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=9A=E8=BF=87hash=E7=B4=A2=E5=BC=95?= =?UTF-8?q?=E6=96=87=E4=BB=B6,=20=E8=80=8C=E4=B8=8D=E6=98=AF=E6=8C=89?= =?UTF-8?q?=E9=A1=BA=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- node_modules/file.js | 17 ++++++++++++-- public/plugins/import_evernote/import.js | 29 ++++++++++++++++-------- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/node_modules/file.js b/node_modules/file.js index 49d28aa2..87ef73ba 100644 --- a/node_modules/file.js +++ b/node_modules/file.js @@ -49,17 +49,30 @@ var File = { // return; try { - var err = fs.writeFileSync(filePath, new Buffer(data, 'base64')); + var bf = new Buffer(data, 'base64'); + var err = fs.writeFileSync(filePath, bf); if(err) { return callback(false); } + + // 得到文件的hash + var hash = crypto.createHash('md5'); + hash.setEncoding('hex'); + hash.write(bf); + hash.end(); + var fileHash = hash.read(); + if(isImage) { me._addImage(Common.objectId(), filePath, function(newImg) { newImg.IsImage = true; + newImg.hash = fileHash; callback && callback(newImg); }); } else { - me._addAttach(filePath, fileTitle, callback) + me._addAttach(filePath, fileTitle, function(attach) { + attach.hash = fileHash; + callback && callback(attach); + }); } /* diff --git a/public/plugins/import_evernote/import.js b/public/plugins/import_evernote/import.js index fa2b1a15..ea6871f5 100755 --- a/public/plugins/import_evernote/import.js +++ b/public/plugins/import_evernote/import.js @@ -20,7 +20,7 @@ var Import = { \n' 把中间部分抽出 - parsedRes = [{FileId: ""}] + parsedRes = {hash1: file, hash2: file} */ parseEvernoteContent: function(xml, parsedRes) { var me = this; @@ -39,19 +39,29 @@ var Import = { return ''; } var reg = new RegExp("", "g"); // - var i = 0; // console.log(content); while(ret = reg.exec(content)) { // ret[1] == type="text/html" style="cursor:pointer;" height="43" hash="bc322a11075e40f3a5b2dce3f2fffcdc" try { - var res = parsedRes[i]; - i++; + var attrs = ret[1]; + + // 得到hash + var hashes = attrs.match(/hash="([0-9a-zA-Z]{32})"/); + var hash = ''; + if (hashes) { + hash = hashes[1]; + } + if (!hash) { + continue; + } + + var res = parsedRes[hash]; var fileId = res['FileId']; if(!fileId) { continue; } if(res.IsImage) { - var replace = ''; + var replace = ''; } else { var replace = '' + res['Title'] + '' } @@ -64,7 +74,6 @@ var Import = { // 如果是, 匹配不到 content = content.replace(/<\/en-media>/g, ''); - return content; }, @@ -87,7 +96,7 @@ var Import = { // 文件保存之 var resources = note['resource'] || []; - var parsedRes = []; + var parsedRes = {}; var attachs = []; // console.log("-----------") // console.log(note); @@ -133,7 +142,8 @@ var Import = { File.writeBase64(base64Str, isImage, type, filename, function(file) { if(file) { - parsedRes.push(file); + parsedRes[file.hash] = file; + // parsedRes.push(file); if(!isImage) { attachs.push(file); } @@ -147,13 +157,14 @@ var Import = { // 把content的替换之 // console.log('ok, writeBase64 ok'); try { + console.log('parsedRes'); console.log(parsedRes); jsonNote.Content = me.parseEvernoteContent(jsonNote.Content, parsedRes); jsonNote.Attachs = attachs; } catch(e) { console.log(e); } - console.log(jsonNote); + // console.log(jsonNote); return callback && callback(jsonNote); } );