1 | "use strict";
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4 | return new (P || (P = Promise))(function (resolve, reject) {
|
5 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9 | });
|
10 | };
|
11 | var __generator = (this && this.__generator) || function (thisArg, body) {
|
12 | var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
13 | return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
14 | function verb(n) { return function (v) { return step([n, v]); }; }
|
15 | function step(op) {
|
16 | if (f) throw new TypeError("Generator is already executing.");
|
17 | while (_) try {
|
18 | if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
19 | if (y = 0, t) op = [op[0] & 2, t.value];
|
20 | switch (op[0]) {
|
21 | case 0: case 1: t = op; break;
|
22 | case 4: _.label++; return { value: op[1], done: false };
|
23 | case 5: _.label++; y = op[1]; op = [0]; continue;
|
24 | case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
25 | default:
|
26 | if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
27 | if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
28 | if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
29 | if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
30 | if (t[2]) _.ops.pop();
|
31 | _.trys.pop(); continue;
|
32 | }
|
33 | op = body.call(thisArg, _);
|
34 | } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
35 | if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
36 | }
|
37 | };
|
38 | var __rest = (this && this.__rest) || function (s, e) {
|
39 | var t = {};
|
40 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
41 | t[p] = s[p];
|
42 | if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
43 | for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
44 | if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
45 | t[p[i]] = s[p[i]];
|
46 | }
|
47 | return t;
|
48 | };
|
49 | Object.defineProperty(exports, "__esModule", { value: true });
|
50 | exports.createCloudinaryStorage = exports.CloudinaryStorage = void 0;
|
51 | var CloudinaryStorage = (function () {
|
52 | function CloudinaryStorage(opts) {
|
53 | var _a;
|
54 | if (opts == null || opts.cloudinary == null) {
|
55 | throw new Error('`cloudinary` option required');
|
56 | }
|
57 | this.cloudinary = opts.cloudinary;
|
58 | this.params = (_a = opts.params) !== null && _a !== void 0 ? _a : {};
|
59 | }
|
60 | CloudinaryStorage.prototype._handleFile = function (req, file, callback) {
|
61 | return __awaiter(this, void 0, void 0, function () {
|
62 | var uploadOptions, _a, public_id, otherParams, _b, _c, _d, _i, untypedKey, key, getterOrValue, value, _e, resp, err_1;
|
63 | return __generator(this, function (_f) {
|
64 | switch (_f.label) {
|
65 | case 0:
|
66 | _f.trys.push([0, 11, , 12]);
|
67 | uploadOptions = void 0;
|
68 | if (!(typeof this.params === 'function')) return [3 , 2];
|
69 | return [4 , this.params(req, file)];
|
70 | case 1:
|
71 | uploadOptions = _f.sent();
|
72 | return [3 , 9];
|
73 | case 2:
|
74 | _a = this.params, public_id = _a.public_id, otherParams = __rest(_a, ["public_id"]);
|
75 | _b = {};
|
76 | return [4 , (public_id === null || public_id === void 0 ? void 0 : public_id(req, file))];
|
77 | case 3:
|
78 | uploadOptions = (_b.public_id = _f.sent(), _b);
|
79 | _c = [];
|
80 | for (_d in otherParams)
|
81 | _c.push(_d);
|
82 | _i = 0;
|
83 | _f.label = 4;
|
84 | case 4:
|
85 | if (!(_i < _c.length)) return [3 , 9];
|
86 | untypedKey = _c[_i];
|
87 | key = untypedKey;
|
88 | getterOrValue = otherParams[key];
|
89 | if (!(typeof getterOrValue === 'function')) return [3 , 6];
|
90 | return [4 , getterOrValue(req, file)];
|
91 | case 5:
|
92 | _e = _f.sent();
|
93 | return [3 , 7];
|
94 | case 6:
|
95 | _e = getterOrValue;
|
96 | _f.label = 7;
|
97 | case 7:
|
98 | value = _e;
|
99 | uploadOptions[key] = value;
|
100 | _f.label = 8;
|
101 | case 8:
|
102 | _i++;
|
103 | return [3 , 4];
|
104 | case 9: return [4 , this.upload(uploadOptions, file)];
|
105 | case 10:
|
106 | resp = _f.sent();
|
107 | callback(undefined, {
|
108 | path: resp.secure_url,
|
109 | size: resp.bytes,
|
110 | filename: resp.public_id,
|
111 | });
|
112 | return [3 , 12];
|
113 | case 11:
|
114 | err_1 = _f.sent();
|
115 | callback(err_1);
|
116 | return [3 , 12];
|
117 | case 12: return [2 ];
|
118 | }
|
119 | });
|
120 | });
|
121 | };
|
122 | CloudinaryStorage.prototype._removeFile = function (req, file, callback) {
|
123 | this.cloudinary.uploader.destroy(file.filename, { invalidate: true }, callback);
|
124 | };
|
125 | CloudinaryStorage.prototype.upload = function (opts, file) {
|
126 | var _this = this;
|
127 | return new Promise(function (resolve, reject) {
|
128 | var stream = _this.cloudinary.uploader.upload_stream(opts, function (err, response) {
|
129 | if (err != null)
|
130 | return reject(err);
|
131 | return resolve(response);
|
132 | });
|
133 | file.stream.pipe(stream);
|
134 | });
|
135 | };
|
136 | return CloudinaryStorage;
|
137 | }());
|
138 | exports.CloudinaryStorage = CloudinaryStorage;
|
139 | function createCloudinaryStorage(opts) {
|
140 | return new CloudinaryStorage(opts);
|
141 | }
|
142 | exports.createCloudinaryStorage = createCloudinaryStorage;
|
143 | exports.default = createCloudinaryStorage;
|