-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
215 lines (181 loc) · 7.93 KB
/
index.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
const cloudinary = require('cloudinary');
const PubNub = require('pubnub')
var uuid = require('uuid-v4');
var cld_settings;
var pubnub_settings;
exports.config = function (cld, pubnub) {
this.pubnub_settings = pubnub;
this.cld_settings = cld;
cloudinary.config(this.cld_settings);
}
exports.getCloudinary = cloudinary;
/**
* The explicit method is used to apply actions to already uploaded assets to Cloudinary
*
* @function explicit
* @param {string} public_id - The identifier of the uploaded asset or the URL of the remote asset.
* @param {string} resource_type - The type of asset. Valid values: image, raw, and video
* @param {string} type - The delivery type of the asset.
* @param {string} eager_transformation - Transformations to create for the uploaded asset.
* @param {function} callback - A callback to run contain all the result details for the action that triggered it
*/
exports.explicit = function (public_id, resource_type, type, eager_transformation, callback) {
eager(this.cld_settings, this.pubnub_settings, public_id, "explicit", resource_type, type, eager_transformation, null, null, callback);
}
/**
* Update one or more of the attributes associated with a specified resource (asset).
*
* @function Update
* @param {string} public_id - The identifier of the uploaded asset or the URL of the remote asset.
* @param {string} resource_type - The type of asset. Valid values: image, raw, and video
* @param {string} type - The delivery type of the asset.
* @param {string} eager_transformation - Transformations to create for the uploaded asset.
* @param {function} callback - A callback to run contain all the result details for the action that triggered it
*/
exports.update = function (public_id, resource_type, type, eager_transformation, callback) {
eager(this.cld_settings, this.pubnub_settings, public_id, "update", resource_type, type, eager_transformation, null, null, callback);
}
/**
* The upload method is used to upload assets to Cloudinary.
*
* @function upload
* @param {string} public_id - The identifier of the uploaded asset or the URL of the remote asset.
* @param {string} resource_type - The type of asset. Valid values: image, raw, and video
* @param {string} type - The delivery type of the asset.
* @param {string} eager_transformation - Transformations to create for the uploaded asset.
* @param {function} callback - A callback to run contain all the result details for the action that triggered it
*/
exports.upload = function (public_id, resource_type, type, eager_transformation, callback) {
eager(this.cld_settings, this.pubnub_settings, public_id, "upload", resource_type, type, eager_transformation, null, null, callback);
}
/**
* The multi method creates either a single animated image (GIF, PNG or WebP), video (MP4 or WebM) or a single PDF from all image assets that have been assigned a specified tag. Each asset is included as a single frame of the resulting animated image/video, or a page of the PDF (sorted alphabetically by their Public ID).
*
* @function multi
* @param {string} tag - The animated GIF or PDF is created from all images with this tag.
* @param {function} callback - A callback to run contain all the result details for the action that triggered it
*/
exports.multi = function (tag, options = {}, callback) {
eager(
this.cld_settings,
this.pubnub_settings,
null,
"multi",
null,
null,
null,
tag,
options,
callback);
}
function eager(
cld_settings,
pubnub_settings,
public_id,
method,
resource_type,
type,
eager_transformation,
tag,
multi_optios = {},
callback) {
var pubnubUrl = "https://ps.pndsn.com/publish/" + pubnub_settings.publishKey + "/" + pubnub_settings.subscribeKey + "/0/channel/0/";
var pubnub = new PubNub(pubnub_settings)
cloudinary.config(cld_settings);
var channel = uuid();
var error = false;
var results;
var pubnubListener = {
status: function (statusEvent) {
if (statusEvent.operation === "PNUnsubscribeOperation") {
pubnub.removeListener(pubnubListener)
pubnub.disconnect();
if (error) {
callback(results)
} else {
callback(null, results)
}
};
if (statusEvent.operation === "PNSubscribeOperation") {
switch (method) {
case "upload":
cloudinary.v2.uploader.upload(public_id, {
eager_notification_url: pubnubUrl.replace("channel", channel),
eager_async: true,
notification_url : pubnubUrl.replace("channel", channel),
resource_type: resource_type,
type: type,
eager: eager_transformation
}, function (err, data) {
if (err) {
error = true;
results = err;
pubnub.unsubscribe({
channels: [channel]
});
}
});
break;
case "multi":
multi_optios.async = true;
multi_optios.notification_url = pubnubUrl.replace("channel", channel);
cloudinary.v2.uploader.multi(tag, options, function (err, data) {
if (err) {
error = true;
results = err;
pubnub.unsubscribe({
channels: [channel]
});
}
});
break;
case "explicit":
cloudinary.v2.uploader.explicit(public_id, {
eager_notification_url: pubnubUrl.replace("channel", channel),
eager_async: true,
resource_type: resource_type,
type: type,
eager: eager_transformation
}, function (err, data) {
if (err) {
error = true;
results = err;
pubnub.unsubscribe({
channels: [channel]
});
}
});
break;
case "update":
var options = eager_transformation;
options.notification_url = pubnubUrl.replace("channel", channel);
options.resource_type = resource_type;
options.type = type;
cloudinary.v2.api.update(public_id, options, function (err, data) {
if (err) {
error = true;
results = err;
pubnub.unsubscribe({
channels: [channel]
});
}
});
break;
}
}
},
message: function (msg) {
if (msg.message.eager && msg.message.eager[0].ignored) {//ignore?
} else {
results = msg.message;
pubnub.unsubscribe({
channels: [channel]
});
}
}
}
pubnub.addListener(pubnubListener);
pubnub.subscribe({
channels: [channel]
});
}