mirror of
https://github.com/leanote/desktop-app.git
synced 2025-10-15 15:41:19 +00:00
183 lines
4.8 KiB
JavaScript
183 lines
4.8 KiB
JavaScript
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');
|
|
|
|
function log(o) {
|
|
console.log(o);
|
|
}
|
|
|
|
// 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"
|
|
},
|
|
start: function() {
|
|
var me = this;
|
|
if(me._started) {
|
|
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();
|
|
}
|
|
});
|
|
}
|
|
});
|
|
});
|
|
server.listen(me.port);
|
|
console.log("Server runing at port: " + me.port + ".");
|
|
me.localUrl = 'http://127.0.0.1:' + me.port;
|
|
me._started = true;
|
|
},
|
|
|
|
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';
|
|
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);
|
|
}
|
|
|
|
// 访问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; |