-
Notifications
You must be signed in to change notification settings - Fork 598
/
file.js
596 lines (564 loc) · 16.4 KB
/
file.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
var db = require('db');
var fs = require('fs');
var needle = require('needle');
var path = require('path');
var Evt = require('evt');
var User = require('user');
var Common = require('common');
var Images = db.images;
var Attachs = db.attachs;
var Web = require('web');
function log(o) {
console.log(o);
}
/*
type File struct {
FileId bson.ObjectId `bson:"_id,omitempty"` //
UserId bson.ObjectId `bson:"UserId"`
AlbumId bson.ObjectId `bson:"AlbumId"`
Name string `Name` // file name
Title string `Title` // file name or user defined for search
Size int64 `Size` // file size (byte)
Type string `Type` // file type, "" = image, "doc" = word
Path string `Path` // the file path
IsDefaultAlbum bool `IsDefaultAlbum`
CreatedTime time.Time `CreatedTime`
FromFileId bson.ObjectId `bson:"FromFileId,omitempty"` // copy from fileId, for collaboration
}
*/
// 文件服务
var File = {
// 将base64字符串写入文件中, 为导入笔记
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 err = fs.writeFileSync(filePath, new Buffer(data, 'base64'));
if(err) {
return callback(false);
}
if(isImage) {
me._addImage(Common.objectId(), filePath, function(newImg) {
newImg.IsImage = true;
callback && callback(newImg);
});
} else {
me._addAttach(filePath, fileTitle, callback)
}
/*
// 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);
}
},
_addAttach: function(filePath, fileTitle, callback) {
try {
var fileStat = fs.statSync(filePath);
} catch(e) {
callback(false);
return;
}
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 attach = {
FileId: Common.objectId(),
ServerFileId: '',
Path: filePath,
NoteId: '',
Name: name,
UserId: User.getCurActiveUserId(),
Title: fileTitle,
IsDirty: true, // 先添加的肯定是dirty, 什么时候不是dirty ? sync 和 send changes后
Type: ext,
Size: fileStat && fileStat.size,
IsDirty: true, // 本地是新添加的, ServerFileId = 0
CreatedTime: new Date()
};
db.attachs.insert(attach);
callback && callback(attach);
},
// data在web端获取到, 在nodejs端会有错误!!
pasteImage2: function(data, callback) {
var me = this;
data = data.replace(/^data:image\/\w+;base64,/, "");
// log(User.getCurUserImagesPath());
var filename = Common.uuid() + '.png';
var filePath = User.getCurUserImagesPath() + '/' + filename;
console.log(filePath);
// return;
fs.writeFile(filePath, new Buffer(data, 'base64'), function(err) {
if(err) {
console.log(err);
return;
}
// return;
// 保存
// var relativePath = User.getCurUserImagesAppPath() + '/' + filename;
// 保存到数据库中
me._addImage(Common.objectId(), filePath, function(newImg) {
callback && callback(Evt.getImageLocalUrl(newImg.FileId));
});
});
},
// FileReaderWeb 是 web上的FileReader, 可能与nodejs这个有冲突
pasteImage: function(event, FileReaderWeb, callback) {
var me = this;
var items = (event.clipboardData || event.originalEvent.clipboardData).items; // 可能有多个file, 找到属于图片的file
// find pasted image among pasted items
var blob;
for (var i = 0; i < items.length; i++) {
if (items[i].type.indexOf("image") === 0) {
blob = items[i].getAsFile();
}
}
// console.log("paste images");
console.log(blob);
// load image if there is a pasted image
if (blob) {
try {
// console.log("??");
var reader = new FileReaderWeb();
// console.log(">>")
// return;
// console.log(">>")
/*
reader.onloadend = function() {
console.log(reader);
// 这个事件在读取结束后,无论成功或者失败都会触发
if (reader.error) {
console.log(reader.error);
} else {
}
};
console.log("-----------");
reader.onload = function(e) {
// alert(3);
console.log('haha');
return;
// 上传之
// log('result');
// log(reader.result);
var ret = reader.result;
ret = ret.replace(/^data:image\/\w+;base64,/, "");
console.log(ret);
return;
// log(User.getCurUserImagesPath());
var filename = Common.uuid() + '.png';
var filePath = User.getCurUserImagesPath() + '/' + filename;
console.log(filePath);
return;
fs.writeFile(filePath, new Buffer(ret, 'base64'), function(err) {
if(err) {
log(err);
return;
}
// 保存
// var relativePath = User.getCurUserImagesAppPath() + '/' + filename;
// 保存到数据库中
File.addImage(filePath, function(newImg) {
callback && callback(Evt.getImageLocalUrl(newImg.FileId));
// callback && callback('app://leanote/' + relativePath);
});
});
};
*/
console.log(reader);
console.log("??");
reader.onloadend = function() {
console.log('end');
console.log(reader.result);
};
reader.readAsDataURL(blob);
} catch(e) {
console.log(e);
}
};
},
// path是相对于项目路径
addImage: function(path, callback) {
var me = this;
var absolutePath = Evt.getBasePath() + '/' + path;
me._addImage(Common.objectId(), absolutePath, callback);
},
_addImage: function(fileId, absolutePath, callback, isForce) {
// var absolutePath = Evt.getBasePath() + '/' + path;
// 得到文件大小
var stat = fs.statSync(absolutePath);
var paths = absolutePath.split('/');
var name = paths[paths.length-1];
var names = name.split('.');
var ext = names[names.length-1];
var image = {
FileId: fileId,
ServerFileId: '',
Path: absolutePath,
Name: name,
UserId: User.getCurActiveUserId(),
Title: name,
Type: ext,
Size: stat && stat.size,
IsDirty: true, // 本地是新添加的
CreatedTime: new Date()
};
if(isForce) {
image.ServerFileId = fileId;
}
db.images.insert(image, function(err, doc) {
log(err);
if(err) {
callback && callback(false);
} else {
callback && callback(doc);
}
})
},
// 从服务器上把文件download下来后
// 这里的fileId是serverFileId, 本地的FileId也保存为该值
addImageForce: function(fileId, path, callback) {
var me = this;
// 先删除之, 可能是本地有记录, 但是文件没了
db.images.remove({FileId: fileId}, function() {
me._addImage(fileId, path, callback, true);
});
},
// 获取图片本地路径
// 通过FileId可ServerFileId来查找
// 因为图片的链接 有可能是本地添加的, 又有可能是远程的
// 如果是远程的, FileId == ServerFileId, 是一样的, 所以不要Or
getImageLocalPath: function(fileId, callback) {
// db.images.findOne({$or: {FileId: fileId}, {ServerFileId: fileId}}, function(err, doc) {
db.images.findOne({FileId: fileId}, function(err, doc) {
if(!err && doc && doc.Path) { // FileLocalPath是相对于项目的路径
callback(true, doc.Path);
} else {
callback(false, false);
}
});
},
// 得到fileIds所有的images, 为了发送到服务器上
getAllImages: function(fileIds, callback) {
var me = this;
db.images.find({$or:[{FileId: {$in: fileIds}}, {ServerFileId: {$in: fileIds}}]}, function(err, images) {
if(err || !images) {
return callback(false);
}
callback(images);
});
},
// 笔记添加/修改后会有LocalFileId <=> FileId映射
// 这个只对image有用
updateImageForce: function(files) {
if(!files) {
// callback && callback(false);
return;
}
for(var i in files) {
var file = files[i];
if(file.IsAttach) {
continue;
}
if(!file.FileId || !file.LocalFileId) {
continue;
}
db.images.update({FileId: file.LocalFileId}, {$set: {ServerFileId: file.FileId, IsDirty: false}});
}
},
// tinymce 或 mceeditor上传图片
// callback({FileId: "xx"})
uploadImage: function(imagePath, callback) {
var me = this;
// 读取文件, 查看是否是图片
var filePathAttr = Common.splitFile(imagePath);
var ext = filePathAttr.ext;
if(!Common.isImageExt(ext)) {
return callback(false, 'Please select a image');
}
var fileId = Common.objectId();
// 复制到图片文件夹
filePathAttr.nameNotExt = fileId + '_cp_';
var newFilename = fileId + '.' + ext;
var newFilePath = User.getCurUserImagesPath() + '/' + newFilename;
// 复制之, 并写入到数据库中
Common.copyFile(imagePath, newFilePath, function(ret) {
if(ret) {
me._addImage(fileId, newFilePath, callback);
} else {
callback(false);
}
});
},
// 处理用户图片
getImage: function(fileId, callback) {
var me = this;
if(!fileId) {
return callback(false);
}
var Api = require('api');
// 访问api, 得到图片
function getImageFromApi() {
// console.log('fetch servers image ' + fileId);
Api.getImage(fileId, function(fileLocalPath, filename) {
if(fileLocalPath) {
// console.log('save image to local');
// 保存到本地数据库中
me.addImageForce(fileId, fileLocalPath, function(doc) {
if(doc) {
// console.log('save image to local success');
} else {
// console.log('save image to local error');
}
callback(fileLocalPath);
// return me.retImage(fileLocalPath, res);
});
} else {
// 远程取不到图片, 是没有网络? 还是远程真的没有了
// TODO
// console.log("cann't get server's image" + fileId);
// callback(false);
// return me.e404(res);
}
});
}
// 先查看本地是否有该文件
// has表示本地数据库有记录
me.getImageLocalPath(fileId, function(has, fileLocalPath) {
// 本地有
// console.log('re img')
// console.log(fileLocalPath);
// console.log(fs.exists(fileLocalPath));
if(has && fileLocalPath) {
fs.exists(fileLocalPath, function(exists) {
if(exists) {
// console.log('本地存在');
callback(fileLocalPath);
// me.retImage(fileLocalPath, res);
} else {
getImageFromApi();
}
});
} else {
getImageFromApi();
}
});
},
// 下载, 复制一份文件
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 && callback(ok);
});
}
if(toIsExists) {
fs.unlink(toPath, function(error) {
if(!error) {
cp();
} else {
callback && callback(false, 'The Target File Cannot Overwrite');
}
});
} else {
cp();
}
},
// 附件操作
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);
},
// 删除不存在的attachs
deleteNotExistsAttach: function(noteId, attachs) {
var me = this;
// console.log('--');
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);
}
}
}
});
},
// 下载图片, 本地的, 或外站的
downloadImg: function(src, callback) {
var me = this;
// 本地的
if(src.indexOf('http://127.0.0.1') != -1) {
var ret = /fileId=([a-zA-Z0-9]{24})/.exec(src);
if(ret && ret.length == 2) {
var fileId = ret[1];
me.getImage(fileId, function(filePath) {
callback(filePath);
});
} else {
callback();
}
} else {
// 远程的图片
needle.get(src, function(err, resp) {
// console.log(resp);
/*
{ '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',
*/
// log(resp.headers);
if(err || !resp || resp.statusCode == 404) {
callback(false);
} else {
// 当图片没有时候还是执行这一步
var typeStr = resp.headers['content-type'];
var type = 'png';
if(typeStr) {
var typeArr = typeStr.split('/');
if(typeStr.length > 1) {
type = typeArr[1];
}
}
var filename = Common.uuid() + '.' + type;
var imagePath = User.getCurUserImagesPath();
var imagePathAll = imagePath + '/' + filename;
fs.writeFile(imagePathAll, resp.body, function(err) {
if(err) {
callback(false);
} else {
callback(imagePathAll);
}
});
}
});
}
},
};
module.exports = File;