-
Notifications
You must be signed in to change notification settings - Fork 0
/
avatars.example.js
73 lines (63 loc) · 1.96 KB
/
avatars.example.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
import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import { Accounts } from 'meteor/accounts-base';
import { FilesCollection } from 'meteor/ostrio:files';
export const Avatars = new FilesCollection({
collectionName: 'avatars',
allowClientCode: true,
downloadRoute: '/images/',
storagePath: 'assets/images/avatars/',
});
if (Meteor.isServer) {
Meteor.publish('avatar', function () {
if (!this.userId) {
return this.ready();
}
return Avatars.findOne({
'meta.userId': this.userId
}, {
fields: {
'_id': 1
}
}).cursor;
});
const _multer = require('multer');
const _fs = require('fs');
const _multerInstanceConfig = { dest: '/tmp' }; // Temp dir for multer
const _multerInstance = _multer(_multerInstanceConfig);
Picker.middleware(_multerInstance.single('photo'));
Picker.route('/api/v1/upload', function(params, req, res, next) {
if (req.file !== undefined && req.file.mimetype.substr(0, 6) == 'image/' && params.query.authToken.length) {
const hashedToken = Accounts._hashLoginToken(params.query.authToken);
const user = Meteor.users.findOne({ 'services.resume.loginTokens.hashedToken': hashedToken });
if (user) {
Avatars.remove({ 'meta.userId': user._id });
_fs.stat(req.file.path, function (_statError, _statData) {
const _addFileMeta = {
fileName: req.file.originalname,
type: req.file.mimetype,
size: req.file.size,
meta: {
userId: user._id
}
};
_fs.readFile(req.file.path, function (_readError, _readData) {
if (_readError) {
console.log(_readError);
} else {
Avatars.write(_readData, _addFileMeta, function (_uploadError, _uploadData) {
if (_uploadError) {
console.log(_uploadError);
} else {
console.log('upload data=', _uploadData);
_fs.unlink(req.file.path); // remove temp upload
}
});
}
});
});
}
}
res.end();
});
}