UNPKG

2.9 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, config, utils;
8
9utils = require("./utils");
10
11config = require("./config");
12
13PRELOADED_CLOUDINARY_PATH = /^([^\/]+)\/([^\/]+)\/v(\d+)\/([^#]+)#([^\/]+)$/;
14
15var PreloadedFile = 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 = PreloadedFile.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;
38 expected_signature = utils.api_sign_request({
39 public_id: this.public_id,
40 version: this.version
41 }, config().api_secret);
42 return this.signature === expected_signature;
43 }
44 }, {
45 key: "identifier",
46 value: function identifier() {
47 return `v${this.version}/${this.filename}`;
48 }
49 }, {
50 key: "toString",
51 value: function toString() {
52 return `${this.resource_type}/${this.type}/v${this.version}/${this.filename}#${this.signature}`;
53 }
54 }, {
55 key: "toJSON",
56 value: function toJSON() {
57 var _this = this;
58
59 var result = {};
60 Object.getOwnPropertyNames(this).forEach(function (key) {
61 var val = _this[key];
62 if (typeof val !== 'function') {
63 result[key] = val;
64 }
65 });
66 return result;
67 }
68 }], [{
69 key: "split_format",
70 value: function split_format(identifier) {
71 var format, last_dot, public_id;
72 last_dot = identifier.lastIndexOf(".");
73 if (last_dot === -1) {
74 return [identifier, null];
75 }
76 public_id = identifier.substr(0, last_dot);
77 format = identifier.substr(last_dot + 1);
78 return [public_id, format];
79 }
80 }]);
81
82 return PreloadedFile;
83}();
84
85module.exports = PreloadedFile;
\No newline at end of file