forked from wesyah234/fileUploadDemo2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileupload.js
100 lines (92 loc) · 3.59 KB
/
fileupload.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
Uploads = new Mongo.Collection('uploads');
if (Meteor.isClient) {
Meteor.subscribe('uploads');
// Helpers for the file listing template
Template.fileList.helpers({
theUploads: function() {
return Uploads.find({}, {
sort: {
"name": 1
}
});
},
// Return number of files uploaded
uploadCount: function() {
return Uploads.find().count();
},
myCallbacks: function() {
return {
finished: function(index, fileInfo, context) {
console.log('This function will execute after each fileupload is finished on the client');
console.log("index ", index);
console.log("fileInfo ", fileInfo);
console.log("context ", context);
}
}
},
someStuff: function() {
// this is data that will be passed to the server with the upload
return {
someData: "hello",
otherData: "goodbye"
}
}
});
Template.fileList.events({
'click .deleteFileButton ': function(event) {
Meteor.call('deleteFile', this._id);
}
})
};
if (Meteor.isServer) {
Meteor.publish("uploads", function() {
console.log("publishing files");
var uploads = Uploads.find({});
console.log("returning " + uploads.fetch().length + " uploads");
return uploads;
});
Meteor.methods({
// Linked to the class deleteFile on the button. Invoked using template 'click' helper
'deleteFile': function(_id) {
console.log("Deleting file with ID " + _id);
check(_id, String);
var upload = Uploads.findOne(_id);
if (upload == null) {
throw new Meteor.Error(404, 'Upload not found'); // maybe some other code
throw new Meteor.Error(500, 'File may have been moved or server has another issue'); // maybe some other code
}
// NOTE: we should also validate here and make sure user is logged in by passing a token through
UploadServer.delete(upload.path);
Uploads.remove(_id);
}
})
Meteor.startup(function() {
UploadServer.init({
// Default upload locations. If you're using it for images, /public/uploads/ can also work
tmpDir: process.env.PWD + '/uploads/tmp',
uploadDir: process.env.PWD + '/uploads/',
checkCreateDirectories: true,
finished: function(fileInfo, formFields) {
console.log("upload finished, fileInfo ", fileInfo);
console.log("upload finished, formFields: ", formFields);
Uploads.insert(fileInfo);
},
cacheTime: 100,
mimeTypes: {
"xml": "application/xml",
"vcf": "text/x-vcard"
},
validateRequest: function(req, res) {
// // to validate, one way to do it would be to form your download urls on the client to pass in some token
// // that we can use to look up the current user by the token
// console.log("token passed in: ", req.query.token);
// // possibly hit the users collection to see if the token is valid by looking at the hashed resume tokens
// var user = Users.findOne({"services.resume.loginTokens":{$elemMatch:{"hashedToken":req.query.token}}});
// console.log("user ", user);
// // if you want to allow the download, return nothing
// // if you want to prevent the download, return a string with the error message, like:
// return "cannot download this file,, sorry";
}
})
});
}