leanote协议完成

This commit is contained in:
life
2015-11-09 23:33:41 +08:00
parent 52ed3ea523
commit f20120ebdf
8 changed files with 154 additions and 25 deletions

38
node_modules/evt.js generated vendored
View File

@@ -11,6 +11,9 @@ if(!fs.existsSync(dataBasePath)) {
*/
// dataBasePath = '';
var protocol = require('remote').require('protocol');
var Evt = {
defaultUrl: 'https://leanote.com',
@@ -37,10 +40,45 @@ var Evt = {
localUrl: 'http://127.0.0.1:8912',
dataBasePath: '',
// 是否有这个方法, 就代表是否可以用(含callback)
// https://github.com/atom/electron/commit/7d97bb6fe0a6feef886d927ea894bcb2f3521577
// 老版本没有这个问题
canUseProtocol: function () {
// return false;
return protocol.registerFileProtocol;
},
getImageLocalUrlPrefix: function () {
if (this.canUseProtocol()) {
return 'leanote://file/getImage';
}
return this.localUrl + '/api/file/getImage';
},
getAttachLocalUrlPrefix: function () {
if (this.canUseProtocol()) {
return 'leanote://file/getAttach';
}
return this.localUrl + '/api/file/getAttach';
},
getAllAttachsLocalUrlPrefix: function () {
if (this.canUseProtocol()) {
return 'leanote://file/getAllAttachs';
}
return this.localUrl + '/api/file/getAllAttachs';
},
getImageLocalUrl: function(fileId) {
if (this.canUseProtocol()) {
return 'leanote://file/getImage?fileId=' + fileId;
}
return this.localUrl + '/api/file/getImage?fileId=' + fileId;
},
getAttachLocalUrl: function(fileId) {
if (this.canUseProtocol()) {
return 'leanote://file/getAttach?fileId=' + fileId;
}
return this.localUrl + '/api/file/getAttach?fileId=' + fileId;
},
getAllAttachLocalUrl: function(noteId) {

31
node_modules/note.js generated vendored
View File

@@ -484,17 +484,18 @@ var Note = {
// http://leanote.com/file/outputImage?fileId=54f9079f38f4115c0200001b
var reg0 = new RegExp(url + '/file/outputImage', 'g');
content = content.replace(reg0, Evt.localUrl + '/api/file/getImage');
content = content.replace(reg0, Evt.getImageLocalUrlPrefix());
var reg = new RegExp(url + '/api/file/getImage', 'g');
content = content.replace(reg, Evt.localUrl + '/api/file/getImage');
content = content.replace(reg, Evt.getImageLocalUrlPrefix());
var reg2 = new RegExp(url + '/api/file/getAttach', 'g');
content = content.replace(reg2, Evt.localUrl + '/api/file/getAttach');
content = content.replace(reg2, Evt.getAttachLocalUrlPrefix());
// 无用
// api/file/getAllAttachs?noteId=xxxxxxxxx, 这里的noteId是服务器上的noteId啊
var reg3 = new RegExp(url + '/api/file/getAllAttachs', 'g');
content = content.replace(reg3, Evt.localUrl + '/api/file/getAllAttachs');
content = content.replace(reg3, Evt.getAllAttachsLocalUrlPrefix());
return content;
},
@@ -507,13 +508,13 @@ var Note = {
// console.log(Evt.localUrl + '/api/file/getImage');
// console.log(content);
var reg = new RegExp(Evt.localUrl + '/api/file/getImage', 'g');
var reg = new RegExp(Evt.getImageLocalUrlPrefix(), 'g');
content = content.replace(reg, Evt.leanoteUrl + '/api/file/getImage');
var reg2 = new RegExp(Evt.localUrl + '/api/file/getAttach', 'g');
var reg2 = new RegExp(Evt.getAttachLocalUrlPrefix(), 'g');
content = content.replace(reg2, Evt.leanoteUrl + '/api/file/getAttach');
var reg3 = new RegExp(Evt.localUrl + '/api/file/getAllAttachs', 'g');
var reg3 = new RegExp(Evt.getAllAttachsLocalUrlPrefix(), 'g');
content = content.replace(reg3, Evt.leanoteUrl + '/api/file/getAllAttachs');
return content;
@@ -1338,6 +1339,7 @@ var Note = {
return callback && callback(false);
} else {
// 每一个笔记得到图片, 附件信息和数据
note.Content = me.fixContentUrl(note.Content);
async.eachSeries(notes, function(note, cb) {
me.getNoteFiles(note, function(files) {
note.Content = me.fixNoteContentForSend(note.Content);
@@ -1349,15 +1351,27 @@ var Note = {
});
});
}, function() {
console.log(notes);
callback(notes);
});
}
});
},
// 历史原因, 支持了protocol, 但url还是有127
fixContentUrl: function(content) {
if (!content) {
return content;
}
if (Evt.canUseProtocol()) {
return content.replace(/http:\/\/127.0.0.1:8912\/api\//g, 'leanote://');
}
},
// 得到笔记的文件
getNoteFiles: function(note, callback) {
var noteId = note.NoteId;
// 先处理内容URL
var content = note.Content;
// 1. 先得到附件
@@ -1373,7 +1387,8 @@ var Note = {
// http://localhost:8002/api/file/getImage?fileId=xxxxxx, 得到fileId, 查询数据库, 得到图片
// console.log(content);
// console.log(Evt.localUrl + '/api/file/getImage?fileId=([0-9a-zA-Z]{24})');
var reg = new RegExp(Evt.localUrl + "/api/file/getImage\\?fileId=([0-9a-zA-Z]{24})", 'g');
// var reg = new RegExp(Evt.localUrl + "/api/file/getImage\\?fileId=([0-9a-zA-Z]{24})", 'g');
var reg = new RegExp(Evt.getImageLocalUrlPrefix() + "\\?fileId=([0-9a-zA-Z]{24})", 'g');
var fileIds = [];
// var fileIdsMap = {}; // 防止多个
while((result = reg.exec(content)) != null) {

61
node_modules/server.js generated vendored
View File

@@ -7,6 +7,8 @@ var path = require('path');
var fs = require('fs');
var Common = require('common');
var protocol = require('remote').require('protocol');
// http server, 处理笔记图片
var Server = {
// port: 8008,
@@ -39,6 +41,13 @@ var Server = {
if(me._started) {
return;
}
if (Evt.canUseProtocol()) {
me.initProtocol();
me._started = true;
return;
}
// return;
var basePath = process.cwd();
var server = http.createServer(function (request, response) {
@@ -120,10 +129,19 @@ var Server = {
},
// 关闭服务
close: function() {
close: function(callback) {
this._started = false;
// 注销prototol, 如果频繁刷新, 会报错, calling a released render
if (Evt.canUseProtocol()) {
protocol.unregisterProtocol('leanote', function () {
callback && callback();
});
return;
}
this.server.close(function(err) {
callback && callback();
});
console.log('close');
this._started = false;
},
@@ -176,6 +194,45 @@ var Server = {
return me.e404(res);
}
})
},
//---------------------
// 新server
// latest 0.31 series
//---------------------
initProtocol: function () {
// 先注销, 为了防止刷新
// protocol.unregisterProtocol('leanote', function () {
protocol.registerFileProtocol('leanote', function(request, callback) {
// console.log(request.url);
var url = request.url;
var ret = /fileId=([a-zA-Z0-9]{24})/.exec(url);
if (ret && ret[1]) {
// console.log(ret);
if(!File) {
File = require('file');
}
File.getImage(ret[1], function(fileLocalPath) {
if(fileLocalPath) {
callback({path: fileLocalPath});
} else {
callback();
}
})
var fileId = ret[1];
}
// var url = request.url.substr(7);
// callback({path: '/Users/life/Desktop/newicon/blog@2x.png'});
}, function (error) {
if (error) {
console.error('Failed to register protocol')
console.log(error);
}
});
// });
}
};
module.exports = Server;