mirror of
https://github.com/leanote/desktop-app.git
synced 2025-10-15 15:41:19 +00:00
leanote protocol移到main进程, 所有图片的操作通过db_client进行操作
This commit is contained in:
79
main.js
79
main.js
@@ -1,5 +1,6 @@
|
|||||||
var app = require('app'); // Module to control application life.
|
var app = require('app'); // Module to control application life.
|
||||||
var BrowserWindow = require('browser-window'); // Module to create native browser window.
|
var BrowserWindow = require('browser-window'); // Module to create native browser window.
|
||||||
|
var ipc = require('ipc');
|
||||||
|
|
||||||
// Report crashes to our server.
|
// Report crashes to our server.
|
||||||
require('crash-reporter').start();
|
require('crash-reporter').start();
|
||||||
@@ -39,47 +40,48 @@ app.on('activate-with-no-open-windows', function() {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// DB
|
||||||
|
var DB = {
|
||||||
|
init: function () {
|
||||||
|
var me = this;
|
||||||
|
var db = require('db_main');
|
||||||
|
|
||||||
|
// 前端发来消息
|
||||||
|
// m = {token: token, method: 'insert, findOne', dbname: 'notes', params: {username: "life"}};
|
||||||
|
ipc.on('db-exec', function(event, m) {
|
||||||
|
// me._token2Sender[m.token] = event.sender;
|
||||||
|
db.exec(m, function (ret) {
|
||||||
|
// console.log('main called ret:');
|
||||||
|
// console.log(ret);
|
||||||
|
event.sender.send('db-exec-ret', ret);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 前端发消息过来说可以初始化了
|
||||||
|
* @param {<Event>} event
|
||||||
|
* @param {Object} params {
|
||||||
|
curUser: <User> 是当前用户
|
||||||
|
dbPath: string 是用户的dbPath
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
ipc.on('db-init', function (event, params) {
|
||||||
|
db.init(params.curUser, params.dbPath);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// This method will be called when Electron has done everything
|
// This method will be called when Electron has done everything
|
||||||
// initialization and ready for creating browser windows.
|
// initialization and ready for creating browser windows.
|
||||||
app.on('ready', openIt);
|
app.on('ready', openIt);
|
||||||
|
|
||||||
function killPort(callback) {
|
|
||||||
var protocol = require('protocol');
|
|
||||||
if (protocol.registerFileProtocol) {
|
|
||||||
callback();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var child_process = require('child_process');
|
|
||||||
var port = '8912';
|
|
||||||
if (process.platform.toLowerCase().indexOf('win') === 0) {
|
|
||||||
// & EXIT 表示只循环一次
|
|
||||||
// Leanote会有两个pid绑定端口, 另一个和electron相关, kill掉也会把自己kill掉
|
|
||||||
var sh1 = 'FOR /F "tokens=4 delims= " %P IN (\'netstat -a -n -o ^| findstr :' + port + '\') DO (TaskKill.exe /F /PID %P) & Exit';
|
|
||||||
var sh2 = 'FOR /F "tokens=5 delims= " %P IN (\'netstat -a -n -o ^| findstr :' + port + '\') DO (TaskKill.exe /F /PID %P) & Exit';
|
|
||||||
child_process.exec(sh1, function () {
|
|
||||||
child_process.exec(sh2, callback);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
var sh = 'kill -9 $(lsof -i:' + port + ' -t)';
|
|
||||||
child_process.exec(sh, callback);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function openIt() {
|
function openIt() {
|
||||||
killPort(_openIt);
|
// 数据库
|
||||||
}
|
DB.init();
|
||||||
|
|
||||||
function _openIt() {
|
// 协议
|
||||||
// console.log(arguments);
|
var leanoteProtocol = require('leanote_protocol');
|
||||||
// app.getPath('appData');
|
leanoteProtocol.init();
|
||||||
|
|
||||||
// var Evt = require('evt');
|
|
||||||
// var basePath = '/Users/life/Library/Application Support/Leanote'; // require('nw.gui').App.dataPath;
|
|
||||||
// Evt.setDataBasePath(basePath);
|
|
||||||
|
|
||||||
// leanote protocol
|
|
||||||
// require('leanote_protocol');
|
|
||||||
|
|
||||||
// Create the browser window.
|
// Create the browser window.
|
||||||
mainWindow = new BrowserWindow({
|
mainWindow = new BrowserWindow({
|
||||||
@@ -93,11 +95,6 @@ function _openIt() {
|
|||||||
// and load the index.html of the app.
|
// and load the index.html of the app.
|
||||||
mainWindow.loadUrl('file://' + __dirname + '/note.html');
|
mainWindow.loadUrl('file://' + __dirname + '/note.html');
|
||||||
|
|
||||||
// 不能放在这里, 刚开始有图片, 之后添加的图片不能显示 ??
|
|
||||||
// // 启动服务器, 图片
|
|
||||||
// var Server = require('server');
|
|
||||||
// Server.start();
|
|
||||||
|
|
||||||
// Emitted when the window is closed.
|
// Emitted when the window is closed.
|
||||||
mainWindow.on('closed', function() {
|
mainWindow.on('closed', function() {
|
||||||
// Dereference the window object, usually you would store windows
|
// Dereference the window object, usually you would store windows
|
||||||
@@ -106,7 +103,6 @@ function _openIt() {
|
|||||||
mainWindow = null;
|
mainWindow = null;
|
||||||
});
|
});
|
||||||
|
|
||||||
var ipc = require('ipc');
|
|
||||||
mainWindow.on('focus', function() {
|
mainWindow.on('focus', function() {
|
||||||
// ipc.send('focusWindow'); mainProcess没有该方法
|
// ipc.send('focusWindow'); mainProcess没有该方法
|
||||||
if(mainWindow && mainWindow.webContents)
|
if(mainWindow && mainWindow.webContents)
|
||||||
@@ -123,6 +119,7 @@ function _openIt() {
|
|||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
mainWindow.webContents.send('closeWindow');
|
mainWindow.webContents.send('closeWindow');
|
||||||
});
|
});
|
||||||
|
|
||||||
// 前端发来可以关闭了
|
// 前端发来可以关闭了
|
||||||
ipc.on('quit-app', function(event, arg) {
|
ipc.on('quit-app', function(event, arg) {
|
||||||
console.log('get quit-app request');
|
console.log('get quit-app request');
|
||||||
|
91
node_modules/api_main.js
generated
vendored
Normal file
91
node_modules/api_main.js
generated
vendored
Normal file
@@ -0,0 +1,91 @@
|
|||||||
|
var async = require('async');
|
||||||
|
var Common = require('common');
|
||||||
|
var needle = require('needle');
|
||||||
|
var fs = require('fs');
|
||||||
|
var Evt = require('evt_main');
|
||||||
|
|
||||||
|
function log(o) {
|
||||||
|
// console.log(o);
|
||||||
|
}
|
||||||
|
|
||||||
|
// timeout 0无限等待, 60,000 1分钟
|
||||||
|
needle.defaults({
|
||||||
|
timeout: 60000
|
||||||
|
});
|
||||||
|
|
||||||
|
// 远程数据服务
|
||||||
|
var Api = {
|
||||||
|
// 检查错误
|
||||||
|
checkError: function(error, resp) {
|
||||||
|
var me = this;
|
||||||
|
},
|
||||||
|
|
||||||
|
getUrl: function(url, param) {
|
||||||
|
var url = Evt.leanoteUrl + '/api/' + url;
|
||||||
|
var token = Evt.getToken();
|
||||||
|
param = param || {};
|
||||||
|
param.token = token;
|
||||||
|
if(param) {
|
||||||
|
var paramStr = '';
|
||||||
|
for(var i in param) {
|
||||||
|
paramStr += i + '=' + param[i] + '&';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(url.indexOf('?') >= 0) {
|
||||||
|
url = url + '&' + paramStr;
|
||||||
|
}
|
||||||
|
url = url + '?' + paramStr;
|
||||||
|
return url;
|
||||||
|
},
|
||||||
|
|
||||||
|
getImage: function(fileId, callback) {
|
||||||
|
var me = this;
|
||||||
|
var url = me.getUrl('file/getImage', {fileId: fileId});
|
||||||
|
needle.get(url, function(err, resp) {
|
||||||
|
me.checkError(err, resp);
|
||||||
|
if(err) {
|
||||||
|
return callback && callback(false);
|
||||||
|
}
|
||||||
|
// log(resp.body);
|
||||||
|
/*
|
||||||
|
{ 'accept-ranges': 'bytes',
|
||||||
|
'content-disposition': 'inline; filename="logo.png"',
|
||||||
|
'content-length': '8583',
|
||||||
|
'content-type': 'image/png',
|
||||||
|
date: 'Mon, 19 Jan 2015 15:01:47 GMT',
|
||||||
|
*/
|
||||||
|
// log(resp.headers);
|
||||||
|
if(err) {
|
||||||
|
callback(false);
|
||||||
|
} else {
|
||||||
|
var typeStr = resp.headers['content-type'];
|
||||||
|
var type = 'png';
|
||||||
|
if(typeStr) {
|
||||||
|
var typeArr = typeStr.split('/');
|
||||||
|
if(typeStr.length > 1) {
|
||||||
|
type = typeArr[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var filename = Common.uuid() + '.' + type;
|
||||||
|
var imagePath = User.getCurUserImagesPath();
|
||||||
|
var imagePathAll = imagePath + '/' + filename;
|
||||||
|
log(imagePathAll);
|
||||||
|
fs.writeFile(imagePathAll, resp.body, function(err) {
|
||||||
|
if(err) {
|
||||||
|
log(err);
|
||||||
|
log('local save image failed 本地保存失败');
|
||||||
|
callback(false);
|
||||||
|
} else {
|
||||||
|
callback(imagePathAll, filename);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
};
|
||||||
|
module.exports = Api;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
12
node_modules/db.js
generated
vendored
12
node_modules/db.js
generated
vendored
@@ -1,6 +1,7 @@
|
|||||||
var Datastore = require('nedb');
|
var Datastore = require('nedb');
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
var Evt = require('evt');
|
var Evt = require('evt');
|
||||||
|
var dbClient = require('db_client');
|
||||||
|
|
||||||
// 数据库初始化
|
// 数据库初始化
|
||||||
// var dbPath = require('nw.gui').App.dataPath + '/nedb';
|
// var dbPath = require('nw.gui').App.dataPath + '/nedb';
|
||||||
@@ -26,10 +27,17 @@ var db = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
// 为特定用户初始化自己的表
|
// 为特定用户初始化自己的表
|
||||||
initDBForUser: function (userId) {
|
initDBForUser: function (userId, curUser) {
|
||||||
var me = this;
|
var me = this;
|
||||||
var dbNames = ['notebooks', 'notes', 'tags', 'images', 'attachs', 'noteHistories'];
|
var dbNames = ['notebooks', 'notes', 'tags', /*'images',*/ 'attachs', 'noteHistories'];
|
||||||
this.initIt(me, dbNames, userId);
|
this.initIt(me, dbNames, userId);
|
||||||
|
|
||||||
|
// init dbClient -> main db
|
||||||
|
var baseDBPath = dbPath;
|
||||||
|
if (userId) {
|
||||||
|
baseDBPath += '/' + userId;
|
||||||
|
}
|
||||||
|
dbClient.init(curUser, baseDBPath);
|
||||||
},
|
},
|
||||||
|
|
||||||
// 过时
|
// 过时
|
||||||
|
37
node_modules/db_client.js
generated
vendored
Normal file
37
node_modules/db_client.js
generated
vendored
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
/**
|
||||||
|
* db_main的db client, 调用db client即会调用后端的db_main
|
||||||
|
*/
|
||||||
|
|
||||||
|
var DatastoreProxy = require('nedb_proxy');
|
||||||
|
var path = require('path');
|
||||||
|
var Evt = require('evt');
|
||||||
|
var ipc = require('ipc');
|
||||||
|
|
||||||
|
|
||||||
|
var dbPath = Evt.getBasePath() + '/nedb55';
|
||||||
|
if(dbPath.length < 6) {
|
||||||
|
var dbPath = '/Users/life/Library/Application Support/Leanote' + '/nedb2';
|
||||||
|
}
|
||||||
|
|
||||||
|
// console.log(dbPath);
|
||||||
|
// g, 表全局环境
|
||||||
|
var db = {};
|
||||||
|
var dbNames = ['images']; // 现在只有images表在后台控制
|
||||||
|
for(var i in dbNames) {
|
||||||
|
var name = dbNames[i];
|
||||||
|
(function (name) {
|
||||||
|
db[name] = new DatastoreProxy(name);
|
||||||
|
})(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 在db.js的initForUser时调用
|
||||||
|
// 初始化, 给后端发消息
|
||||||
|
db.init = function (curUser, dbPath) {
|
||||||
|
ipc.send('db-init', {
|
||||||
|
curUser: curUser,
|
||||||
|
dbPath: dbPath
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = db;
|
||||||
|
console.log('db inited');
|
86
node_modules/db_main.js
generated
vendored
Normal file
86
node_modules/db_main.js
generated
vendored
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
var Datastore = require('nedb');
|
||||||
|
var path = require('path');
|
||||||
|
var Evt = require('evt_main');
|
||||||
|
|
||||||
|
var db = {};
|
||||||
|
|
||||||
|
// dbPath是用户的dbPath
|
||||||
|
db.init = function (curUser, dbPath) {
|
||||||
|
var me = this;
|
||||||
|
var dbNames = [
|
||||||
|
'images',
|
||||||
|
/*
|
||||||
|
'users', 'notebooks', 'notes', 'tags', 'attachs', 'noteHistories', 'g'*/
|
||||||
|
];
|
||||||
|
for(var i in dbNames) {
|
||||||
|
var name = dbNames[i];
|
||||||
|
var baseDBPath = dbPath;
|
||||||
|
|
||||||
|
var dbFilepath = path.join(baseDBPath, name + '.db');
|
||||||
|
(function (name) {
|
||||||
|
var autoload = true;
|
||||||
|
db[name] = new Datastore(
|
||||||
|
{filename: dbFilepath, autoload: autoload, onload: function () {
|
||||||
|
console.log(name + ' is loaded [main]');
|
||||||
|
}});
|
||||||
|
})(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
db._inited = true;
|
||||||
|
|
||||||
|
// 保存起来
|
||||||
|
Evt.init(curUser, dbPath);
|
||||||
|
|
||||||
|
console.log('db inited [main]');
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行
|
||||||
|
* @param {[type]} m [description]
|
||||||
|
* @param {Function} callback [description]
|
||||||
|
* @return {[type]} [description]
|
||||||
|
*/
|
||||||
|
db.exec = function(m, callback) {
|
||||||
|
var me = this;
|
||||||
|
// console.log('------------------------');
|
||||||
|
console.log('main db called');
|
||||||
|
// console.log(m);
|
||||||
|
|
||||||
|
if (!this._inited) {
|
||||||
|
callback({token: m.token, err: new Error(), ret: false});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m.method == 'find') {
|
||||||
|
var params = m.params;
|
||||||
|
var query = params.query;
|
||||||
|
var sorter = params.sorter;
|
||||||
|
if (sorter) {
|
||||||
|
db[m.dbname].find(query).sort(sorter).exec(function (err, ret) {
|
||||||
|
// 返回结果
|
||||||
|
callback({token: m.token, err: err, ret: ret});
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
db.params = query;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (m.method == 'update') {
|
||||||
|
var params = m.params;
|
||||||
|
// console.log('update------------------')
|
||||||
|
// console.log(params.options ? '' : '????????????????????????????');
|
||||||
|
db[m.dbname].update(params.query, params.sets, params.options, function (err, ret) {
|
||||||
|
// 返回结果
|
||||||
|
callback({token: m.token, err: err, ret: ret});
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// insert
|
||||||
|
db[m.dbname][m.method](m.params, function (err, ret) {
|
||||||
|
// 返回结果
|
||||||
|
callback({token: m.token, err: err, ret: ret});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = db;
|
20
node_modules/evt.js
generated
vendored
20
node_modules/evt.js
generated
vendored
@@ -11,8 +11,6 @@ if(!fs.existsSync(dataBasePath)) {
|
|||||||
*/
|
*/
|
||||||
// dataBasePath = '';
|
// dataBasePath = '';
|
||||||
|
|
||||||
var protocol = require('remote').require('protocol');
|
|
||||||
|
|
||||||
var Evt = {
|
var Evt = {
|
||||||
defaultUrl: 'https://leanote.com',
|
defaultUrl: 'https://leanote.com',
|
||||||
|
|
||||||
@@ -43,42 +41,28 @@ var Evt = {
|
|||||||
// https://github.com/atom/electron/commit/7d97bb6fe0a6feef886d927ea894bcb2f3521577
|
// https://github.com/atom/electron/commit/7d97bb6fe0a6feef886d927ea894bcb2f3521577
|
||||||
// 老版本没有这个问题
|
// 老版本没有这个问题
|
||||||
canUseProtocol: function () {
|
canUseProtocol: function () {
|
||||||
|
return true;
|
||||||
// return false;
|
// return false;
|
||||||
return protocol.registerFileProtocol;
|
// return protocol.registerFileProtocol;
|
||||||
},
|
},
|
||||||
|
|
||||||
getImageLocalUrlPrefix: function () {
|
getImageLocalUrlPrefix: function () {
|
||||||
if (this.canUseProtocol()) {
|
|
||||||
return 'leanote://file/getImage';
|
return 'leanote://file/getImage';
|
||||||
}
|
|
||||||
return this.localUrl + '/api/file/getImage';
|
|
||||||
},
|
},
|
||||||
|
|
||||||
getAttachLocalUrlPrefix: function () {
|
getAttachLocalUrlPrefix: function () {
|
||||||
if (this.canUseProtocol()) {
|
|
||||||
return 'leanote://file/getAttach';
|
return 'leanote://file/getAttach';
|
||||||
}
|
|
||||||
return this.localUrl + '/api/file/getAttach';
|
|
||||||
},
|
},
|
||||||
|
|
||||||
getAllAttachsLocalUrlPrefix: function () {
|
getAllAttachsLocalUrlPrefix: function () {
|
||||||
if (this.canUseProtocol()) {
|
|
||||||
return 'leanote://file/getAllAttachs';
|
return 'leanote://file/getAllAttachs';
|
||||||
}
|
|
||||||
return this.localUrl + '/api/file/getAllAttachs';
|
|
||||||
},
|
},
|
||||||
|
|
||||||
getImageLocalUrl: function(fileId) {
|
getImageLocalUrl: function(fileId) {
|
||||||
if (this.canUseProtocol()) {
|
|
||||||
return 'leanote://file/getImage?fileId=' + fileId;
|
return 'leanote://file/getImage?fileId=' + fileId;
|
||||||
}
|
|
||||||
return this.localUrl + '/api/file/getImage?fileId=' + fileId;
|
|
||||||
},
|
},
|
||||||
getAttachLocalUrl: function(fileId) {
|
getAttachLocalUrl: function(fileId) {
|
||||||
if (this.canUseProtocol()) {
|
|
||||||
return 'leanote://file/getAttach?fileId=' + fileId;
|
return 'leanote://file/getAttach?fileId=' + fileId;
|
||||||
}
|
|
||||||
return this.localUrl + '/api/file/getAttach?fileId=' + fileId;
|
|
||||||
},
|
},
|
||||||
getAllAttachLocalUrl: function(noteId) {
|
getAllAttachLocalUrl: function(noteId) {
|
||||||
return this.localUrl + '/api/file/getAllAttachs?noteId=' + noteId;
|
return this.localUrl + '/api/file/getAllAttachs?noteId=' + noteId;
|
||||||
|
43
node_modules/evt_main.js
generated
vendored
Normal file
43
node_modules/evt_main.js
generated
vendored
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
var fs = require('fs');
|
||||||
|
|
||||||
|
// main 进程环境
|
||||||
|
/**
|
||||||
|
当前端登录后, db.init时, 调用db_client的init
|
||||||
|
*/
|
||||||
|
|
||||||
|
var Evt = {
|
||||||
|
defaultUrl: 'https://leanote.com',
|
||||||
|
leanoteUrl: 'https://leanote.com',
|
||||||
|
// leanoteUrl: 'http://localhost:9000',
|
||||||
|
|
||||||
|
setHost: function(host) {
|
||||||
|
if(!host) {
|
||||||
|
this.leanoteUrl = this.defaultUrl;
|
||||||
|
} else {
|
||||||
|
this.leanoteUrl = host;
|
||||||
|
}
|
||||||
|
// leanote服务强制https
|
||||||
|
if (this.leanoteUrl === 'http://leanote.com') {
|
||||||
|
this.leanoteUrl = 'https://leanote.com';
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
getHost: function() {
|
||||||
|
return this.leanoteUrl;
|
||||||
|
},
|
||||||
|
|
||||||
|
init: function (curUser, dbPath) {
|
||||||
|
this.curUser = curUser;
|
||||||
|
this.setHost(curUser.Host);
|
||||||
|
},
|
||||||
|
|
||||||
|
getCurUserId: function () {
|
||||||
|
return this.curUser.UserId;
|
||||||
|
},
|
||||||
|
|
||||||
|
getToken: function () {
|
||||||
|
return this.curUser.Token;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = Evt;
|
20
node_modules/file.js
generated
vendored
20
node_modules/file.js
generated
vendored
@@ -1,4 +1,5 @@
|
|||||||
var db = require('db');
|
var db = require('db');
|
||||||
|
var dbClient = require('db_client');
|
||||||
var fs = require('fs');
|
var fs = require('fs');
|
||||||
var crypto = require('crypto');
|
var crypto = require('crypto');
|
||||||
var needle = require('needle');
|
var needle = require('needle');
|
||||||
@@ -6,7 +7,7 @@ var path = require('path');
|
|||||||
var Evt = require('evt');
|
var Evt = require('evt');
|
||||||
var User = require('user');
|
var User = require('user');
|
||||||
var Common = require('common');
|
var Common = require('common');
|
||||||
var Images = db.images;
|
var Images = dbClient.images;
|
||||||
var Attachs = db.attachs;
|
var Attachs = db.attachs;
|
||||||
var Web = require('web');
|
var Web = require('web');
|
||||||
|
|
||||||
@@ -392,8 +393,9 @@ var File = {
|
|||||||
if(isForce) {
|
if(isForce) {
|
||||||
image.ServerFileId = fileId;
|
image.ServerFileId = fileId;
|
||||||
}
|
}
|
||||||
db.images.insert(image, function(err, doc) {
|
dbClient.images.insert(image, function(err, doc) {
|
||||||
log(err);
|
console.log(err);
|
||||||
|
console.log(doc);
|
||||||
if(err) {
|
if(err) {
|
||||||
callback && callback(false);
|
callback && callback(false);
|
||||||
} else {
|
} else {
|
||||||
@@ -407,7 +409,7 @@ var File = {
|
|||||||
addImageForce: function(fileId, path, callback) {
|
addImageForce: function(fileId, path, callback) {
|
||||||
var me = this;
|
var me = this;
|
||||||
// 先删除之, 可能是本地有记录, 但是文件没了
|
// 先删除之, 可能是本地有记录, 但是文件没了
|
||||||
db.images.remove({FileId: fileId}, function() {
|
dbClient.images.remove({FileId: fileId}, function() {
|
||||||
me._addImage(fileId, path, callback, true);
|
me._addImage(fileId, path, callback, true);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
@@ -417,8 +419,8 @@ var File = {
|
|||||||
// 因为图片的链接 有可能是本地添加的, 又有可能是远程的
|
// 因为图片的链接 有可能是本地添加的, 又有可能是远程的
|
||||||
// 如果是远程的, FileId == ServerFileId, 是一样的, 所以不要Or
|
// 如果是远程的, FileId == ServerFileId, 是一样的, 所以不要Or
|
||||||
getImageLocalPath: function(fileId, callback) {
|
getImageLocalPath: function(fileId, callback) {
|
||||||
// db.images.findOne({$or: {FileId: fileId}, {ServerFileId: fileId}}, function(err, doc) {
|
// dbClient.images.findOne({$or: {FileId: fileId}, {ServerFileId: fileId}}, function(err, doc) {
|
||||||
db.images.findOne({FileId: fileId}, function(err, doc) {
|
dbClient.images.findOne({FileId: fileId}, function(err, doc) {
|
||||||
if(!err && doc && doc.Path) { // FileLocalPath是相对于项目的路径
|
if(!err && doc && doc.Path) { // FileLocalPath是相对于项目的路径
|
||||||
callback(true, doc.Path);
|
callback(true, doc.Path);
|
||||||
} else {
|
} else {
|
||||||
@@ -428,7 +430,7 @@ var File = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
getImageInfo: function(fileId, callback) {
|
getImageInfo: function(fileId, callback) {
|
||||||
db.images.findOne({FileId: fileId}, function(err, doc) {
|
dbClient.images.findOne({FileId: fileId}, function(err, doc) {
|
||||||
if(!err && doc && doc.Path) {
|
if(!err && doc && doc.Path) {
|
||||||
callback(true, doc);
|
callback(true, doc);
|
||||||
} else {
|
} else {
|
||||||
@@ -450,7 +452,7 @@ var File = {
|
|||||||
// 得到fileIds所有的images, 为了发送到服务器上
|
// 得到fileIds所有的images, 为了发送到服务器上
|
||||||
getAllImages: function(fileIds, callback) {
|
getAllImages: function(fileIds, callback) {
|
||||||
var me = this;
|
var me = this;
|
||||||
db.images.find({$or:[{FileId: {$in: fileIds}}, {ServerFileId: {$in: fileIds}}]}, function(err, images) {
|
dbClient.images.find({$or:[{FileId: {$in: fileIds}}, {ServerFileId: {$in: fileIds}}]}, function(err, images) {
|
||||||
if(err || !images) {
|
if(err || !images) {
|
||||||
return callback(false);
|
return callback(false);
|
||||||
}
|
}
|
||||||
@@ -473,7 +475,7 @@ var File = {
|
|||||||
if(!file.FileId || !file.LocalFileId) {
|
if(!file.FileId || !file.LocalFileId) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
db.images.update({FileId: file.LocalFileId}, {$set: {ServerFileId: file.FileId, IsDirty: false}});
|
dbClient.images.update({FileId: file.LocalFileId}, {$set: {ServerFileId: file.FileId, IsDirty: false}});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
125
node_modules/file_main.js
generated
vendored
Normal file
125
node_modules/file_main.js
generated
vendored
Normal file
@@ -0,0 +1,125 @@
|
|||||||
|
var fs = require('fs');
|
||||||
|
var path = require('path');
|
||||||
|
var db = require('db_main');
|
||||||
|
|
||||||
|
function log(o) {
|
||||||
|
console.trace(o);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 文件服务 main 进程调用
|
||||||
|
|
||||||
|
var File = {
|
||||||
|
// 从服务器上把文件download下来后
|
||||||
|
// 这里的fileId是serverFileId, 本地的FileId也保存为该值
|
||||||
|
addImageForce: function(fileId, path, callback) {
|
||||||
|
var me = this;
|
||||||
|
// 先删除之, 可能是本地有记录, 但是文件没了
|
||||||
|
db.images.remove({FileId: fileId}, function() {
|
||||||
|
me._addImage(fileId, path, callback, true);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
// 获取图片本地路径
|
||||||
|
// 通过FileId可ServerFileId来查找
|
||||||
|
// 因为图片的链接 有可能是本地添加的, 又有可能是远程的
|
||||||
|
// 如果是远程的, FileId == ServerFileId, 是一样的, 所以不要Or
|
||||||
|
getImageLocalPath: function(fileId, callback) {
|
||||||
|
// db.images.findOne({$or: {FileId: fileId}, {ServerFileId: fileId}}, function(err, doc) {
|
||||||
|
db.images.findOne({FileId: fileId}, function(err, doc) {
|
||||||
|
if(!err && doc && doc.Path) { // FileLocalPath是相对于项目的路径
|
||||||
|
callback(true, doc.Path);
|
||||||
|
} else {
|
||||||
|
callback(false, false);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
getImageInfo: function(fileId, callback) {
|
||||||
|
db.images.findOne({FileId: fileId}, function(err, doc) {
|
||||||
|
if(!err && doc && doc.Path) {
|
||||||
|
callback(true, doc);
|
||||||
|
} else {
|
||||||
|
callback(false, false);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
// 笔记添加/修改后会有LocalFileId <=> FileId映射
|
||||||
|
// 这个只对image有用
|
||||||
|
updateImageForce: function(files) {
|
||||||
|
if(!files) {
|
||||||
|
// callback && callback(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for(var i in files) {
|
||||||
|
var file = files[i];
|
||||||
|
if(file.IsAttach) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if(!file.FileId || !file.LocalFileId) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
db.images.update({FileId: file.LocalFileId},
|
||||||
|
{$set: {ServerFileId: file.FileId, IsDirty: false}});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
// 处理用户图片
|
||||||
|
getImage: function(fileId, callback) {
|
||||||
|
var me = this;
|
||||||
|
if(!fileId) {
|
||||||
|
return callback(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
var Api = require('api');
|
||||||
|
|
||||||
|
// 访问api, 得到图片
|
||||||
|
function getImageFromApi() {
|
||||||
|
// console.log('fetch servers image ' + fileId);
|
||||||
|
Api.getImage(fileId, function(fileLocalPath, filename) {
|
||||||
|
if(fileLocalPath) {
|
||||||
|
// console.log('save image to local');
|
||||||
|
// 保存到本地数据库中
|
||||||
|
me.addImageForce(fileId, fileLocalPath, function(doc) {
|
||||||
|
if(doc) {
|
||||||
|
// console.log('save image to local success');
|
||||||
|
} else {
|
||||||
|
// console.log('save image to local error');
|
||||||
|
}
|
||||||
|
callback(fileLocalPath);
|
||||||
|
// return me.retImage(fileLocalPath, res);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
// 远程取不到图片, 是没有网络? 还是远程真的没有了
|
||||||
|
// TODO
|
||||||
|
// console.log("cann't get server's image" + fileId);
|
||||||
|
// callback(false);
|
||||||
|
// return me.e404(res);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// 先查看本地是否有该文件
|
||||||
|
// has表示本地数据库有记录
|
||||||
|
me.getImageLocalPath(fileId, function(has, fileLocalPath) {
|
||||||
|
// 本地有
|
||||||
|
// console.log('re img')
|
||||||
|
// console.log(fileLocalPath);
|
||||||
|
// console.log(fs.exists(fileLocalPath));
|
||||||
|
if(has && fileLocalPath) {
|
||||||
|
fs.exists(fileLocalPath, function(exists) {
|
||||||
|
if(exists) {
|
||||||
|
// console.log('本地存在');
|
||||||
|
callback(fileLocalPath);
|
||||||
|
// me.retImage(fileLocalPath, res);
|
||||||
|
} else {
|
||||||
|
getImageFromApi();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
getImageFromApi();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = File;
|
129
node_modules/leanote_protocol.js
generated
vendored
129
node_modules/leanote_protocol.js
generated
vendored
@@ -1,109 +1,42 @@
|
|||||||
// 为了显示图片
|
|
||||||
// leanote://api/file/getImage?fileId=xxx
|
|
||||||
//
|
|
||||||
//
|
|
||||||
// 没用!!! 因为protocal不支持异步
|
|
||||||
// https://github.com/atom/electron/issues/410
|
|
||||||
|
|
||||||
var protocol = require('protocol');
|
var protocol = require('protocol');
|
||||||
|
var File = require('file_main');
|
||||||
|
|
||||||
var Evt = require('evt');
|
var leanoteProtocol = {
|
||||||
var Common = require('common');
|
destroy: function (callback) {
|
||||||
var File = require('file');
|
protocol.unregisterProtocol('leanote', function () {
|
||||||
var Api = require('api');
|
callback();
|
||||||
var db = require('db');
|
});
|
||||||
|
|
||||||
var http = require('http');
|
|
||||||
var url = require('url');
|
|
||||||
var path = require('path');
|
|
||||||
var fs = require('fs');
|
|
||||||
|
|
||||||
var Server = {
|
|
||||||
mime: {
|
|
||||||
"css": "text/css",
|
|
||||||
"gif": "image/gif",
|
|
||||||
"html": "text/html",
|
|
||||||
"ico": "image/x-icon",
|
|
||||||
"jpeg": "image/jpeg",
|
|
||||||
"jpg": "image/jpeg",
|
|
||||||
"js": "text/javascript",
|
|
||||||
"json": "application/json",
|
|
||||||
"pdf": "application/pdf",
|
|
||||||
"png": "image/png",
|
|
||||||
"svg": "image/svg+xml",
|
|
||||||
"swf": "application/x-shockwave-flash",
|
|
||||||
"tiff": "image/tiff",
|
|
||||||
"txt": "text/plain",
|
|
||||||
"wav": "audio/x-wav",
|
|
||||||
"wma": "audio/x-ms-wma",
|
|
||||||
"wmv": "video/x-ms-wmv",
|
|
||||||
"xml": "text/xml"
|
|
||||||
},
|
},
|
||||||
router: function(request) {
|
init: function () {
|
||||||
var me = this;
|
// 先注销, 为了防止刷新
|
||||||
|
// this.destroy(funciton () {
|
||||||
|
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]) {
|
||||||
|
var fileId = ret[1];
|
||||||
|
// console.log(fileId);
|
||||||
|
|
||||||
var pathname = url.parse(request.url).pathname;
|
File.getImage(ret[1], function(fileLocalPath) {
|
||||||
while(pathname[0] == '/') {
|
if(fileLocalPath) {
|
||||||
pathname = pathname.substr(1);
|
callback({path: fileLocalPath});
|
||||||
}
|
|
||||||
|
|
||||||
if(pathname == 'api/file/getImage') {
|
|
||||||
return me.getImage(request, response);
|
|
||||||
} else {
|
} else {
|
||||||
response.end();
|
callback();
|
||||||
return false;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
e404: function(res) {
|
|
||||||
var me = this;
|
|
||||||
res.writeHead(404, {
|
|
||||||
'Content-Type': 'text/plain'
|
|
||||||
});
|
|
||||||
res.write("This request URL " + me._req.url + " was not found on this server.");
|
|
||||||
res.end();
|
|
||||||
},
|
|
||||||
|
|
||||||
// 返回图片
|
|
||||||
retImage: function(filePath, res) {
|
|
||||||
var me = this;
|
|
||||||
var ext = path.extname(filePath);
|
|
||||||
ext = ext ? ext.slice(1) : 'unknown';
|
|
||||||
filePath = filePath + '';
|
|
||||||
fs.readFile(filePath, "binary", function (err, file) {
|
|
||||||
if (err) {
|
|
||||||
res.writeHead(500, {
|
|
||||||
'Content-Type': 'text/plain'
|
|
||||||
});
|
|
||||||
res.end();
|
|
||||||
} else {
|
|
||||||
var contentType = me.mime[ext] || "text/plain";
|
|
||||||
res.writeHead(200, {'Content-Type': contentType});
|
|
||||||
res.write(file, "binary");
|
|
||||||
res.end();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
},
|
|
||||||
|
|
||||||
getImage: function(req, res) {
|
|
||||||
var me = this;
|
|
||||||
// fileId
|
|
||||||
var fileId = url.parse(req.url, true).query['fileId'];
|
|
||||||
if(!fileId) {
|
|
||||||
return me.e404(res);
|
|
||||||
}
|
}
|
||||||
File.getImage(fileId, function(fileLocalPath) {
|
// var url = request.url.substr(7);
|
||||||
if(path) {
|
// callback({path: '/Users/life/Desktop/newicon/blog@2x.png'});
|
||||||
return me.retImage(fileLocalPath, res);
|
}, function (error) {
|
||||||
} else {
|
if (error) {
|
||||||
return me.e404(res);
|
console.error('Failed to register protocol')
|
||||||
|
console.error(error);
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
protocol.registerProtocol('leanote', function(request) {
|
|
||||||
var url = request.url.substr(7);
|
|
||||||
return new protocol.RequestFileJob(path.normalize(__dirname + '/' + url));
|
|
||||||
});
|
});
|
||||||
|
// });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = leanoteProtocol;
|
||||||
|
90
node_modules/nedb_proxy.js
generated
vendored
Normal file
90
node_modules/nedb_proxy.js
generated
vendored
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
var ipc = require('ipc');
|
||||||
|
|
||||||
|
function Find(dbProxy, dbname, query) {
|
||||||
|
this.query = query;
|
||||||
|
this.dbProxy = dbProxy;
|
||||||
|
this.dbname = dbname;
|
||||||
|
}
|
||||||
|
Find.prototype.sort = function (sorter) {
|
||||||
|
this.sorter = sorter;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
Find.prototype.exec = function (callback) {
|
||||||
|
var params = {
|
||||||
|
query: this.query,
|
||||||
|
sorter: this.sorter
|
||||||
|
};
|
||||||
|
this.dbProxy.send(params, callback, 'find');
|
||||||
|
};
|
||||||
|
|
||||||
|
//==========
|
||||||
|
|
||||||
|
function DBProxy(dbname) {
|
||||||
|
this.dbname = dbname;
|
||||||
|
};
|
||||||
|
|
||||||
|
var token = 1;
|
||||||
|
var token2Callback = {};
|
||||||
|
|
||||||
|
DBProxy.prototype.send = function(params, callback, method) {
|
||||||
|
token++;
|
||||||
|
var m = {
|
||||||
|
token: token,
|
||||||
|
method: method,
|
||||||
|
dbname: this.dbname,
|
||||||
|
params: params
|
||||||
|
}
|
||||||
|
token2Callback[token] = callback;
|
||||||
|
ipc.send('db-exec', m);
|
||||||
|
};
|
||||||
|
|
||||||
|
// NB.find({UserId: userId, $or: [{LocalIsDelete : { $exists : false }}, {LocalIsDelete: false}] }, function(err, notebooks) {
|
||||||
|
// Notes.find(query).sort({'UpdatedTime': -1}).exec(function(err, notes) {
|
||||||
|
DBProxy.prototype.find = function (params, callback) {
|
||||||
|
if (callback) {
|
||||||
|
this.send({query: params}, callback, 'find');
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return new Find(this, this.dbname, params);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
DBProxy.prototype.findOne = function (params, callback) {
|
||||||
|
this.send(params, callback, 'findOne');
|
||||||
|
};
|
||||||
|
DBProxy.prototype.count = function (params, callback) {
|
||||||
|
this.send(params, callback, 'count');
|
||||||
|
};
|
||||||
|
DBProxy.prototype.insert = function (params, callback) {
|
||||||
|
this.send(params, callback, 'insert');
|
||||||
|
};
|
||||||
|
DBProxy.prototype.update = function (query, sets, options, callback) {
|
||||||
|
if (typeof options == 'function') {
|
||||||
|
callback = options;
|
||||||
|
options = {};
|
||||||
|
}
|
||||||
|
if (!options) {
|
||||||
|
options = {};
|
||||||
|
}
|
||||||
|
var params = {
|
||||||
|
query: query,
|
||||||
|
sets: sets,
|
||||||
|
options: options
|
||||||
|
};
|
||||||
|
this.send(params, callback, 'update');
|
||||||
|
};
|
||||||
|
DBProxy.prototype.remove = function (params, callback) {
|
||||||
|
this.send(params, callback, 'remove');
|
||||||
|
};
|
||||||
|
|
||||||
|
// m = {token: , err : , ret: }
|
||||||
|
ipc.on('db-exec-ret', function(m) {
|
||||||
|
var token = m.token;
|
||||||
|
var callback = token2Callback[token];
|
||||||
|
// console.log('clent 接收到消息');
|
||||||
|
// console.log(m);
|
||||||
|
// console.log('--------------');
|
||||||
|
callback && callback(m.err, m.ret);
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = DBProxy;
|
1
node_modules/note.js
generated
vendored
1
node_modules/note.js
generated
vendored
@@ -7,7 +7,6 @@ var User = require('user');
|
|||||||
var Notebook = require('notebook');
|
var Notebook = require('notebook');
|
||||||
// var Tag = require('tag');
|
// var Tag = require('tag');
|
||||||
// var Api = require('api');
|
// var Api = require('api');
|
||||||
var Server = require('server');
|
|
||||||
var Common = require('common');
|
var Common = require('common');
|
||||||
var Web = require('web');
|
var Web = require('web');
|
||||||
|
|
||||||
|
240
node_modules/server.js
generated
vendored
240
node_modules/server.js
generated
vendored
@@ -1,240 +0,0 @@
|
|||||||
var Evt = require('evt');
|
|
||||||
var File = null;
|
|
||||||
|
|
||||||
var http = require('http');
|
|
||||||
var url = require('url');
|
|
||||||
var path = require('path');
|
|
||||||
var fs = require('fs');
|
|
||||||
var Common = require('common');
|
|
||||||
|
|
||||||
var protocol = require('remote').require('protocol');
|
|
||||||
|
|
||||||
// http server, 处理笔记图片
|
|
||||||
var Server = {
|
|
||||||
// port: 8008,
|
|
||||||
// localUrl: 'http://127.0.0.1:8008',
|
|
||||||
_started: false,
|
|
||||||
_req: null,
|
|
||||||
mime: {
|
|
||||||
"css": "text/css",
|
|
||||||
"gif": "image/gif",
|
|
||||||
"html": "text/html",
|
|
||||||
"ico": "image/x-icon",
|
|
||||||
"jpeg": "image/jpeg",
|
|
||||||
"jpg": "image/jpeg",
|
|
||||||
"js": "text/javascript",
|
|
||||||
"json": "application/json",
|
|
||||||
"pdf": "application/pdf",
|
|
||||||
"png": "image/png",
|
|
||||||
"svg": "image/svg+xml",
|
|
||||||
"swf": "application/x-shockwave-flash",
|
|
||||||
"tiff": "image/tiff",
|
|
||||||
"txt": "text/plain",
|
|
||||||
"wav": "audio/x-wav",
|
|
||||||
"wma": "audio/x-ms-wma",
|
|
||||||
"wmv": "video/x-ms-wmv",
|
|
||||||
"xml": "text/xml"
|
|
||||||
},
|
|
||||||
server: null,
|
|
||||||
start: function() {
|
|
||||||
var me = this;
|
|
||||||
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) {
|
|
||||||
var pathname = url.parse(request.url).pathname;
|
|
||||||
me._req = request;
|
|
||||||
if(!pathname) {
|
|
||||||
return me.e404(response);
|
|
||||||
}
|
|
||||||
while(pathname[0] == '/') {
|
|
||||||
pathname = pathname.substr(1);
|
|
||||||
}
|
|
||||||
if(pathname == 'api/file/getImage') {
|
|
||||||
return me.getImage(request, response);
|
|
||||||
} else {
|
|
||||||
response.end();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
var realPath = basePath + pathname;
|
|
||||||
var ext = path.extname(realPath);
|
|
||||||
ext = ext ? ext.slice(1) : 'unknown';
|
|
||||||
|
|
||||||
fs.exists(realPath, function (exists) {
|
|
||||||
if (!exists) {
|
|
||||||
response.writeHead(404, {
|
|
||||||
'Content-Type': 'text/plain'
|
|
||||||
});
|
|
||||||
response.write("This request URL " + pathname + " was not found on this server.");
|
|
||||||
response.end();
|
|
||||||
} else {
|
|
||||||
fs.readFile(realPath, "binary", function (err, file) {
|
|
||||||
if (err) {
|
|
||||||
response.writeHead(500, {
|
|
||||||
'Content-Type': 'text/plain'
|
|
||||||
});
|
|
||||||
|
|
||||||
response.end(err);
|
|
||||||
} else {
|
|
||||||
var contentType = me.mime[ext] || "text/plain";
|
|
||||||
response.writeHead(200, {'Content-Type': contentType});
|
|
||||||
response.write(file, "binary");
|
|
||||||
response.end();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
// kill 端口占用的pid
|
|
||||||
var child_process = require('child_process');
|
|
||||||
function killPort(callback) {
|
|
||||||
if (Common.isWin()) {
|
|
||||||
// & EXIT 表示只循环一次
|
|
||||||
// Leanote会有两个pid绑定端口, 另一个和electron相关, kill掉也会把自己kill掉
|
|
||||||
var sh1 = 'FOR /F "tokens=4 delims= " %P IN (\'netstat -a -n -o ^| findstr :' + Evt.port + '\') DO (TaskKill.exe /F /PID %P) & Exit';
|
|
||||||
var sh2 = 'FOR /F "tokens=5 delims= " %P IN (\'netstat -a -n -o ^| findstr :' + Evt.port + '\') DO (TaskKill.exe /F /PID %P) & Exit';
|
|
||||||
child_process.exec(sh1, function () {
|
|
||||||
child_process.exec(sh2, callback);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
var sh = 'kill -9 $(lsof -i:' + Evt.port + ' -t)';
|
|
||||||
child_process.exec(sh, callback);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// killPort(function (error, stdout, stderr) {
|
|
||||||
// if (error !== null) {
|
|
||||||
// console.log('kill port error: ' + error);
|
|
||||||
// }
|
|
||||||
|
|
||||||
server.listen(Evt.port);
|
|
||||||
console.log("Server runing at port: " + Evt.port + ".");
|
|
||||||
|
|
||||||
me.localUrl = Evt.localUrl;
|
|
||||||
me._started = true;
|
|
||||||
|
|
||||||
me.server = server;
|
|
||||||
// });
|
|
||||||
},
|
|
||||||
|
|
||||||
// 关闭服务
|
|
||||||
close: function(callback) {
|
|
||||||
this._started = false;
|
|
||||||
|
|
||||||
// 注销prototol, 如果频繁刷新, 会报错, calling a released render
|
|
||||||
if (Evt.canUseProtocol()) {
|
|
||||||
protocol.unregisterProtocol('leanote', function (ok) {
|
|
||||||
console.log('unregisterProtocol: ' + ok)
|
|
||||||
callback && callback();
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this.server.close(function(err) {
|
|
||||||
});
|
|
||||||
this._started = false;
|
|
||||||
callback && callback();
|
|
||||||
},
|
|
||||||
|
|
||||||
e404: function(res) {
|
|
||||||
var me = this;
|
|
||||||
res.writeHead(404, {
|
|
||||||
'Content-Type': 'text/plain'
|
|
||||||
});
|
|
||||||
res.write("This request URL " + me._req.url + " was not found on this server.");
|
|
||||||
res.end();
|
|
||||||
},
|
|
||||||
|
|
||||||
// 返回图片
|
|
||||||
retImage: function(filePath, res) {
|
|
||||||
var me = this;
|
|
||||||
var ext = path.extname(filePath);
|
|
||||||
ext = ext ? ext.slice(1) : 'unknown';
|
|
||||||
filePath = filePath + '';
|
|
||||||
fs.readFile(filePath, "binary", function (err, file) {
|
|
||||||
if (err) {
|
|
||||||
res.writeHead(500, {
|
|
||||||
'Content-Type': 'text/plain'
|
|
||||||
});
|
|
||||||
res.end();
|
|
||||||
} else {
|
|
||||||
var contentType = me.mime[ext] || "text/plain";
|
|
||||||
res.writeHead(200, {'Content-Type': contentType});
|
|
||||||
res.write(file, "binary");
|
|
||||||
res.end();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
getImage: function(req, res) {
|
|
||||||
var me = this;
|
|
||||||
// fileId
|
|
||||||
var fileId = url.parse(req.url, true).query['fileId'];
|
|
||||||
if(!fileId) {
|
|
||||||
return me.e404(res);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!File) {
|
|
||||||
File = require('file');
|
|
||||||
}
|
|
||||||
|
|
||||||
File.getImage(fileId, function(fileLocalPath) {
|
|
||||||
if(path) {
|
|
||||||
return me.retImage(fileLocalPath, res);
|
|
||||||
} else {
|
|
||||||
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.error(error);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
// });
|
|
||||||
|
|
||||||
}
|
|
||||||
};
|
|
||||||
module.exports = Server;
|
|
4
node_modules/user.js
generated
vendored
4
node_modules/user.js
generated
vendored
@@ -122,7 +122,7 @@ User = {
|
|||||||
// 设为当前user
|
// 设为当前user
|
||||||
me.saveCurUser(doc, function () {
|
me.saveCurUser(doc, function () {
|
||||||
// 为该用户初始化数据库
|
// 为该用户初始化数据库
|
||||||
db.initDBForUser(user.UserId);
|
db.initDBForUser(user.UserId, user);
|
||||||
|
|
||||||
me.userId = user.UserId;
|
me.userId = user.UserId;
|
||||||
var Notebook = require('notebook');
|
var Notebook = require('notebook');
|
||||||
@@ -239,7 +239,7 @@ User = {
|
|||||||
me.hasDB = user.HasDB; // 是否有自己的DB
|
me.hasDB = user.HasDB; // 是否有自己的DB
|
||||||
|
|
||||||
// 为该用户初始化数据库
|
// 为该用户初始化数据库
|
||||||
db.initDBForUser(me.hasDB ? me.userId : '');
|
db.initDBForUser(me.hasDB ? me.userId : '', user);
|
||||||
|
|
||||||
Evt.setHost(me.host);
|
Evt.setHost(me.host);
|
||||||
|
|
||||||
|
@@ -22,11 +22,9 @@ $(function() {
|
|||||||
var isMacP = isMac();
|
var isMacP = isMac();
|
||||||
|
|
||||||
$('.tool-close, .tool-close-blur').click(function() {
|
$('.tool-close, .tool-close-blur').click(function() {
|
||||||
// mac下关闭才是隐藏
|
onClose(function() {
|
||||||
onCloseNotStopServerForMac(function() {
|
|
||||||
gui.win.hide();
|
gui.win.hide();
|
||||||
});
|
});
|
||||||
// gui.win.showInactive();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// 从login.html -> note.html过来就没有reopen事件了?
|
// 从login.html -> note.html过来就没有reopen事件了?
|
||||||
|
@@ -6,12 +6,6 @@ var basePath = app.getPath('appData') + '/leanote'; // /Users/life/Library/Appli
|
|||||||
Evt.setDataBasePath(basePath);
|
Evt.setDataBasePath(basePath);
|
||||||
var protocol = require('remote').require('protocol');
|
var protocol = require('remote').require('protocol');
|
||||||
|
|
||||||
if(!/login.html/.test(location.href)) {
|
|
||||||
// 启动服务器, 图片
|
|
||||||
var Server = require('server');
|
|
||||||
Server.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
// 数据库初始化
|
// 数据库初始化
|
||||||
var db = require('db');
|
var db = require('db');
|
||||||
// db.init();
|
// db.init();
|
||||||
@@ -35,7 +29,6 @@ var NoteService = Service.noteService;
|
|||||||
var NotebookService = Service.notebookService;
|
var NotebookService = Service.notebookService;
|
||||||
var TagService = Service.tagService;
|
var TagService = Service.tagService;
|
||||||
var WebService = require('web');
|
var WebService = require('web');
|
||||||
var ServerService = require('server');
|
|
||||||
var FileService = require('file');
|
var FileService = require('file');
|
||||||
var EvtService = require('evt');
|
var EvtService = require('evt');
|
||||||
var CommonService = require('common');
|
var CommonService = require('common');
|
||||||
|
@@ -1524,9 +1524,7 @@ function switchAccount() {
|
|||||||
|
|
||||||
// 当没有用户时, 切换之
|
// 当没有用户时, 切换之
|
||||||
function switchToLoginWhenNoUser() {
|
function switchToLoginWhenNoUser() {
|
||||||
Server.close(function () {
|
|
||||||
toLogin();
|
toLogin();
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 没有一处调用
|
// 没有一处调用
|
||||||
@@ -1763,8 +1761,6 @@ var Notify = {
|
|||||||
var onClose = function(afterFunc) {
|
var onClose = function(afterFunc) {
|
||||||
console.log('on close');
|
console.log('on close');
|
||||||
try {
|
try {
|
||||||
// 先把服务/协议关掉
|
|
||||||
Server.close(function () {
|
|
||||||
SyncService.stop();
|
SyncService.stop();
|
||||||
// 先保存之前改变的
|
// 先保存之前改变的
|
||||||
Note.curChangedSaveIt();
|
Note.curChangedSaveIt();
|
||||||
@@ -1772,34 +1768,6 @@ var onClose = function(afterFunc) {
|
|||||||
State.saveCurState(function() {
|
State.saveCurState(function() {
|
||||||
afterFunc && afterFunc();
|
afterFunc && afterFunc();
|
||||||
});
|
});
|
||||||
});
|
|
||||||
} catch(e) {
|
|
||||||
console.error(e);
|
|
||||||
afterFunc && afterFunc();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 如果是mac, 当关闭窗口时不要stop server
|
|
||||||
var onCloseNotStopServerForMac = function(afterFunc) {
|
|
||||||
function o () {
|
|
||||||
SyncService.stop();
|
|
||||||
// 先保存之前改变的
|
|
||||||
Note.curChangedSaveIt();
|
|
||||||
// 保存状态
|
|
||||||
State.saveCurState(function() {
|
|
||||||
afterFunc && afterFunc();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
if (isMac()) {
|
|
||||||
o();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// 先把服务/协议关掉
|
|
||||||
Server.close(function () {
|
|
||||||
o();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
afterFunc && afterFunc();
|
afterFunc && afterFunc();
|
||||||
|
@@ -103,7 +103,7 @@ define(function() {
|
|||||||
var me = this;
|
var me = this;
|
||||||
// http://127.0.0.1:8912/api/file/getImage?fileId=5581029f6289dc3301000000
|
// http://127.0.0.1:8912/api/file/getImage?fileId=5581029f6289dc3301000000
|
||||||
// 找到图片
|
// 找到图片
|
||||||
var reg = new RegExp(Api.evtService.localUrl + '/api/file/getImage\\?fileId=([0-9a-zA-Z]{24})', 'g');
|
var reg = new RegExp('leanote://file/getImage\\?fileId=([0-9a-zA-Z]{24})', 'g');
|
||||||
// console.log(Api.evtService.localUrl + '/api/file/getImage\\?fileId=([0-9a-zA-Z]{24})');
|
// console.log(Api.evtService.localUrl + '/api/file/getImage\\?fileId=([0-9a-zA-Z]{24})');
|
||||||
var matches = content.match(reg);
|
var matches = content.match(reg);
|
||||||
// content = content.replace(reg, Evt.leanoteUrl + '/api/file/getImage');
|
// content = content.replace(reg, Evt.leanoteUrl + '/api/file/getImage');
|
||||||
|
@@ -4,7 +4,6 @@ var Notebook = require('notebook');
|
|||||||
var Note = require('note');
|
var Note = require('note');
|
||||||
var Api = require('api');
|
var Api = require('api');
|
||||||
var User = require('user');
|
var User = require('user');
|
||||||
var Server = require('server');
|
|
||||||
var Evt = require('evt');
|
var Evt = require('evt');
|
||||||
var Common = require('common');
|
var Common = require('common');
|
||||||
|
|
||||||
@@ -91,7 +90,6 @@ Note.getNoteByServerNoteId("54c6313799c37bdeec000008", function(ret){
|
|||||||
// Api.auth('c@a.com', 'abc123');
|
// Api.auth('c@a.com', 'abc123');
|
||||||
// var content = '<img src="http://localhost:9000/api/file/getImage?fileId=54c2083f99c37bea5f000001"> <img src="http://localhost:9000/api/file/getImage?fileId=54c2083f99c37bea5f000001">' + "\n" + '<img src="http://localhost:9000/api/file/getImage?fileId=54c2083f99c37bea5f000001">';
|
// var content = '<img src="http://localhost:9000/api/file/getImage?fileId=54c2083f99c37bea5f000001"> <img src="http://localhost:9000/api/file/getImage?fileId=54c2083f99c37bea5f000001">' + "\n" + '<img src="http://localhost:9000/api/file/getImage?fileId=54c2083f99c37bea5f000001">';
|
||||||
// var reg = new RegExp('<img *src="' + Api.leanoteUrl + '/api/file/getImage', 'g');
|
// var reg = new RegExp('<img *src="' + Api.leanoteUrl + '/api/file/getImage', 'g');
|
||||||
// content = content.replace(reg, '<img src="' + Server.localUrl + '/api/file/getImage');
|
|
||||||
// console.log(content);
|
// console.log(content);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
Reference in New Issue
Block a user