This repository has been archived by the owner on Feb 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathserver.js
86 lines (68 loc) · 2.36 KB
/
server.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
import Cloudinary from 'cloudinary';
Slingshot.Cloudinary = {
directiveMatch: {
CloudinaryCloudName: String,
CloudinaryKey: String,
CloudinarySecret: String,
CloudinaryPreset: String,
key: Match.OneOf(String, Function),
tags: Match.Optional(Match.OneOf(String, [String])),
folder: Match.Optional(String),
},
directiveDefault: Object.assign({}, {
CloudinarySecret: Meteor.settings.CloudinarySecret,
CloudinaryKey: Meteor.settings.CloudinaryKey,
CloudinaryCloudName: Meteor.settings.CloudinaryCloudName,
CloudinaryPreset: Meteor.settings.CloudinaryPreset,
}),
isImage(mime) {
return _.contains(['image/jpeg','image/jpg', 'image/png', 'image/svg', 'application/pdf'], mime);
},
isVideo(type) {
return _.contains(
['video/mp4'],
type
);
},
resourceType(type) {
return this.isImage(type)
? 'image'
: this.isVideo(type)
? 'video'
: 'raw';
},
upload: function upload(method, directive, file) {
const { CloudinaryCloudName } = directive;
const publicId = directive.key();
// Cloudinary's node lib supplies most of what we need.
const cloudinarySign = this.cloudinarySign(publicId, directive, file);
const postData = _.map(cloudinarySign.hidden_fields,
(value, name) => { return { value, name }; }
);
const type = this.resourceType(file.type);
const retVal = {
upload: cloudinarySign.form_attrs.action,
download: `http://res.cloudinary.com/${CloudinaryCloudName}/${type}/upload/${publicId}`,
postData,
};
return retVal;
},
cloudinarySign: function sign(publicId, directive, file) {
const options = _.extend({
public_id: publicId,
upload_preset: directive.CloudinaryPreset,
api_key: directive.CloudinaryKey,
api_secret: directive.CloudinarySecret,
cloud_name: directive.CloudinaryCloudName,
// TODO make isImage more robust, add isVideo, and allow
// raw uploads. Probably better if isImage and isVideo
// isn't in this lib.
resource_type: this.resourceType(file.type),
});
// Assign optional cloudinary options
if (_.has(directive, 'tags')) options.tags = directive.tags;
if (_.has(directive, 'folder')) options.folder = directive.folder;
const signature = Cloudinary.uploader.direct_upload('', options);
return signature;
},
};