UNPKG

2.99 kBJavaScriptView Raw
1"use strict";
2
3var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
4
5function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
6
7var PRELOADED_CLOUDINARY_PATH, PreloadedFile, config, utils;
8
9utils = require("./utils");
10
11config = require("./config");
12
13PRELOADED_CLOUDINARY_PATH = /^([^\/]+)\/([^\/]+)\/v(\d+)\/([^#]+)#([^\/]+)$/;
14
15PreloadedFile = function () {
16 function PreloadedFile(file_info) {
17 _classCallCheck(this, PreloadedFile);
18
19 var matches, public_id_and_format;
20 matches = file_info.match(PRELOADED_CLOUDINARY_PATH);
21 if (!matches) {
22 throw "Invalid preloaded file info";
23 }
24 this.resource_type = matches[1];
25 this.type = matches[2];
26 this.version = matches[3];
27 this.filename = matches[4];
28 this.signature = matches[5];
29 public_id_and_format = this.split_format(this.filename);
30 this.public_id = public_id_and_format[0];
31 this.format = public_id_and_format[1];
32 }
33
34 _createClass(PreloadedFile, [{
35 key: "is_valid",
36 value: function is_valid() {
37 var expected_signature, public_id;
38 public_id = this.resource_type === "raw" ? this.filename : this.public_id;
39 expected_signature = utils.api_sign_request({
40 public_id: this.public_id,
41 version: this.version
42 }, config().api_secret);
43 return this.signature === expected_signature;
44 }
45 }, {
46 key: "split_format",
47 value: function split_format(identifier) {
48 var format, last_dot, public_id;
49 last_dot = identifier.lastIndexOf(".");
50 if (last_dot === -1) {
51 return [identifier, null];
52 }
53 public_id = identifier.substr(0, last_dot);
54 format = identifier.substr(last_dot + 1);
55 return [public_id, format];
56 }
57 }, {
58 key: "identifier",
59 value: function identifier() {
60 return "v" + this.version + "/" + this.filename;
61 }
62 }, {
63 key: "toString",
64 value: function toString() {
65 return this.resource_type + "/" + this.type + "/v" + this.version + "/" + this.filename + "#" + this.signature;
66 }
67 }, {
68 key: "toJSON",
69 value: function toJSON() {
70 var key, ref, result, val;
71 result = {};
72 ref = this;
73 for (key in ref) {
74 val = ref[key];
75 if (typeof val !== 'function') {
76 result[key] = val;
77 }
78 }
79 return result;
80 }
81 }]);
82
83 return PreloadedFile;
84}();
85
86module.exports = PreloadedFile;
\No newline at end of file