mirror of
https://github.com/leanote/desktop-app.git
synced 2025-10-14 23:22:40 +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);
|
||||
}
|
||||
};
|
||||
|
||||
|
@@ -436,7 +436,8 @@ function log(o) {
|
||||
</ul>
|
||||
<form id="uploadAttach" method="post" action="/attach/UploadAttach" enctype="multipart/form-data">
|
||||
<div id="dropAttach" class="dropzone">
|
||||
<a class="btn btn-success btn-choose-file">
|
||||
<input id="chooseFileInput" type="file" name="file" multiple/>
|
||||
<a id="chooseFile" class="btn btn-success btn-choose-file">
|
||||
<i class="fa fa-upload"></i>
|
||||
<span>Choose File</span>
|
||||
</a>
|
||||
@@ -448,7 +449,6 @@ function log(o) {
|
||||
<i class="fa fa-link"></i>
|
||||
<span>Link All</span>
|
||||
</a>
|
||||
<input type="file" name="file" multiple/>
|
||||
</div>
|
||||
<div id="attachUploadMsg">
|
||||
</div>
|
||||
|
@@ -1583,6 +1583,7 @@ var Attach = {
|
||||
attachsMap: {}, // attachId => attachInfo
|
||||
init: function() {
|
||||
var self = this;
|
||||
var me = this;
|
||||
// 显示attachs
|
||||
$("#showAttach").click(function(){
|
||||
self.renderAttachs(Note.curNoteId);
|
||||
@@ -1650,6 +1651,20 @@ var Attach = {
|
||||
tinymce.activeEditor.insertContent('<a target="_blank" href="' + src + '">' + title + '</a>');
|
||||
}
|
||||
});
|
||||
|
||||
$('#chooseFile').click(function() {
|
||||
$('#chooseFileInput').click();
|
||||
});
|
||||
// 得到路径, 保存文件即可
|
||||
$('#chooseFileInput').change(function() {
|
||||
var files = $(this).val();
|
||||
$(this).val('');
|
||||
FileService.addAttach(files, Note.curNoteId, function(files) {
|
||||
if(files) {
|
||||
me.addAttachs(files);
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
attachListO: $("#attachList"),
|
||||
attachNumO: $("#attachNum"),
|
||||
@@ -1695,7 +1710,7 @@ var Attach = {
|
||||
var attachNum = attachs.length;
|
||||
for(var i = 0; i < attachNum; ++i) {
|
||||
var each = attachs[i];
|
||||
html += '<li class="clearfix" data-id="' + each.AttachId + '">' +
|
||||
html += '<li class="clearfix" data-id="' + each.FileId + '">' +
|
||||
'<div class="attach-title">' + each.Title + '</div>' +
|
||||
'<div class="attach-process"> ' +
|
||||
' <button class="btn btn-sm btn-warning delete-attach" data-loading-text="..."><i class="fa fa-trash-o"></i></button> ' +
|
||||
@@ -1703,7 +1718,7 @@ var Attach = {
|
||||
' <button type="button" class="btn btn-sm btn-default link-attach" title="Insert link into content"><i class="fa fa-link"></i></button> ' +
|
||||
'</div>' +
|
||||
'</li>';
|
||||
self.attachsMap[each.AttachId] = each;
|
||||
self.attachsMap[each.FileId] = each;
|
||||
}
|
||||
self.attachListO.html(html);
|
||||
|
||||
@@ -1748,6 +1763,11 @@ var Attach = {
|
||||
self.loadedNoteAttachs[attachInfo.NoteId].push(attachInfo);
|
||||
self.renderAttachs(attachInfo.NoteId);
|
||||
},
|
||||
addAttachs: function(attachInfos) {
|
||||
for(var i in attachInfos) {
|
||||
this.addAttach(attachInfos[i]);
|
||||
}
|
||||
},
|
||||
// 删除
|
||||
deleteAttach: function(attachId) {
|
||||
var self = this;
|
||||
|
10
test.js
10
test.js
@@ -20,9 +20,17 @@ Api.addNotebook({
|
||||
User.userId = '54bdc65599c37b0da9000002';
|
||||
// console.log(User.getCurActiveUserId());
|
||||
Note.getDirtyNotes(function(ret) {
|
||||
Api.updateNote(ret[2], function(ret2){
|
||||
console.log(ret2);
|
||||
});
|
||||
|
||||
// console.log(ret[2]);
|
||||
/*
|
||||
Api.addNote(ret[0], function(ret2){
|
||||
console.log(ret2);
|
||||
})
|
||||
});
|
||||
*/
|
||||
|
||||
});
|
||||
/*
|
||||
Note.getNoteByServerNoteId("54c6313799c37bdeec000008", function(ret){
|
||||
|
Reference in New Issue
Block a user