通过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;
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);
});
}
/*

View File

@@ -20,7 +20,7 @@ var Import = {
</en-note>\n'
把<en-note>与</en-note>中间部分抽出
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("<en\-media(.*?)\/*>", "g"); // <en-media /> <en-media></en-media>
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 = '<img src="' + Evt.getImageLocalUrl(fileId) + '" ' + ret[1] + '>';
var replace = '<img src="' + Evt.getImageLocalUrl(fileId) + '" ' + attrs + '>';
} else {
var replace = '<a href="' + Evt.getAttachLocalUrl(fileId) + '">' + res['Title'] + '</a>'
}
@@ -64,7 +74,6 @@ var Import = {
// 如果是<en-media></en-media>, </en-media>匹配不到
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);
}
);