UNPKG

3.02 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 = void 0,
8 config = void 0,
9 utils = void 0;
10
11utils = require("./utils");
12
13config = require("./config");
14
15PRELOADED_CLOUDINARY_PATH = /^([^\/]+)\/([^\/]+)\/v(\d+)\/([^#]+)#([^\/]+)$/;
16
17var PreloadedFile = function () {
18 function PreloadedFile(file_info) {
19 _classCallCheck(this, PreloadedFile);
20
21 var matches = void 0,
22 public_id_and_format = void 0;
23 matches = file_info.match(PRELOADED_CLOUDINARY_PATH);
24 if (!matches) {
25 throw "Invalid preloaded file info";
26 }
27 this.resource_type = matches[1];
28 this.type = matches[2];
29 this.version = matches[3];
30 this.filename = matches[4];
31 this.signature = matches[5];
32 public_id_and_format = PreloadedFile.split_format(this.filename);
33 this.public_id = public_id_and_format[0];
34 this.format = public_id_and_format[1];
35 }
36
37 _createClass(PreloadedFile, [{
38 key: "is_valid",
39 value: function is_valid() {
40 var expected_signature = void 0;
41 expected_signature = utils.api_sign_request({
42 public_id: this.public_id,
43 version: this.version
44 }, config().api_secret);
45 return this.signature === expected_signature;
46 }
47 }, {
48 key: "identifier",
49 value: function identifier() {
50 return `v${this.version}/${this.filename}`;
51 }
52 }, {
53 key: "toString",
54 value: function toString() {
55 return `${this.resource_type}/${this.type}/v${this.version}/${this.filename}#${this.signature}`;
56 }
57 }, {
58 key: "toJSON",
59 value: function toJSON() {
60 var _this = this;
61
62 var result = {};
63 Object.getOwnPropertyNames(this).forEach(function (key) {
64 var val = _this[key];
65 if (typeof val !== 'function') {
66 result[key] = val;
67 }
68 });
69 return result;
70 }
71 }], [{
72 key: "split_format",
73 value: function split_format(identifier) {
74 var format = void 0,
75 last_dot = void 0,
76 public_id = void 0;
77 last_dot = identifier.lastIndexOf(".");
78 if (last_dot === -1) {
79 return [identifier, null];
80 }
81 public_id = identifier.substr(0, last_dot);
82 format = identifier.substr(last_dot + 1);
83 return [public_id, format];
84 }
85 }]);
86
87 return PreloadedFile;
88}();
89
90module.exports = PreloadedFile;
\No newline at end of file