支持导出附件

This commit is contained in:
life
2015-10-26 18:50:04 +08:00
parent 54e9eb2a69
commit be47d2498a
3 changed files with 91 additions and 21 deletions

10
node_modules/file.js generated vendored
View File

@@ -424,6 +424,16 @@ var File = {
}); });
}, },
getAttachInfo: function(fileId, callback) {
db.attachs.findOne({FileId: fileId}, function(err, doc) {
if(!err && doc && doc.Path) {
callback(true, doc);
} else {
callback(false, false);
}
});
},
// 得到fileIds所有的images, 为了发送到服务器上 // 得到fileIds所有的images, 为了发送到服务器上
getAllImages: function(fileIds, callback) { getAllImages: function(fileIds, callback) {
var me = this; var me = this;

View File

@@ -1,8 +1,12 @@
/** /**
* 导出Evernote插件 * 导出Evernote插件
* @author life life@leanote.com * @author life life@leanote.com
* 选择目录, 将图片保存到文件夹中, 有个html文件(以笔记名命名) *
* 注意, fs.existsSync总返回false, readFileSync可用 * 注意/遗留问题:
*
* 1. 导出的文件有可能不能导入到evernote, 即使可以导入, 也有可能不能同步
* 原因: enml.dtd
* 2. 导出markdown问题, 加一个<pre>markdown content</pre>. 导出的markdown没有图片
*/ */
define(function() { define(function() {
var async = require('async'); var async = require('async');
@@ -102,6 +106,8 @@ define(function() {
while(filename.indexOf('--') != -1) { while(filename.indexOf('--') != -1) {
filename = this.replaceAll(filename, '--', '-'); filename = this.replaceAll(filename, '--', '-');
} }
// 最后一个-
filename = filename.replace(/\-$/, '');
return filename; return filename;
}, },
@@ -133,11 +139,10 @@ define(function() {
createdTime: me.getEvernoteTime(note.createdTime), createdTime: me.getEvernoteTime(note.createdTime),
updatedTime: me.getEvernoteTime(note.updatedTime), updatedTime: me.getEvernoteTime(note.updatedTime),
exportedTime: me.getEvernoteTime(), exportedTime: me.getEvernoteTime(),
appVersion: '1.0',
authorEmail: Api.userService.email || Api.userService.username, authorEmail: Api.userService.email || Api.userService.username,
platform: process.platform, platform: process.platform,
appVersion: appVersion.version, appVersion: appVersion.version,
isMarkdown: note.isMarkdown ? 'true' : 'false', isMarkdown: note.IsMarkdown ? 'true' : 'false',
tags: me.renderTags(note.Tags) tags: me.renderTags(note.Tags)
}; };
@@ -153,18 +158,25 @@ define(function() {
else { else {
info.content = ENML; info.content = ENML;
} }
if (note.IsMarkdown) {
info.content = '<pre>' + info.content + '</pre>';
}
callback(me.renderTpl(tpl, info, keys)); callback(me.renderTpl(tpl, info, keys));
}); });
}); });
}, },
renderTags: function (tags) { renderTags: function (tags) {
if (!tags || tags.length === 0){ if (!tags || tags.length === 0) {
return '' return ''
} }
var str = ''; var str = '';
for (var i = 0; i < tags.length; ++i) { for (var i = 0; i < tags.length; ++i) {
str += '<tag>' + tags[i] + '</tag>'; if (tags[i]) {
str += '<tag>' + tags[i] + '</tag>';
}
} }
return str; return str;
}, },
@@ -180,9 +192,8 @@ define(function() {
return me.renderTpl(tpl, fileInfo, keys); return me.renderTpl(tpl, fileInfo, keys);
}, },
fixResources: function (content, callback) { findAllImages: function (content) {
var me = this; var reg = new RegExp('<img[^>]*?src=["\']?' + Api.evtService.localUrl + '/api/file/getImage\\?fileId=([0-9a-zA-Z]{24})["\']?.*?>', 'g');
var reg = new RegExp('<img.*?src=["\']?' + Api.evtService.localUrl + '/api/file/getImage\\?fileId=([0-9a-zA-Z]{24})["\']?.*?>', 'g');
var matches = reg.exec(content); var matches = reg.exec(content);
// width="330" height="330", style="width:200px" // width="330" height="330", style="width:200px"
@@ -218,6 +229,49 @@ define(function() {
matches = reg.exec(content); matches = reg.exec(content);
} }
return allMatchs;
},
findAllAttachs: function (content) {
var reg = new RegExp('<a[^>]*?href=["\']?' + Api.evtService.localUrl + '/api/file/getAttach\\?fileId=([0-9a-zA-Z]{24})["\']?.*?>([^<]*)</a>', 'g');
var matches = reg.exec(content);
// 先找到所有的
var allMatchs = [];
while(matches) {
var all = matches[0];
var fileId = matches[1];
var title = matches[2];
allMatchs.push({
fileId: fileId,
title: title,
isAttach: true,
all: all
});
// 下一个
matches = reg.exec(content);
}
return allMatchs;
},
fixResources: function (content, callback) {
var me = this;
var allImages = me.findAllImages(content) || [];
var allAttachs = me.findAllAttachs(content) || [];
var allMatchs = allImages.concat(allAttachs);
console.log(allMatchs);
if (allMatchs.length == 0) {
callback(content, '');
return;
}
var resources = ''; var resources = '';
var fileIdFixed = {}; var fileIdFixed = {};
@@ -232,17 +286,22 @@ define(function() {
} }
var media = '<en-media'; var media = '<en-media';
if (eachMatch.width) { if (!eachMatch.isAttach) {
media += ' width="' + eachMatch.width + '"'; if (eachMatch.width) {
} media += ' width="' + eachMatch.width + '"';
if (eachMatch.height) { }
media += ' height="' + eachMatch.height + '"'; if (eachMatch.height) {
media += ' height="' + eachMatch.height + '"';
}
else {
media += ' style="height:auto"';
}
} }
else { else {
media += ' style="height:auto"'; media += ' height="43" style="cursor:pointer;"';
} }
media += 'type="' + fileInfo.type + '"'; media += ' type="' + fileInfo.type + '"';
media += ' hash="' + fileInfo.md5 + '"'; media += ' hash="' + fileInfo.md5 + '"';
media += ' />'; media += ' />';
@@ -261,14 +320,14 @@ define(function() {
return; return;
} }
Api.fileService.getImageInfo(fileId, function(err, doc) { var server = eachMatch.isAttach ? Api.fileService.getAttachInfo : Api.fileService.getImageInfo;
server.call(Api.fileService, fileId, function(err, doc) {
fileIdFixed[fileId] = true; fileIdFixed[fileId] = true;
if(doc) { if(doc) {
var imagePath = doc.Path; var base64AndMd5 = Api.fileService.getFileBase64AndMd5(doc.Path);
var base64AndMd5 = Api.fileService.getFileBase64AndMd5(imagePath);
if (base64AndMd5) { if (base64AndMd5) {
base64AndMd5.createdTime = doc.CreatedTime; base64AndMd5.createdTime = doc.CreatedTime;
base64AndMd5.name = doc.Name; base64AndMd5.name = eachMatch.title || doc.Name;
base64AndMd5.type = Api.fileService.getFileType(doc.Type); base64AndMd5.type = Api.fileService.getFileType(doc.Type);
fileInfos[fileId] = base64AndMd5; fileInfos[fileId] = base64AndMd5;
} }
@@ -499,5 +558,4 @@ define(function() {
}; };
return exportEvernote; return exportEvernote;
}); });

View File

@@ -87,6 +87,8 @@ define(function() {
while(filename.indexOf('--') != -1) { while(filename.indexOf('--') != -1) {
filename = this.replaceAll(filename, '--', '-'); filename = this.replaceAll(filename, '--', '-');
} }
// 最后一个-
filename = filename.replace(/\-$/, '');
return filename; return filename;
}, },