通过hash索引文件, 而不是按顺序

This commit is contained in:
life
2015-10-26 19:48:00 +08:00
parent 7ce5c90de8
commit a09ff973e6
2 changed files with 35 additions and 11 deletions

17
node_modules/file.js generated vendored
View File

@@ -49,17 +49,30 @@ var File = {
// return; // return;
try { try {
var err = fs.writeFileSync(filePath, new Buffer(data, 'base64')); var bf = new Buffer(data, 'base64');
var err = fs.writeFileSync(filePath, bf);
if(err) { if(err) {
return callback(false); return callback(false);
} }
// 得到文件的hash
var hash = crypto.createHash('md5');
hash.setEncoding('hex');
hash.write(bf);
hash.end();
var fileHash = hash.read();
if(isImage) { if(isImage) {
me._addImage(Common.objectId(), filePath, function(newImg) { me._addImage(Common.objectId(), filePath, function(newImg) {
newImg.IsImage = true; newImg.IsImage = true;
newImg.hash = fileHash;
callback && callback(newImg); callback && callback(newImg);
}); });
} else { } else {
me._addAttach(filePath, fileTitle, callback) me._addAttach(filePath, fileTitle, function(attach) {
attach.hash = fileHash;
callback && callback(attach);
});
} }
/* /*

View File

@@ -20,7 +20,7 @@ var Import = {
</en-note>\n' </en-note>\n'
把<en-note>与</en-note>中间部分抽出 把<en-note>与</en-note>中间部分抽出
parsedRes = [{FileId: ""}] parsedRes = {hash1: file, hash2: file}
*/ */
parseEvernoteContent: function(xml, parsedRes) { parseEvernoteContent: function(xml, parsedRes) {
var me = this; var me = this;
@@ -39,19 +39,29 @@ var Import = {
return ''; return '';
} }
var reg = new RegExp("<en\-media(.*?)\/*>", "g"); // <en-media /> <en-media></en-media> var reg = new RegExp("<en\-media(.*?)\/*>", "g"); // <en-media /> <en-media></en-media>
var i = 0;
// console.log(content); // console.log(content);
while(ret = reg.exec(content)) { while(ret = reg.exec(content)) {
// ret[1] == type="text/html" style="cursor:pointer;" height="43" hash="bc322a11075e40f3a5b2dce3f2fffcdc" // ret[1] == type="text/html" style="cursor:pointer;" height="43" hash="bc322a11075e40f3a5b2dce3f2fffcdc"
try { try {
var res = parsedRes[i]; var attrs = ret[1];
i++;
// 得到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']; var fileId = res['FileId'];
if(!fileId) { if(!fileId) {
continue; continue;
} }
if(res.IsImage) { if(res.IsImage) {
var replace = '<img src="' + Evt.getImageLocalUrl(fileId) + '" ' + ret[1] + '>'; var replace = '<img src="' + Evt.getImageLocalUrl(fileId) + '" ' + attrs + '>';
} else { } else {
var replace = '<a href="' + Evt.getAttachLocalUrl(fileId) + '">' + res['Title'] + '</a>' var replace = '<a href="' + Evt.getAttachLocalUrl(fileId) + '">' + res['Title'] + '</a>'
} }
@@ -64,7 +74,6 @@ var Import = {
// 如果是<en-media></en-media>, </en-media>匹配不到 // 如果是<en-media></en-media>, </en-media>匹配不到
content = content.replace(/<\/en-media>/g, ''); content = content.replace(/<\/en-media>/g, '');
return content; return content;
}, },
@@ -87,7 +96,7 @@ var Import = {
// 文件保存之 // 文件保存之
var resources = note['resource'] || []; var resources = note['resource'] || [];
var parsedRes = []; var parsedRes = {};
var attachs = []; var attachs = [];
// console.log("-----------") // console.log("-----------")
// console.log(note); // console.log(note);
@@ -133,7 +142,8 @@ var Import = {
File.writeBase64(base64Str, isImage, type, filename, function(file) { File.writeBase64(base64Str, isImage, type, filename, function(file) {
if(file) { if(file) {
parsedRes.push(file); parsedRes[file.hash] = file;
// parsedRes.push(file);
if(!isImage) { if(!isImage) {
attachs.push(file); attachs.push(file);
} }
@@ -147,13 +157,14 @@ var Import = {
// 把content的替换之 // 把content的替换之
// console.log('ok, writeBase64 ok'); // console.log('ok, writeBase64 ok');
try { try {
console.log('parsedRes');
console.log(parsedRes); console.log(parsedRes);
jsonNote.Content = me.parseEvernoteContent(jsonNote.Content, parsedRes); jsonNote.Content = me.parseEvernoteContent(jsonNote.Content, parsedRes);
jsonNote.Attachs = attachs; jsonNote.Attachs = attachs;
} catch(e) { } catch(e) {
console.log(e); console.log(e);
} }
console.log(jsonNote); // console.log(jsonNote);
return callback && callback(jsonNote); return callback && callback(jsonNote);
} }
); );