UNPKG

23.8 kBJavaScriptView Raw
1'use strict';
2
3var config = require("./config");
4var ensureOption = require('./utils/ensureOption').defaults(config());
5
6var https = /^http:/.test(config().upload_prefix) ? require('http') : require('https');
7var utils = require("./utils");
8
9var extend = utils.extend,
10 includes = utils.includes,
11 isString = utils.isString,
12 only = utils.only,
13 ensurePresenceOf = utils.ensurePresenceOf;
14
15
16var querystring = require("querystring");
17
18var Q = require('q');
19
20var api = module.exports;
21
22function call_api(method, uri, params, callback, options) {
23 var handle_response = void 0,
24 query_params = void 0;
25 ensurePresenceOf({ method, uri });
26 var deferred = Q.defer();
27 var cloudinary = ensureOption(options, "upload_prefix", "https://api.cloudinary.com");
28 var cloud_name = ensureOption(options, "cloud_name");
29 var api_key = ensureOption(options, "api_key");
30 var api_secret = ensureOption(options, "api_secret");
31
32 method = method.toUpperCase();
33 var api_url = [cloudinary, "v1_1", cloud_name].concat(uri).join("/");
34 var content_type = 'application/x-www-form-urlencoded';
35 if (options['content_type'] === 'json') {
36 query_params = JSON.stringify(params);
37 content_type = 'application/json';
38 } else {
39 query_params = querystring.stringify(params);
40 }
41 if (method === "GET") {
42 api_url += "?" + query_params;
43 }
44 var request_options = require('url').parse(api_url);
45 request_options = extend(request_options, {
46 method: method,
47 headers: {
48 'Content-Type': content_type,
49 'User-Agent': utils.getUserAgent()
50 },
51 auth: api_key + ":" + api_secret
52 });
53 if (options.agent != null) {
54 request_options.agent = options.agent;
55 }
56 if (method !== "GET") {
57 request_options.headers['Content-Length'] = Buffer.byteLength(query_params);
58 }
59 handle_response = function handle_response(res) {
60 if (includes([200, 400, 401, 403, 404, 409, 420, 500], res.statusCode)) {
61 var buffer = "";
62 var error = false;
63 res.on("data", function (d) {
64 return buffer += d;
65 });
66 res.on("end", function () {
67 var e = void 0,
68 result = void 0;
69 if (error) {
70 return;
71 }
72 try {
73 result = JSON.parse(buffer);
74 } catch (error1) {
75 e = error1;
76 result = {
77 error: {
78 message: "Server return invalid JSON response. Status Code " + res.statusCode
79 }
80 };
81 }
82 if (result["error"]) {
83 result["error"]["http_code"] = res.statusCode;
84 } else {
85 result["rate_limit_allowed"] = parseInt(res.headers["x-featureratelimit-limit"]);
86 result["rate_limit_reset_at"] = new Date(res.headers["x-featureratelimit-reset"]);
87 result["rate_limit_remaining"] = parseInt(res.headers["x-featureratelimit-remaining"]);
88 }
89 if (result.error) {
90 deferred.reject(result);
91 } else {
92 deferred.resolve(result);
93 }
94 return typeof callback === "function" ? callback(result) : void 0;
95 });
96 return res.on("error", function (e) {
97 error = true;
98 var err_obj = {
99 error: {
100 message: e,
101 http_code: res.statusCode
102 }
103 };
104 deferred.reject(err_obj.error);
105 return typeof callback === "function" ? callback(err_obj) : void 0;
106 });
107 } else {
108 var err_obj = {
109 error: {
110 message: "Server returned unexpected status code - " + res.statusCode,
111 http_code: res.statusCode
112 }
113 };
114 deferred.reject(err_obj.error);
115 return typeof callback === "function" ? callback(err_obj) : void 0;
116 }
117 };
118 var request = https.request(request_options, handle_response);
119 request.on("error", function (e) {
120 return typeof callback === "function" ? callback({
121 error: e
122 }) : void 0;
123 });
124 request.setTimeout(ensureOption(options, "timeout", 60000));
125 if (method !== "GET") {
126 request.write(query_params);
127 }
128 request.end();
129 return deferred.promise;
130}
131
132function transformationString(transformation) {
133 if (isString(transformation)) {
134 return transformation;
135 } else {
136 return utils.generate_transformation_string(extend({}, transformation));
137 }
138}
139
140function deleteResourcesParams(options) {
141 var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
142
143 return extend(params, only(options, "keep_original", "invalidate", "next_cursor", "transformations"));
144}
145
146exports.ping = function ping(callback) {
147 var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
148
149 return call_api("get", ["ping"], {}, callback, options);
150};
151
152exports.usage = function usage(callback) {
153 var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
154
155 return call_api("get", ["usage"], {}, callback, options);
156};
157
158exports.resource_types = function resource_types(callback) {
159 var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
160
161 return call_api("get", ["resources"], {}, callback, options);
162};
163
164exports.resources = function resources(callback) {
165 var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
166
167 var ref = void 0,
168 resource_type = void 0,
169 type = void 0,
170 uri = void 0;
171 resource_type = (ref = options["resource_type"]) != null ? ref : "image";
172 type = options["type"];
173 uri = ["resources", resource_type];
174 if (type != null) {
175 uri.push(type);
176 }
177 if (options.start_at != null && Object.prototype.toString.call(options.start_at) === '[object Date]') {
178 options.start_at = options.start_at.toUTCString();
179 }
180 return call_api("get", uri, only(options, "next_cursor", "max_results", "prefix", "tags", "context", "direction", "moderations", "start_at"), callback, options);
181};
182
183exports.resources_by_tag = function resources_by_tag(tag, callback) {
184 var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
185
186 var ref = void 0,
187 resource_type = void 0,
188 uri = void 0;
189 resource_type = (ref = options["resource_type"]) != null ? ref : "image";
190 uri = ["resources", resource_type, "tags", tag];
191 return call_api("get", uri, only(options, "next_cursor", "max_results", "tags", "context", "direction", "moderations"), callback, options);
192};
193
194exports.resources_by_context = function resources_by_context(key, value, callback) {
195 var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
196
197 var params = void 0,
198 ref = void 0,
199 resource_type = void 0,
200 uri = void 0;
201 resource_type = (ref = options["resource_type"]) != null ? ref : "image";
202 uri = ["resources", resource_type, "context"];
203 params = only(options, "next_cursor", "max_results", "tags", "context", "direction", "moderations");
204 params.key = key;
205 if (value != null) {
206 params.value = value;
207 }
208 return call_api("get", uri, params, callback, options);
209};
210
211exports.resources_by_moderation = function resources_by_moderation(kind, status, callback) {
212 var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
213
214 var ref = void 0,
215 resource_type = void 0,
216 uri = void 0;
217 resource_type = (ref = options["resource_type"]) != null ? ref : "image";
218 uri = ["resources", resource_type, "moderations", kind, status];
219 return call_api("get", uri, only(options, "next_cursor", "max_results", "tags", "context", "direction", "moderations"), callback, options);
220};
221
222exports.resources_by_ids = function resources_by_ids(public_ids, callback) {
223 var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
224
225 var params = void 0,
226 ref = void 0,
227 ref1 = void 0,
228 resource_type = void 0,
229 type = void 0,
230 uri = void 0;
231 resource_type = (ref = options["resource_type"]) != null ? ref : "image";
232 type = (ref1 = options["type"]) != null ? ref1 : "upload";
233 uri = ["resources", resource_type, type];
234 params = only(options, "tags", "context", "moderations");
235 params["public_ids[]"] = public_ids;
236 return call_api("get", uri, params, callback, options);
237};
238
239exports.resource = function resource(public_id, callback) {
240 var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
241
242 var ref = void 0,
243 ref1 = void 0,
244 resource_type = void 0,
245 type = void 0,
246 uri = void 0;
247 resource_type = (ref = options["resource_type"]) != null ? ref : "image";
248 type = (ref1 = options["type"]) != null ? ref1 : "upload";
249 uri = ["resources", resource_type, type, public_id];
250 return call_api("get", uri, only(options, "exif", "colors", "faces", "image_metadata", "pages", "phash", "coordinates", "max_results"), callback, options);
251};
252
253exports.restore = function restore(public_ids, callback) {
254 var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
255
256 var ref = void 0,
257 ref1 = void 0,
258 resource_type = void 0,
259 type = void 0,
260 uri = void 0;
261 resource_type = (ref = options["resource_type"]) != null ? ref : "image";
262 type = (ref1 = options["type"]) != null ? ref1 : "upload";
263 uri = ["resources", resource_type, type, "restore"];
264 return call_api("post", uri, {
265 public_ids: public_ids
266 }, callback, options);
267};
268
269exports.update = function update(public_id, callback) {
270 var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
271
272 var params = void 0,
273 ref = void 0,
274 ref1 = void 0,
275 resource_type = void 0,
276 type = void 0,
277 uri = void 0;
278 resource_type = (ref = options["resource_type"]) != null ? ref : "image";
279 type = (ref1 = options["type"]) != null ? ref1 : "upload";
280 uri = ["resources", resource_type, type, public_id];
281 params = utils.updateable_resource_params(options);
282 if (options.moderation_status != null) {
283 params.moderation_status = options.moderation_status;
284 }
285 return call_api("post", uri, params, callback, options);
286};
287
288exports.delete_resources = function delete_resources(public_ids, callback) {
289 var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
290
291 var ref = void 0,
292 ref1 = void 0,
293 resource_type = void 0,
294 type = void 0,
295 uri = void 0;
296 resource_type = (ref = options["resource_type"]) != null ? ref : "image";
297 type = (ref1 = options["type"]) != null ? ref1 : "upload";
298 uri = ["resources", resource_type, type];
299 return call_api("delete", uri, deleteResourcesParams(options, {
300 "public_ids[]": public_ids
301 }), callback, options);
302};
303
304exports.delete_resources_by_prefix = function delete_resources_by_prefix(prefix, callback) {
305 var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
306
307 var ref = void 0,
308 ref1 = void 0,
309 resource_type = void 0,
310 type = void 0,
311 uri = void 0;
312 resource_type = (ref = options["resource_type"]) != null ? ref : "image";
313 type = (ref1 = options["type"]) != null ? ref1 : "upload";
314 uri = ["resources", resource_type, type];
315 return call_api("delete", uri, deleteResourcesParams(options, {
316 prefix: prefix
317 }), callback, options);
318};
319
320exports.delete_resources_by_tag = function delete_resources_by_tag(tag, callback) {
321 var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
322
323 var ref = void 0,
324 resource_type = void 0,
325 uri = void 0;
326 resource_type = (ref = options["resource_type"]) != null ? ref : "image";
327 uri = ["resources", resource_type, "tags", tag];
328 return call_api("delete", uri, deleteResourcesParams(options), callback, options);
329};
330
331exports.delete_all_resources = function delete_all_resources(callback) {
332 var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
333
334 var ref = void 0,
335 ref1 = void 0,
336 resource_type = void 0,
337 type = void 0,
338 uri = void 0;
339
340 resource_type = (ref = options["resource_type"]) != null ? ref : "image";
341 type = (ref1 = options["type"]) != null ? ref1 : "upload";
342 uri = ["resources", resource_type, type];
343 return call_api("delete", uri, deleteResourcesParams(options, {
344 all: true
345 }), callback, options);
346};
347
348exports.delete_derived_resources = function delete_derived_resources(derived_resource_ids, callback) {
349 var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
350
351 var uri = void 0;
352 uri = ["derived_resources"];
353 return call_api("delete", uri, {
354 "derived_resource_ids[]": derived_resource_ids
355 }, callback, options);
356};
357
358exports.delete_derived_by_transformation = function delete_derived_by_transformation(public_ids, transformations, callback) {
359 var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
360
361 var params = void 0,
362 resource_type = void 0,
363 type = void 0,
364 uri = void 0;
365 resource_type = options["resource_type"] || "image";
366 type = options["type"] || "upload";
367 uri = "resources/" + resource_type + "/" + type;
368 params = extend({
369 "public_ids[]": public_ids
370 }, only(options, "invalidate"));
371 params["keep_original"] = true;
372 params["transformations"] = utils.build_eager(transformations);
373 return call_api("delete", uri, params, callback, options);
374};
375
376exports.tags = function tags(callback) {
377 var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
378
379 var ref = void 0,
380 resource_type = void 0,
381 uri = void 0;
382 resource_type = (ref = options["resource_type"]) != null ? ref : "image";
383 uri = ["tags", resource_type];
384 return call_api("get", uri, only(options, "next_cursor", "max_results", "prefix"), callback, options);
385};
386
387exports.transformations = function transformations(callback) {
388 var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
389
390 return call_api("get", ["transformations"], only(options, "next_cursor", "max_results", "named"), callback, options);
391};
392
393exports.transformation = function transformation(transformation, callback) {
394 var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
395
396 var uri = void 0;
397 uri = ["transformations", transformationString(transformation)];
398 return call_api("get", uri, only(options, "next_cursor", "max_results"), callback, options);
399};
400
401exports.delete_transformation = function delete_transformation(transformation, callback) {
402 var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
403
404 var uri = void 0;
405 uri = ["transformations", transformationString(transformation)];
406 return call_api("delete", uri, {}, callback, options);
407};
408
409exports.update_transformation = function update_transformation(transformation, updates, callback) {
410 var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
411
412 var params = void 0,
413 uri = void 0;
414 uri = ["transformations", transformationString(transformation)];
415 params = only(updates, "allowed_for_strict");
416 if (updates.unsafe_update != null) {
417 params.unsafe_update = transformationString(updates.unsafe_update);
418 }
419 return call_api("put", uri, params, callback, options);
420};
421
422exports.create_transformation = function create_transformation(name, definition, callback) {
423 var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
424
425 var uri = void 0;
426 uri = ["transformations", name];
427 return call_api("post", uri, {
428 transformation: transformationString(definition)
429 }, callback, options);
430};
431
432exports.upload_presets = function upload_presets(callback) {
433 var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
434
435 return call_api("get", ["upload_presets"], only(options, "next_cursor", "max_results"), callback, options);
436};
437
438exports.upload_preset = function upload_preset(name, callback) {
439 var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
440
441 var uri = void 0;
442 uri = ["upload_presets", name];
443 return call_api("get", uri, {}, callback, options);
444};
445
446exports.delete_upload_preset = function delete_upload_preset(name, callback) {
447 var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
448
449 var uri = void 0;
450 uri = ["upload_presets", name];
451 return call_api("delete", uri, {}, callback, options);
452};
453
454exports.update_upload_preset = function update_upload_preset(name, callback) {
455 var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
456
457 var params = void 0,
458 uri = void 0;
459 uri = ["upload_presets", name];
460 params = utils.merge(utils.clear_blank(utils.build_upload_params(options)), only(options, "unsigned", "disallow_public_id"));
461 return call_api("put", uri, params, callback, options);
462};
463
464exports.create_upload_preset = function create_upload_preset(callback) {
465 var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
466
467 var params = void 0,
468 uri = void 0;
469 uri = ["upload_presets"];
470 params = utils.merge(utils.clear_blank(utils.build_upload_params(options)), only(options, "name", "unsigned", "disallow_public_id"));
471 return call_api("post", uri, params, callback, options);
472};
473
474exports.root_folders = function root_folders(callback) {
475 var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
476
477 var uri = void 0;
478 uri = ["folders"];
479 return call_api("get", uri, {}, callback, options);
480};
481
482exports.sub_folders = function sub_folders(path, callback) {
483 var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
484
485 var uri = void 0;
486 uri = ["folders", path];
487 return call_api("get", uri, {}, callback, options);
488};
489
490exports.upload_mappings = function upload_mappings(callback) {
491 var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
492
493 var params = void 0;
494 params = only(options, "next_cursor", "max_results");
495 return call_api("get", "upload_mappings", params, callback, options);
496};
497
498exports.upload_mapping = function upload_mapping(name, callback) {
499 var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
500
501 if (name == null) {
502 name = null;
503 }
504 return call_api("get", 'upload_mappings', {
505 folder: name
506 }, callback, options);
507};
508
509exports.delete_upload_mapping = function delete_upload_mapping(name, callback) {
510 var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
511
512 return call_api("delete", 'upload_mappings', {
513 folder: name
514 }, callback, options);
515};
516
517exports.update_upload_mapping = function update_upload_mapping(name, callback) {
518 var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
519
520 var params = void 0;
521 params = only(options, "template");
522 params["folder"] = name;
523 return call_api("put", 'upload_mappings', params, callback, options);
524};
525
526exports.create_upload_mapping = function create_upload_mapping(name, callback) {
527 var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
528
529 var params = void 0;
530 params = only(options, "template");
531 params["folder"] = name;
532 return call_api("post", 'upload_mappings', params, callback, options);
533};
534
535function publishResource(byKey, value, callback) {
536 var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
537
538 var params = void 0,
539 ref = void 0,
540 resource_type = void 0,
541 uri = void 0;
542 params = only(options, "type", "invalidate", "overwrite");
543 params[byKey] = value;
544 resource_type = (ref = options.resource_type) != null ? ref : "image";
545 uri = ["resources", resource_type, "publish_resources"];
546 options = extend({
547 resource_type: resource_type
548 }, options);
549 return call_api("post", uri, params, callback, options);
550}
551
552exports.publish_by_prefix = function publish_by_prefix(prefix, callback) {
553 var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
554
555 return publishResource("prefix", prefix, callback, options);
556};
557
558exports.publish_by_tag = function publish_by_tag(tag, callback) {
559 var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
560
561 return publishResource("tag", tag, callback, options);
562};
563
564exports.publish_by_ids = function publish_by_ids(public_ids, callback) {
565 var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
566
567 return publishResource("public_ids", public_ids, callback, options);
568};
569
570exports.list_streaming_profiles = function list_streaming_profiles(callback) {
571 var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
572
573 return call_api("get", "streaming_profiles", {}, callback, options);
574};
575
576exports.get_streaming_profile = function get_streaming_profile(name, callback) {
577 var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
578
579 return call_api("get", "streaming_profiles/" + name, {}, callback, options);
580};
581
582exports.delete_streaming_profile = function delete_streaming_profile(name, callback) {
583 var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
584
585 return call_api("delete", "streaming_profiles/" + name, {}, callback, options);
586};
587
588exports.update_streaming_profile = function update_streaming_profile(name, callback) {
589 var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
590
591 var params = void 0;
592 params = utils.build_streaming_profiles_param(options);
593 return call_api("put", "streaming_profiles/" + name, params, callback, options);
594};
595
596exports.create_streaming_profile = function create_streaming_profile(name, callback) {
597 var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
598
599 var params = void 0;
600 params = utils.build_streaming_profiles_param(options);
601 params["name"] = name;
602 return call_api("post", 'streaming_profiles', params, callback, options);
603};
604
605function updateResourcesAccessMode(access_mode, by_key, value, callback) {
606 var options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
607
608 var params = void 0,
609 ref = void 0,
610 ref1 = void 0,
611 resource_type = void 0,
612 type = void 0;
613 resource_type = (ref = options.resource_type) != null ? ref : "image";
614 type = (ref1 = options.type) != null ? ref1 : "upload";
615 params = {
616 access_mode: access_mode
617 };
618 params[by_key] = value;
619 return call_api("post", "resources/" + resource_type + "/" + type + "/update_access_mode", params, callback, options);
620}
621
622exports.search = function search(params, callback) {
623 var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
624
625 options['content_type'] = 'json';
626 return call_api("post", "resources/search", params, callback, options);
627};
628
629exports.update_resources_access_mode_by_prefix = function update_resources_access_mode_by_prefix(access_mode, prefix, callback) {
630 var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
631
632 return updateResourcesAccessMode(access_mode, "prefix", prefix, callback, options);
633};
634
635exports.update_resources_access_mode_by_tag = function update_resources_access_mode_by_tag(access_mode, tag, callback) {
636 var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
637
638 return updateResourcesAccessMode(access_mode, "tag", tag, callback, options);
639};
640
641exports.update_resources_access_mode_by_ids = function update_resources_access_mode_by_ids(access_mode, ids, callback) {
642 var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
643
644 return updateResourcesAccessMode(access_mode, "public_ids[]", ids, callback, options);
645};
\No newline at end of file