/
directive.js
232 lines (195 loc) · 5.67 KB
/
directive.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
/**
* @callback Directive~authorize
*
* The meteor method context is passed on to this function, including
* this.userId
*
* @throws Meteor.Error
*
* @param {{size: Number, type: String, name: String}} file - File to be
* uploaded
* @param {Object} [meta] - Meta information provided by the client.
*
* @returns Boolean Return true to authorize the requested upload.
*/
/**
* @typedef {Object} Directive
*
* @property {Number} maxSize - Maximum size in bytes
* @property {(string, Array.<String>, RegExp, null)} allowedFileTypes - MIME
* types that can be uploaded. If null is passed, then all file types are
* allowed.
*
* @property {Directive~authorize} authorize - Function to determine whether a
* file-upload is authorized or not.
*
* @property {String} [cacheControl] - rfc2616 Cache-Control directive (if
* applicable to the selected storage service)
*
* @property {String} [contentDisposition] - rfc2616 Content-Disposition
* directive. Defaults to "inline; <uploaded file name>"
*
* @property {String}
*/
/**
* @typedef {Object} FileInfo
*
* @property {String} name
* @property {Number} size - File-size in bytes.
* @property {String} type - mime type.
*
*/
/**
* @typedef {Object} UploadInstructions
*
* @property {String} upload - POST URL
* @property {String} download - Download URL
* @property {Array.<{name: String, value: Object}>} postData - POST data to be
* transferred to storage service along with credentials.
*/
/**
* List of installed directives by name.
*
* @type {Object.<string, Directive>}
* @private
*/
Slingshot._directives = {};
/**
* Creates file upload directive that defines a set of rule by which a file may
* be uploaded.
*
* @param {string} name - A unique identifier of the directive.
* @param {Object} service - A storage service to use.
* @param {Directive} options
* @returns {Slingshot.Directive}
*/
Slingshot.createDirective = function (name, service, options) {
if (_.has(Slingshot._directives, name))
throw new Error("Directive '" + name + "' already exists");
var restrictions = Slingshot.getRestrictions(name);
_.defaults(options, restrictions);
return (Slingshot._directives[name] =
new Slingshot.Directive(service, options));
};
/**
* @param {string} name - The unique identifier of the directive to be
* retrieved.
* @returns {Slingshot.Directive}
*/
Slingshot.getDirective = function (name) {
return this._directives[name];
};
/**
* @param {Object} service
* @param {Directive} directive
* @constructor
*/
Slingshot.Directive = function (service, directive) {
check(this, Slingshot.Directive);
//service does not have to be a plain-object, so checking fields individually
check(service.directiveMatch, Object);
check(service.upload, Function);
check(service.maxSize, Match.Optional(Number));
check(service.allowedFileTypes, Match.Optional(matchAllowedFileTypes));
_.defaults(directive, service.directiveDefault);
check(directive, _.defaults({
authorize: Function,
maxSize: Match.OneOf(Number, null),
allowedFileTypes: matchAllowedFileTypes,
cacheControl: Match.Optional(String),
contentDisposition: Match.Optional(Match.OneOf(String, null))
}, service.directiveMatch));
/**
* @method storageService
* @returns {Object}
*/
this.storageService = function () {
return service;
};
/**
* @private
* @property {Directive} _directive
*/
this._directive = directive;
};
_.extend(Slingshot.Directive.prototype, {
/**
* @param {{userId: String}} method
* @param {FileInfo} file
* @param {Object} [meta]
*
* @returns UploadInstructions
*/
getInstructions: function (method, file, meta) {
var instructions = this.storageService().upload(method, _.extend({
contentDisposition: "inline; filename=" + quoteString(file.name, '"')
}, this._directive), file, meta);
check(instructions, {
upload: String,
download: String,
postData: [{
name: String,
value: Match.OneOf(String, Number, null)
}]
});
return instructions;
},
/**
*
* @method requestAuthorization
*
* @throws Meteor.Error
*
* @param {Object} context
* @param {FileInfo} file
* @param {Object} [meta]
*
* @returns {Boolean}
*/
requestAuthorization: function (context, file, meta) {
var validators = Slingshot.Validators,
restrictions = _.pick(this._directive,
['authorize', 'maxSize', 'allowedFileTypes']
);
return validators.checkAll(context, file, meta, restrictions);
}
});
Meteor.methods({
/**
* Requests to perform a file upload.
*
* @param {String} directiveName
* @param {FileInfo} file
* @param {Object} [meta]
*
* @returns {UploadInstructions}
*/
"slingshot/uploadRequest": function (directiveName, file, meta) {
check(directiveName, String);
check(file, {
type: Match.Where(function (type) {
check(type, String);
return /^[^\/]+\/[^\/]+$/.test(type);
}),
name: String,
size: Match.Where(function (size) {
check(size, Number);
return size >= 0;
})
});
check(meta, Match.Optional(Match.OneOf(Object, null)));
var directive = Slingshot.getDirective(directiveName);
if (!directive) {
throw new Meteor.Error("Invalid directive",
"The directive " + directiveName + " does not seem to exist");
}
if (!directive.requestAuthorization(this, file, meta)) {
throw new Meteor.Error("Unauthorized", "You are not allowed to " +
"upload this file");
}
return directive.getInstructions(this, file, meta);
}
});
function quoteString(string, quotes) {
return quotes + string.replace(quotes, '\\' + quotes) + quotes;
}