上传文件

Note表是否要有Attachs信息呢? 还是只要一个attachNum ?
This commit is contained in:
life
2015-01-30 00:04:22 +08:00
parent ed37b1178c
commit 4a78441cad
4 changed files with 77 additions and 5 deletions

44
node_modules/file.js generated vendored
View File

@@ -5,6 +5,7 @@ var Evt = require('evt');
var User = require('user');
var Common = require('common');
var Images = db.images;
var Attachs = db.attachs;
function log(o) {
console.log(o);
@@ -173,6 +174,49 @@ var File = {
}
Images.update({FileId: file.LocalFileId}, {$set: {ServerFileId: file.FileId, IsDirty: false}});
}
},
// 附件操作
addAttach: function(filePaths, noteId, callback) {
if(!noteId || !filePaths) {
return callback && callback(false);
}
filePaths = filePaths.split(';');
// 复制每一个文件, 保存到数据库中
var targets = [];
for(var i in filePaths) {
var filePath = filePaths[i];
var fileStat = fs.statSync(filePath);
var paths = filePath.split('/');
var name = paths[paths.length-1];
var names = name.split('.');
var ext = names[names.length-1];
var rename = Common.uuid() + "." + ext;
var distPath = User.getCurUserAttachsAppPath() + '/' + rename;
var readable = fs.createReadStream(filePath);
// 创建写入流
var writable = fs.createWriteStream(distPath);
// 通过管道来传输流
readable.pipe(writable);
var attach = {
FileId: Common.objectId,
ServerFileId: '',
Path: distPath,
NoteId: noteId,
Name: rename,
UserId: User.getCurActiveUserId(),
Title: name,
Type: ext,
Size: fileStat && fileStat.size,
IsDirty: true, // 本地是新添加的
CreatedTime: new Date()
};
Attachs.insert(attach);
targets.push(attach);
}
callback && callback(targets);
}
};