mirror of
https://github.com/leanote/desktop-app.git
synced 2025-10-14 23:22:40 +00:00
59 lines
1.5 KiB
JavaScript
59 lines
1.5 KiB
JavaScript
var db = require('db');
|
|
var fs = require('fs');
|
|
var Evt = require('evt');
|
|
var User = require('user');
|
|
var Images = db.images;
|
|
|
|
function log(o) {
|
|
console.log(o);
|
|
}
|
|
/*
|
|
type File struct {
|
|
FileId bson.ObjectId `bson:"_id,omitempty"` //
|
|
UserId bson.ObjectId `bson:"UserId"`
|
|
AlbumId bson.ObjectId `bson:"AlbumId"`
|
|
Name string `Name` // file name
|
|
Title string `Title` // file name or user defined for search
|
|
Size int64 `Size` // file size (byte)
|
|
Type string `Type` // file type, "" = image, "doc" = word
|
|
Path string `Path` // the file path
|
|
IsDefaultAlbum bool `IsDefaultAlbum`
|
|
CreatedTime time.Time `CreatedTime`
|
|
|
|
FromFileId bson.ObjectId `bson:"FromFileId,omitempty"` // copy from fileId, for collaboration
|
|
}
|
|
|
|
*/
|
|
|
|
// 图片服务
|
|
var Image = {
|
|
// path是相对于项目路径
|
|
addImage: function(path, callback) {
|
|
var absolutePath = Evt.getBasePath() + '/' + path;
|
|
// 得到文件大小
|
|
var stat = fs.statSync(absolutePath);
|
|
var paths = path.split('/');
|
|
var name = paths[paths.length-1];
|
|
var names = name.split('.');
|
|
var ext = names[names.length-1];
|
|
var image = {
|
|
Path: path,
|
|
Name: name,
|
|
UserId: User.getCurActiveUserId(),
|
|
Title: name,
|
|
Type: ext,
|
|
Size: stat && stat.size,
|
|
CreatedTime: new Date()
|
|
}
|
|
Images.insert(image, function(err, doc) {
|
|
log(err);
|
|
if(err) {
|
|
callback && callback(false);
|
|
} else {
|
|
callback && callback(doc);
|
|
}
|
|
})
|
|
}
|
|
};
|
|
|
|
module.exports = Image; |