From d07a3a00a32f0393eb7646d57d9974a20df14169 Mon Sep 17 00:00:00 2001 From: life Date: Fri, 1 May 2015 15:43:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9B=BE=E7=89=87server=20ok?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 插入图片 --- src/main.js | 4 + src/node_modules/leanote_protocol.js | 105 +++++++++++++++++++++++++++ src/node_modules/server.js | 58 +-------------- 3 files changed, 110 insertions(+), 57 deletions(-) create mode 100644 src/node_modules/leanote_protocol.js diff --git a/src/main.js b/src/main.js index 19d27145..ed7ecc4f 100644 --- a/src/main.js +++ b/src/main.js @@ -17,6 +17,10 @@ app.on('window-all-closed', function() { // This method will be called when Electron has done everything // initialization and ready for creating browser windows. app.on('ready', function() { + + // leanote protocol + // require('leanote_protocol'); + // Create the browser window. mainWindow = new BrowserWindow({width: 1000, height: 600, frame: true, transparent: false }); diff --git a/src/node_modules/leanote_protocol.js b/src/node_modules/leanote_protocol.js new file mode 100644 index 00000000..e9fa4bad --- /dev/null +++ b/src/node_modules/leanote_protocol.js @@ -0,0 +1,105 @@ +// 为了显示图片 +// leanote://api/file/getImage?fileId=xxx + +var protocol = require('protocol'); + +var Evt = require('evt'); +var Common = require('common'); +var File = require('file'); +var Api = require('api'); +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) { + var me = this; + + var pathname = url.parse(request.url).pathname; + while(pathname[0] == '/') { + pathname = pathname.substr(1); + } + + if(pathname == 'api/file/getImage') { + return me.getImage(request, response); + } else { + response.end(); + 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) { + if(path) { + return me.retImage(fileLocalPath, res); + } else { + return me.e404(res); + } + }) + } +}; + + +protocol.registerProtocol('leanote', function(request) { + var url = request.url.substr(7); + return new protocol.RequestFileJob(path.normalize(__dirname + '/' + url)); +}); \ No newline at end of file diff --git a/src/node_modules/server.js b/src/node_modules/server.js index 936c0715..46250844 100644 --- a/src/node_modules/server.js +++ b/src/node_modules/server.js @@ -44,7 +44,7 @@ var Server = { if(me._started) { return; } - return; + // return; var basePath = process.cwd(); var server = http.createServer(function (request, response) { var pathname = url.parse(request.url).pathname; @@ -141,62 +141,6 @@ var Server = { return me.e404(res); } }) - }, - - /* - // 处理用户图片 - getImage: function(req, res) { - var me = this; - // fileId - var fileId = url.parse(req.url, true).query['fileId']; - if(!fileId) { - return me.e404(res); - } - - // 访问api, 得到图片 - function getImageFromApi() { - log('从远程得到图片 ' + fileId); - Api.getImage(fileId, function(fileLocalPath, filename) { - if(fileLocalPath) { - log('图片保存到本地成功'); - // 保存到本地数据库中 - File.addImageForce(fileId, fileLocalPath, function(doc) { - if(doc) { - log('保存到本地数据库成功'); - } else { - log('保存到数据库失败'); - } - return me.retImage(fileLocalPath, res); - }); - } else { - // 远程取不到图片, 是没有网络? 还是远程真的没有了 - // TODO - log('取不远程的图片' + fileId); - return me.e404(res); - } - }); - } - // 先查看本地是否有该文件 - // has表示本地数据库有记录 - File.getImageLocalPath(fileId, function(has, fileLocalPath) { - // 本地有 - log('re img') - console.log(fileLocalPath); - // console.log(fs.exists(fileLocalPath)); - if(has && fileLocalPath) { - fs.exists(fileLocalPath, function(exists) { - if(exists) { - log('本地存在') - me.retImage(fileLocalPath, res); - } else { - getImageFromApi(); - } - }); - } else { - getImageFromApi(); - } - }); } - */ }; module.exports = Server; \ No newline at end of file