sync attach ok

This commit is contained in:
life
2015-02-09 23:48:47 +08:00
parent b7b5d241f7
commit 22283f22c7
8 changed files with 214 additions and 9 deletions

65
node_modules/api.js generated vendored
View File

@@ -222,8 +222,6 @@ var Api = {
callback(false);
} else {
var typeStr = resp.headers['content-type'];
log(resp.headers);
log(typeStr);
var type = 'png';
if(typeStr) {
var typeArr = typeStr.split('/');
@@ -249,6 +247,69 @@ var Api = {
});
},
// 获取附件
// FileService调用
getAttach: function(serverFileId, callback) {
var me = this;
var url = me.getUrl('file/getAttach', {fileId: serverFileId});
console.log(url);
needle.get(url, function(err, resp) {
if(err) {
return callback && callback(false);
}
// log(resp.body);
/*
{ 'accept-ranges': 'bytes',
'content-disposition': 'inline; filename="logo.png"',
'content-length': '8583',
'content-type': 'image/png', ""
date: 'Mon, 19 Jan 2015 15:01:47 GMT',
'accept-ranges': 'bytes',
'content-disposition': 'attachment; filename="box.js"',
'content-length': '45503',
'content-type': 'application/javascript',
*/
// console.log(resp.headers);
// return;
if(err) {
callback(false);
} else {
// TODO 这里, 要知道文件类型
var typeStr = resp.headers['content-type'];
var contentDisposition = resp.headers['content-disposition'];
var matches = contentDisposition.match(/filename="(.+?)"/);
var filename = matches && matches.length == 2 ? matches[1] : "";
// log(resp.headers);
// log(typeStr);
var type = '';
if(filename) {
type = filename.split('.').pop();
}
if(!filename && typeStr) {
var typeArr = typeStr.split('/');
if(typeStr.length > 1) {
type = typeArr[1];
}
}
var filename = Common.uuid() + '.' + type;
var attachPath = User.getCurUserAttachsPath();
var attachPathAll = attachPath + '/' + filename;
log(attachPathAll);
fs.writeFile(attachPathAll, resp.body, function(err) {
if(err) {
log(err);
log('local save attach failed 本地保存失败');
callback(false);
} else {
callback(true, attachPathAll, filename);
}
});
}
});
},
//------------
// 笔记本操作
//------------

3
node_modules/evt.js generated vendored
View File

