file main get image

This commit is contained in:
life
2015-12-18 16:50:34 +08:00
parent d18f1d1310
commit a2980904cf
9 changed files with 70 additions and 30 deletions

View File

@@ -63,10 +63,11 @@ var DB = {
* @param {Object} params { * @param {Object} params {
curUser: <User> 是当前用户 curUser: <User> 是当前用户
dbPath: string 是用户的dbPath dbPath: string 是用户的dbPath
dataBasePath: string 所有数据的基地址
* } * }
*/ */
ipc.on('db-init', function (event, params) { ipc.on('db-init', function (event, params) {
db.init(params.curUser, params.dbPath); db.init(params.curUser, params.dbPath, params.dataBasePath);
}); });
} }
}; };

8
node_modules/api_main.js generated vendored
View File

@@ -55,9 +55,7 @@ var Api = {
date: 'Mon, 19 Jan 2015 15:01:47 GMT', date: 'Mon, 19 Jan 2015 15:01:47 GMT',
*/ */
// log(resp.headers); // log(resp.headers);
if(err) { else {
callback(false);
} else {
var typeStr = resp.headers['content-type']; var typeStr = resp.headers['content-type'];
var type = 'png'; var type = 'png';
if(typeStr) { if(typeStr) {
@@ -68,15 +66,15 @@ var Api = {
} }
var filename = Common.uuid() + '.' + type; var filename = Common.uuid() + '.' + type;
var imagePath = User.getCurUserImagesPath(); var imagePath = Evt.getCurUserImagesPath();
var imagePathAll = imagePath + '/' + filename; var imagePathAll = imagePath + '/' + filename;
log(imagePathAll);
fs.writeFile(imagePathAll, resp.body, function(err) { fs.writeFile(imagePathAll, resp.body, function(err) {
if(err) { if(err) {
log(err); log(err);
log('local save image failed 本地保存失败'); log('local save image failed 本地保存失败');
callback(false); callback(false);
} else { } else {
console.log('main save image success');
callback(imagePathAll, filename); callback(imagePathAll, filename);
} }
}); });

9
node_modules/db_client.js generated vendored
View File

@@ -7,12 +7,6 @@ var path = require('path');
var Evt = require('evt'); var Evt = require('evt');
var ipc = require('ipc'); 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); // console.log(dbPath);
// g, 表全局环境 // g, 表全局环境
var db = {}; var db = {};
@@ -29,7 +23,8 @@ for(var i in dbNames) {
db.init = function (curUser, dbPath) { db.init = function (curUser, dbPath) {
ipc.send('db-init', { ipc.send('db-init', {
curUser: curUser, curUser: curUser,
dbPath: dbPath dbPath: dbPath,
dataBasePath: Evt.getBasePath()
}); });
}; };

4
node_modules/db_main.js generated vendored
View File

@@ -5,7 +5,7 @@ var Evt = require('evt_main');
var db = {}; var db = {};
// dbPath是用户的dbPath // dbPath是用户的dbPath
db.init = function (curUser, dbPath) { db.init = function (curUser, dbPath, dataBasePath) {
var me = this; var me = this;
var dbNames = [ var dbNames = [
'images', 'images',
@@ -29,7 +29,7 @@ db.init = function (curUser, dbPath) {
db._inited = true; db._inited = true;
// 保存起来 // 保存起来
Evt.init(curUser, dbPath); Evt.init(curUser, dbPath, dataBasePath);
console.log('db inited [main]'); console.log('db inited [main]');
}; };

20
node_modules/evt_main.js generated vendored
View File

@@ -9,6 +9,14 @@ var Evt = {
defaultUrl: 'https://leanote.com', defaultUrl: 'https://leanote.com',
leanoteUrl: 'https://leanote.com', leanoteUrl: 'https://leanote.com',
// leanoteUrl: 'http://localhost:9000', // leanoteUrl: 'http://localhost:9000',
//
dataBasePath: '',
init: function (curUser, dbPath, dataBasePath) {
this.curUser = curUser;
this.setHost(curUser.Host);
this.dataBasePath = dataBasePath;
},
setHost: function(host) { setHost: function(host) {
if(!host) { if(!host) {
@@ -26,18 +34,18 @@ var Evt = {
return this.leanoteUrl; return this.leanoteUrl;
}, },
init: function (curUser, dbPath) {
this.curUser = curUser;
this.setHost(curUser.Host);
},
getCurUserId: function () { getCurUserId: function () {
return this.curUser.UserId; return this.curUser.UserId;
}, },
getToken: function () { getToken: function () {
return this.curUser.Token; return this.curUser.Token;
} },
// 用户的图片路径
getCurUserImagesPath: function() {
return this.dataBasePath + '/data/' + this.getCurUserId() + '/images';;
},
}; };
module.exports = Evt; module.exports = Evt;

2
node_modules/file.js generated vendored
View File

@@ -533,7 +533,7 @@ var File = {
// 远程取不到图片, 是没有网络? 还是远程真的没有了 // 远程取不到图片, 是没有网络? 还是远程真的没有了
// TODO // TODO
// console.log("cann't get server's image" + fileId); // console.log("cann't get server's image" + fileId);
// callback(false); callback(false);
// return me.e404(res); // return me.e404(res);
} }
}); });

45
node_modules/file_main.js generated vendored
View File

@@ -1,6 +1,8 @@
var fs = require('fs'); var fs = require('fs');
var path = require('path'); var path = require('path');
var db = require('db_main'); var db = require('db_main');
var Api = require('api_main');
var Evt = require('evt_main');
function log(o) { function log(o) {
console.trace(o); console.trace(o);
@@ -71,11 +73,9 @@ var File = {
return callback(false); return callback(false);
} }
var Api = require('api');
// 访问api, 得到图片 // 访问api, 得到图片
function getImageFromApi() { function getImageFromApi() {
// console.log('fetch servers image ' + fileId); console.log('main fetch servers image ' + fileId);
Api.getImage(fileId, function(fileLocalPath, filename) { Api.getImage(fileId, function(fileLocalPath, filename) {
if(fileLocalPath) { if(fileLocalPath) {
// console.log('save image to local'); // console.log('save image to local');
@@ -93,11 +93,12 @@ var File = {
// 远程取不到图片, 是没有网络? 还是远程真的没有了 // 远程取不到图片, 是没有网络? 还是远程真的没有了
// TODO // TODO
// console.log("cann't get server's image" + fileId); // console.log("cann't get server's image" + fileId);
// callback(false); callback(false);
// return me.e404(res); // return me.e404(res);
} }
}); });
} }
// 先查看本地是否有该文件 // 先查看本地是否有该文件
// has表示本地数据库有记录 // has表示本地数据库有记录
me.getImageLocalPath(fileId, function(has, fileLocalPath) { me.getImageLocalPath(fileId, function(has, fileLocalPath) {
@@ -119,6 +120,40 @@ var File = {
getImageFromApi(); getImageFromApi();
} }
}); });
},
_addImage: function(fileId, absolutePath, callback, isForce) {
// var absolutePath = Evt.getBasePath() + '/' + path;
// 得到文件大小
var stat = fs.statSync(absolutePath);
var paths = absolutePath.split('/');
var name = paths[paths.length-1];
var names = name.split('.');
var ext = names[names.length-1];
var image = {
FileId: fileId,
ServerFileId: '',
Path: absolutePath,
Name: name,
UserId: Evt.getCurUserId(),
Title: name,
Type: ext,
Size: stat && stat.size,
IsDirty: true, // 本地是新添加的
CreatedTime: new Date()
};
if(isForce) {
image.ServerFileId = fileId;
}
db.images.insert(image, function(err, doc) {
console.log(err);
console.log(doc);
if(err) {
callback && callback(false);
} else {
callback && callback(doc);
}
})
} }
}; };

7
node_modules/note.js generated vendored
View File

@@ -1673,7 +1673,10 @@ var Note = {
if(!content) { if(!content) {
return; return;
} }
console.log('syncImages..................');
// 这里, 导致file_main不用调用api_main了, 因为这里会调用api处理
// console.log('syncImages..................');
// console.log(content); // console.log(content);
// 得到图片id // 得到图片id
// var reg = new RegExp(Evt.localUrl + "/api/file/getImage\\?fileId=(.{24})\"", 'g'); // var reg = new RegExp(Evt.localUrl + "/api/file/getImage\\?fileId=(.{24})\"", 'g');
@@ -1686,7 +1689,7 @@ var Note = {
// console.log(s); // console.log(s);
if(s && s.length >= 2) { if(s && s.length >= 2) {
var fileId = s[1]; var fileId = s[1];
console.log('sync image: ' + fileId); // console.log('sync image: ' + fileId);
if(!me.inSyncImage[fileId]) { if(!me.inSyncImage[fileId]) {
me.inSyncImage[fileId] = true; me.inSyncImage[fileId] = true;
File.getImage(fileId, function() { File.getImage(fileId, function() {

View File

@@ -666,7 +666,7 @@ window.debug = false;
<!-- 导入插件 执行 --> <!-- 导入插件 执行 -->
<script src="public/js/main.js"></script> <script src="public/js/main.js"></script>
<!-- markdown editor --> <!-- markdown editor -->
<script src="public/md/main-v2.min.js"></script> <script src="public/md/main-v2.js"></script>
<!-- extra额外 --> <!-- extra额外 -->
<script src="public/js/dec/main.js"></script> <script src="public/js/dec/main.js"></script>