mirror of
https://github.com/leanote/desktop-app.git
synced 2025-10-15 15:41:19 +00:00
上传文件
Note表是否要有Attachs信息呢? 还是只要一个attachNum ?
This commit is contained in:
44
node_modules/file.js
generated
vendored
44
node_modules/file.js
generated
vendored
@@ -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);
|
||||
}
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user