@@ -9,6 +9,9 @@ var Evt = {
getImageLocalUrl: function(fileId) {
return this.localUrl + '/api/file/getImage?fileId=' + fileId;
},
getAttachLocalUrl: function(fileId) {
return this.localUrl + '/api/file/getAttach?fileId=' + fileId;
},
// 项目绝对地址
getBasePath: function() {
return process.cwd();

29
node_modules/file.js generated vendored
View File

@@ -205,6 +205,35 @@ var File = {
});
},
// 下载, 复制一份文件
download: function(srcPath, toPath, callback) {
var srcIsExists = fs.existsSync(srcPath);
if(!srcIsExists) {
return callback(false, 'File Not Exists');
}
console.log(srcPath);
console.log(toPath);
var toIsExists = fs.existsSync(toPath);
function cp() {
Common.copyFile(srcPath, toPath, function(ok) {
callback(ok);
});
}
if(toIsExists) {
fs.unlink(toPath, function(error) {
if(!error) {
cp();
} else {
callback(false, 'The Target File Cannot Overwrite');
}
});
} else {
cp();
}
},
// 附件操作
addAttach: function(filePaths, noteId, callback) {
if(!noteId || !filePaths) {

62
node_modules/note.js generated vendored
View File

@@ -1086,6 +1086,68 @@ var Note = {
}
},
/*
1) sync时判断是否有attach, 如果有, 则异步下载之
2) 前端render note时, 判断是否有未Path的attach, 调用该服务
从服务器端下载文件, 并通过到前端已下载完成
*/
inDownload: {}, // 正在下载的文件 fileId => true
downloaded: {}, // 下载完成的
downloadAttachFromServer: function(noteId, serverFileId, fileId) {
var me = this;
if(me.inDownload[serverFileId]) {
return;
}
if(!Api) {
Api = require('api');
}
me.inDownload[serverFileId] = true;
Api.getAttach(serverFileId, function(ok, toPath, filename) {
me.inDownload[serverFileId] = false;
if(ok) {
me.downloaded[serverFileId] = fileId;
// 更新serverFileId与fileId的映射, 修改的是note
me.syncAttach(noteId, serverFileId, fileId, toPath, filename, function(ok, attachs, attach) {
if(ok) {
// 通知web
Web.attachSynced(attachs, attach, noteId);
}
});
} else {
// 下次再下载 ?
// 或者放到一个队列中 ?
// TODO
}
});
},
// 同步附件, 更新serverFileId
syncAttach: function(noteId, serverFileId, fileId, path, filename, callback) {
var me = this;
me.getNote(noteId, function(note) {
if(!note) {
callback(false);
}
var attachs = note.Attachs;
for(var i in attachs) {
var attach = attachs[i];
if(attach.FileId == fileId) {
attach.ServerFileId = serverFileId;
attach.Path = path;
// attach.Title = filename;
// attach.Filename = filename;
Notes.update({_id: note._id}, {$set: {Attachs: attachs}}, function() {
callback(true, attachs, attach);
});
break;
}
}
callback(false);
});
},
// 根据标签得到笔记数量
countNoteByTag: function(title, callback) {
var userId = User.getCurActiveUserId();

9
node_modules/web.js generated vendored
View File

@@ -11,10 +11,11 @@ var Web = {
Tag: null,
// 注入前端变量
set: function(notebook, note, tag) {
set: function(notebook, note, attach, tag) {
var me = this;
me.Notebook = notebook;
me.Note = note;
me.Attach = attach;
me.Tag = tag;
},
@@ -32,6 +33,12 @@ var Web = {
me.Tag.addTagsNav(adds);
var deletes = tagSyncInfo.deletes;
me.Tag.deleteTagsNav(deletes);
},
// 通过attach已同步成功
attachSynced: function(attachs, attach, noteId) {
var me = this;
me.Attach.attachSynced(attachs, attach, noteId);
}
};
module.exports = Web;

View File

@@ -1725,6 +1725,9 @@ Note.initContextmenu = function() {
var Attach = {
loadedNoteAttachs: {}, // noteId => [attch1Info, attach2Info...] // 按笔记
attachsMap: {}, // attachId => attachInfo
getAttach: function(attachId) {
return this.attachsMap[attachId];
},
init: function() {
var self = this;
var me = this;
@@ -1748,10 +1751,12 @@ var Attach = {
}
});
// 下载
var curAttachId = '';
self.attachListO.on("click", ".download-attach", function(e) {
e.stopPropagation();
var $li = $(this).closest('li');
var attachId = $li.data("id");
curAttachId = attachId;
$('#downloadFileInput').attr('nwsaveas', $li.find('.attach-title').text()).click();
// window.open(UrlPrefix + "/attach/download?attachId=" + attachId);
@@ -1759,7 +1764,6 @@ var Attach = {
});
// 下载全部
self.downloadAllBtnO.click(function() {
// window.open(UrlPrefix + "/attach/downloadAll?noteId=" + Note.curNoteId);
// location.href = "/attach/downloadAll?noteId=" + Note.curNoteId;
});
@@ -1767,7 +1771,18 @@ var Attach = {
$('#downloadFileInput').change(function(e) {
var value = $(this).val();
$(this).val('');
alert(value);
var curAttach = me.getAttach(curAttachId);
if(curAttach) {
FileService.download(curAttach.Path, value, function(ok, msg) {
if(!ok) {
alert(msg || "error");
} else {
// TODO 提示下载成功
}
});
} else {
alert('error');
}
});
// make link
@@ -1775,7 +1790,7 @@ var Attach = {
e.stopPropagation();
var attachId = $(this).closest('li').data("id");
var attach = self.attachsMap[attachId];
var src = UrlPrefix + "/attach/download?attachId=" + attachId;
var src = EvtService.getAttachLocalUrl(attachId); // + "/attach/download?attachId=" + attachId;
if(LEA.isMarkdownEditor() && MD) {
MD.insertLink(src, attach.Title);
} else {
@@ -1871,16 +1886,20 @@ var Attach = {
var each = attachs[i];
var path = each.Path;
// 本地是否有, 没有, 是否是在显示的时候才去从服务器上抓? 不
var disabled = '';
if(path) {
var d = '<i class="fa fa-download"></i>';
} else {
d = 'no'
d = '...'
disabled = 'disabled';
// 通过后端去下载
NoteService.downloadAttachFromServer(Note.curNoteId, each.ServerFileId, each.FileId);
}
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> ' +
' <button type="button" class="btn btn-sm btn-primary download-attach">' + d + '</button> ' +
' <button type="button" class="btn btn-sm btn-primary download-attach" ' + disabled + '>' + d + '</button> ' +
' <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>';
@@ -1979,6 +1998,21 @@ var Attach = {
var self = this;
},
downloadAll: function() {
},
// 服务器端同步成功后调用
attachSynced: function(attachs, attach, noteId) {
var me = this;
var fileId = attach.FileId;
var note = Note.getNote(noteId);
if(note) {
note.Attachs = attachs;
me.attachsMap[fileId] = attach;
if(noteId == Note.curNoteId) {
// 重新render之
me.renderAttachs(noteId);
}
}
}
}

View File

@@ -1315,7 +1315,7 @@ function incrSync() {
// 判断是否登录
function initPage() {
// 注入前端变量#
WebService.set(Notebook, Note, Tag);
WebService.set(Notebook, Note, Attach, Tag);
function _init() {
$(function() {

View File

@@ -18,9 +18,18 @@ Api.addNotebook({
*/
// Api.uploadImage();
User.userId = '54bdc65599c37b0da9000002';
User.userId = '54d7620d99c37b030600002c';
User.init(function() {
Api.getAttach('54d8c8de99c37b02fa000002', function() {
});
});
/*
Note.hasNotes('54bdc65599c37b0da9000005', function(doc) {
console.log(doc);
});
*/
/*
// console.log(User.getCurActiveUserId());
Note.getDirtyNotes(function(ret) {