1 | (function() {
|
2 | var PRELOADED_CLOUDINARY_PATH, PreloadedFile, config, utils;
|
3 |
|
4 | utils = require("./utils");
|
5 |
|
6 | config = require("./config");
|
7 |
|
8 | PRELOADED_CLOUDINARY_PATH = /^([^\/]+)\/([^\/]+)\/v(\d+)\/([^#]+)#([^\/]+)$/;
|
9 |
|
10 | PreloadedFile = (function() {
|
11 | function PreloadedFile(file_info) {
|
12 | var matches, public_id_and_format;
|
13 | matches = file_info.match(PRELOADED_CLOUDINARY_PATH);
|
14 | if (!matches) {
|
15 | throw "Invalid preloaded file info";
|
16 | }
|
17 | this.resource_type = matches[1];
|
18 | this.type = matches[2];
|
19 | this.version = matches[3];
|
20 | this.filename = matches[4];
|
21 | this.signature = matches[5];
|
22 | public_id_and_format = this.split_format(this.filename);
|
23 | this.public_id = public_id_and_format[0];
|
24 | this.format = public_id_and_format[1];
|
25 | }
|
26 |
|
27 | PreloadedFile.prototype.is_valid = function() {
|
28 | var expected_signature, public_id;
|
29 | public_id = this.resource_type === "raw" ? this.filename : this.public_id;
|
30 | expected_signature = utils.api_sign_request({
|
31 | public_id: this.public_id,
|
32 | version: this.version
|
33 | }, config().api_secret);
|
34 | return this.signature === expected_signature;
|
35 | };
|
36 |
|
37 | PreloadedFile.prototype.split_format = function(identifier) {
|
38 | var format, last_dot, public_id;
|
39 | last_dot = identifier.lastIndexOf(".");
|
40 | if (last_dot === -1) {
|
41 | return [identifier, null];
|
42 | }
|
43 | public_id = identifier.substr(0, last_dot);
|
44 | format = identifier.substr(last_dot + 1);
|
45 | return [public_id, format];
|
46 | };
|
47 |
|
48 | PreloadedFile.prototype.identifier = function() {
|
49 | return "v" + this.version + "/" + this.filename;
|
50 | };
|
51 |
|
52 | PreloadedFile.prototype.toString = function() {
|
53 | return this.resource_type + "/" + this.type + "/v" + this.version + "/" + this.filename + "#" + this.signature;
|
54 | };
|
55 |
|
56 | PreloadedFile.prototype.toJSON = function() {
|
57 | var key, ref, result, val;
|
58 | result = {};
|
59 | ref = this;
|
60 | for (key in ref) {
|
61 | val = ref[key];
|
62 | if (typeof val !== 'function') {
|
63 | result[key] = val;
|
64 | }
|
65 | }
|
66 | return result;
|
67 | };
|
68 |
|
69 | return PreloadedFile;
|
70 |
|
71 | })();
|
72 |
|
73 | module.exports = PreloadedFile;
|
74 |
|
75 | }).call(this);
|
76 |
|
77 |
|