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

View File

@@ -231,6 +231,7 @@ function openIt() {
killPort(_openIt);
}
function _openIt() {
// console.log(arguments);
// app.getPath('appData');
@@ -294,3 +295,5 @@ function _openIt() {
// 作为调试
debug && setMenu();
}

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;

View File

@@ -24,5 +24,5 @@ var Config = {
}
],
"lang": "en-us",
"theme": ""
"theme": "black"
};

View File

@@ -422,6 +422,10 @@ Note.genAbstract = function(content, len) {
return d.innerHTML;
};
Note.fixImageSrc = function(src) {
return fixContentUrl(src);
};
Note.getImgSrc = function(content) {
if(!content) {
return "";
@@ -779,9 +783,9 @@ Note.renderChangedNote = function(changedNote) {
$thumb = $leftNoteNav.find(".item-thumb");
// 有可能之前没有图片
if($thumb.length > 0) {
$thumb.find("img").attr("src", changedNote.ImgSrc);
$thumb.find("img").attr("src", Note.fixImageSrc(changedNote.ImgSrc));
} else {
$leftNoteNav.append(tt('<div class="item-thumb" style=""><img src="?"></div>', changedNote.ImgSrc));
$leftNoteNav.append(tt('<div class="item-thumb" style=""><img src="?"></div>', Note.fixImageSrc(changedNote.ImgSrc)));
$leftNoteNav.addClass("item-image");
}
$leftNoteNav.find(".item-desc").removeAttr("style");
@@ -982,7 +986,7 @@ Note._getNoteHtmlObjct = function(note, isShared) {
var tmp;
if(note.ImgSrc) {
tmp = tt(Note.itemTpl, classes, this.newNoteSeq(), note.NoteId, note.ImgSrc, note.Title, Notebook.getNotebookTitle(note.NotebookId), goNowToDatetime(note.UpdatedTime), note.Desc);
tmp = tt(Note.itemTpl, classes, this.newNoteSeq(), note.NoteId, Note.fixImageSrc(note.ImgSrc), note.Title, Notebook.getNotebookTitle(note.NotebookId), goNowToDatetime(note.UpdatedTime), note.Desc);
} else {
tmp = tt(Note.itemTplNoImg, classes, this.newNoteSeq(), note.NoteId, note.Title, Notebook.getNotebookTitle(note.NotebookId), goNowToDatetime(note.UpdatedTime), note.Desc);
}
@@ -1028,7 +1032,7 @@ Note._renderNotes = function(notes, forNewNote, isShared, tang) { // 第几趟
var tmp;
if(note.ImgSrc) {
tmp = tt(Note.itemTpl, classes, i, note.NoteId, note.ImgSrc, note.Title, Notebook.getNotebookTitle(note.NotebookId), goNowToDatetime(note.UpdatedTime), note.Desc || '');
tmp = tt(Note.itemTpl, classes, i, note.NoteId, Note.fixImageSrc(note.ImgSrc), note.Title, Notebook.getNotebookTitle(note.NotebookId), goNowToDatetime(note.UpdatedTime), note.Desc || '');
} else {
tmp = tt(Note.itemTplNoImg, classes, i, note.NoteId, note.Title, Notebook.getNotebookTitle(note.NotebookId), goNowToDatetime(note.UpdatedTime), note.Desc || '');
}

View File

@@ -4,6 +4,7 @@ var Evt = require('evt');
var app = require('remote').require('app');
var basePath = app.getPath('appData') + '/leanote'; // /Users/life/Library/Application Support/Leanote'; // require('nw.gui').App.dataPath;
Evt.setDataBasePath(basePath);
var protocol = require('remote').require('protocol');
if(!/login.html/.test(location.href)) {
// 启动服务器, 图片

View File

@@ -344,6 +344,14 @@ function switchEditor(isMarkdown) {
}
}
// 将http://127.0.0.1:8912转为leanote://
function fixContentUrl(content) {
if (EvtService.canUseProtocol()) {
return content.replace(/http:\/\/127.0.0.1:8912\/api\//g, 'leanote://');
}
return content;
}
// editor 设置内容
// 可能是tinymce还没有渲染成功
var previewToken = "<div style='display: none'>FORTOKEN</div>"
@@ -357,6 +365,9 @@ function _setEditorContent(content, isMarkdown, preview, callback) {
if(!content) {
content = "";
}
content = fixContentUrl(content);
if(clearIntervalForSetContent) {
clearInterval(clearIntervalForSetContent);
}
@@ -1704,16 +1715,16 @@ var Notify = {
var onClose = function(afterFunc) {
try {
// 先把服务关掉
Server.close();
SyncService.stop();
// 先保存之前改变的
Note.curChangedSaveIt();
// 保存状态
State.saveCurState(function() {
afterFunc && afterFunc();
});
// 先把服务/协议关掉
Server.close(function () {
SyncService.stop();
// 先保存之前改变的
Note.curChangedSaveIt();
// 保存状态
State.saveCurState(function() {
afterFunc && afterFunc();
});
});
} catch(e) {
afterFunc && afterFunc();
}