me.getNote(noteId, function(note) { if(!note) { callback(); return; } var everAttachs = note.Attachs || []; var nowMap = {}; for(var i in attachs) { nowMap[attachs[i].FileId] = attachs[i]; } // console.log(note); // console.log('end'); // console.log(everAttachs.length); // console.log(attachs.length); // console.log(attachs == everAttachs); var fileBasePath = User.getCurUserAttachsPath(); for(var i in everAttachs) { var attach = everAttachs[i]; var path = attach.Path; if(!nowMap[attach.FileId]) { // 如果不在, 则删除之 // console.log(">>>>>>>>>"); try { // 删除源文件, 别删错了啊 if(path.indexOf(fileBasePath) >= 0) { fs.unlink(path); } } catch(e) { console.log(e); } } } // 一个坑!!!!!!!!!!! callback(); });
copyFile: function(originPath, isImage, callback) { var me = this; var basePath; if(isImage) { basePath = User.getCurUserImagesPath(); } else { basePath = User.getCurUserAttachsPath(); } var filePathAttr = Common.splitFile(originPath); var fileId = Common.objectId(); var newFilename = fileId + '_html_' + '.' + filePathAttr.ext; var newFilePath = basePath + '/' + newFilename; Common.copyFile(originPath, newFilePath, function(ret) { if(ret) { if(isImage) { me._addImage(fileId, newFilePath, function(newImg) { newImg.IsImage = true; callback(newImg); }); } else { me._addAttach(newFilePath, '', function(attach) { callback(attach); }); } } else { callback(false); } }); },
needle.get(url, function(err, resp) { me.checkError(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); } }); } });
writeBase64: function(data, isImage, type, fileTitle, callback) { var me = this; var filename = Common.uuid() + '.' + type; if(isImage) { var basePath = User.getCurUserImagesPath(); } else { var basePath = User.getCurUserAttachsPath(); } var filePath = basePath + '/' + filename; // test // return callback && callback({FileId: Common.objectId(), IsImage: true}); // return; try { var bf = new Buffer(data, 'base64'); var err = fs.writeFileSync(filePath, bf); if(err) { return callback(false); } // 得到文件的hash var hash = crypto.createHash('md5'); hash.setEncoding('hex'); hash.write(bf); hash.end(); var fileHash = hash.read(); if(isImage) { me._addImage(Common.objectId(), filePath, function(newImg) { newImg.IsImage = true; newImg.hash = fileHash; callback && callback(newImg); }); } else { me._addAttach(filePath, fileTitle, function(attach) { attach.hash = fileHash; callback && callback(attach); }); } } catch(e) { console.log("error!!!"); console.error(e); } },
deleteAttachs: function(attachs) { if (!attachs || attachs.length == 0) { return; } var me = this; var fileBasePath = User.getCurUserAttachsPath(); for(var i = 0; i < attachs.length; ++i) { var path = attachs[i].Path; if(path && path.indexOf(fileBasePath) >= 0) { try { fs.unlink(path); } catch(e) { console.error(e); } } } },
deleteAttachs: function(attachs) { var me = this; var fileBasePath = User.getCurUserAttachsPath(); if(!attachs) { return; } for(var i in attachs) { var path = attachs[i].Path; if(path && path.indexOf(fileBasePath) > 0) { try { fs.unlink(path); } catch(e) { console.log(e); } } } },
addAttach: function(filePaths, noteId, callback) { if(!noteId || !filePaths || filePaths.length == 0) { 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(Common.getPathSep()); // windows的filePath不同 var name = paths[paths.length-1]; // Web.alertWeb(name); var names = name.split('.'); var ext = names[names.length-1]; var rename = Common.uuid() + "." + ext; var distPath = User.getCurUserAttachsPath() + '/' + 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, IsDirty: true, // 先添加的肯定是dirty, 什么时候不是dirty ? sync 和 send changes后 Type: ext, Size: fileStat && fileStat.size, IsDirty: true, // 本地是新添加的, ServerFileId = 0 CreatedTime: new Date() }; db.attachs.insert(attach); targets.push(attach); } callback && callback(targets); },
db.attachs.find({NoteId: noteId}, function(err, everAttachs) { if(err) { return; } var nowMap = {}; for(var i in attachs) { nowMap[attachs[i].FileId] = attachs[i]; } var fileBasePath = User.getCurUserAttachsPath(); for(var i in everAttachs) { var attach = everAttachs[i]; if(!nowMap[attach.FileId]) { // 如果不在, 则删除之 db.attachs.remove({FileId: attach.FileId}); // 删除源文件, 别删错了啊 if(attach.Path.indexOf(fileBasePath) >= 0) { fs.unlink(attach.Path); } } } });
writeBase64: function(data, isImage, type, fileTitle, callback) { var me = this; var filename = Common.uuid() + '.' + type; if(isImage) { var basePath = User.getCurUserImagesPath(); } else { var basePath = User.getCurUserAttachsPath(); } var filePath = basePath + '/' + filename; // test // return callback && callback({FileId: Common.objectId(), IsImage: true}); // return; try { var bf = new Buffer(data, 'base64'); var err = fs.writeFileSync(filePath, bf); if(err) { return callback(false); } // 得到文件的hash var hash = crypto.createHash('md5'); hash.setEncoding('hex'); hash.write(bf); hash.end(); var fileHash = hash.read(); if(isImage) { me._addImage(Common.objectId(), filePath, function(newImg) { newImg.IsImage = true; newImg.hash = fileHash; callback && callback(newImg); }); } else { me._addAttach(filePath, fileTitle, function(attach) { attach.hash = fileHash; callback && callback(attach); }); } /* // var buf = new Buffer(data, 'utf-8'); // 读出来 var err = fs.writeFile(filePath, data, 'ascii'); if(err) { console.log(err); callback(false); return; } */ /* var exec = require('child_process').exec, last = exec('"/Users/life/Documents/kuaipan/go/go-study/bin/file2" "' + txtPath + '" "' + filePath + '"'); // last = exec('ls /'); // last.stdout.on('data', function (data) { // console.log('标准输出:' + data); // }); last.on('exit', function (code) { console.log('子进程已关闭,代码:' + code); fs.unlink(txtPath); if(!code) { if(isImage) { me._addImage(Common.objectId(), filePath, function(newImg) { newImg.IsImage = true; callback && callback(newImg); }); } else { me._addAttach(filePath, fileTitle, callback) } } else { callback(false); } }); */ /* // console.log(txtPath); // console.log(filePath); // 先写到txt中去, 然后调用命令将.txt -> img var txtPath = filePath + '.txt'; var err = fs.writeFileSync(txtPath, data); Common.cmd([txtPath, filePath], function(code) { // console.log('子进程已关闭,代码:' + code); // fs.unlink(txtPath); if(!code) { try { if(isImage) { me._addImage(Common.objectId(), filePath, function(newImg) { newImg.IsImage = true; callback && callback(newImg); }); } else { me._addAttach(filePath, fileTitle, callback) } } catch(e) { console.log(e); callback(false); } } else { callback(false); } }); */ // return callback && callback({FileId: Common.objectId(), IsImage: true}); } catch(e) { console.log("error!!!"); console.error(e); } },