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;