drapcode-utility 1.5.2 → 1.5.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -25,7 +25,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
25
25
  function verb(n) { return function (v) { return step([n, v]); }; }
26
26
  function step(op) {
27
27
  if (f) throw new TypeError("Generator is already executing.");
28
- while (_) try {
28
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
29
  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;
30
30
  if (y = 0, t) op = [op[0] & 2, t.value];
31
31
  switch (op[0]) {
@@ -58,6 +58,7 @@ var lib_storage_1 = require("@aws-sdk/lib-storage");
58
58
  var encryption_1 = require("../encryption");
59
59
  var encryption_2 = require("../encryption");
60
60
  var uuid_1 = require("uuid");
61
+ var gm_1 = __importDefault(require("gm"));
61
62
  var createS3Client = function (awsConfig) {
62
63
  if (!awsConfig) {
63
64
  return null;
@@ -73,12 +74,12 @@ var createS3Client = function (awsConfig) {
73
74
  return s3client;
74
75
  };
75
76
  exports.createS3Client = createS3Client;
76
- var fileUploadToS3 = function (files, s3Client, s3Config, encryption, options) {
77
+ var fileUploadToS3 = function (files, s3Client, s3Config, publicS3Client, publicS3Config, isGenerateIcons, encryption, options) {
77
78
  if (options === void 0) { options = {}; }
78
79
  return __awaiter(void 0, void 0, void 0, function () {
79
- var awsConfig, encryptionType, dataKey, accessKeyId, secretAccessKey, region, config, plainTextData, fileOptions, s3Key, _a, _b, _i, index, file, fileObj, fileObj;
80
- return __generator(this, function (_c) {
81
- switch (_c.label) {
80
+ var awsConfig, encryptionType, dataKey, accessKeyId, secretAccessKey, region, config, plainTextData, fileOptions, s3Key, _a, _b, _c, _i, index, file, fileSlug, fileObj, fileSlug, fileObj;
81
+ return __generator(this, function (_d) {
82
+ switch (_d.label) {
82
83
  case 0:
83
84
  if (!encryption) return [3 /*break*/, 2];
84
85
  awsConfig = encryption.awsConfig, encryptionType = encryption.encryptionType, dataKey = encryption.dataKey;
@@ -89,118 +90,302 @@ var fileUploadToS3 = function (files, s3Client, s3Config, encryption, options) {
89
90
  accessKeyId: accessKeyId,
90
91
  secretAccessKey: secretAccessKey,
91
92
  };
92
- return [4 /*yield*/, encryption_2.processKMSDecryption(config, dataKey, {})];
93
+ return [4 /*yield*/, (0, encryption_2.processKMSDecryption)(config, dataKey, {})];
93
94
  case 1:
94
- plainTextData = _c.sent();
95
+ plainTextData = _d.sent();
95
96
  if (plainTextData.status === "FAILED") {
96
97
  return [2 /*return*/, plainTextData];
97
98
  }
98
99
  encryption.dataKey = plainTextData.data;
99
- _c.label = 2;
100
+ _d.label = 2;
100
101
  case 2:
101
102
  if (!Array.isArray(files)) return [3 /*break*/, 7];
102
103
  console.log("I have multiple file");
103
104
  fileOptions = [];
104
105
  s3Key = s3Config.key;
105
- _a = [];
106
- for (_b in files)
107
- _a.push(_b);
106
+ _a = files;
107
+ _b = [];
108
+ for (_c in _a)
109
+ _b.push(_c);
108
110
  _i = 0;
109
- _c.label = 3;
111
+ _d.label = 3;
110
112
  case 3:
111
- if (!(_i < _a.length)) return [3 /*break*/, 6];
112
- index = _a[_i];
113
+ if (!(_i < _b.length)) return [3 /*break*/, 6];
114
+ _c = _b[_i];
115
+ if (!(_c in _a)) return [3 /*break*/, 5];
116
+ index = _c;
113
117
  file = files[index];
118
+ fileSlug = file.originalname
119
+ .replace(/\.[^/.]+$/, "")
120
+ .replace(/\s+/g, "_")
121
+ .replace(/[^a-zA-Z0-9_]/g, "")
122
+ .toLowerCase();
114
123
  if (s3Config.append) {
115
- s3Config.key = s3Key + "/" + uuid_1.v4() + "/" + file.originalname;
124
+ s3Config.key = "".concat(s3Key, "/").concat((0, uuid_1.v4)(), "/").concat(fileSlug);
116
125
  }
117
- return [4 /*yield*/, processFileUploadToS3(file, s3Client, s3Config, encryption, options, encryption ? true : false)];
126
+ return [4 /*yield*/, processFileUploadToS3(file, s3Client, s3Config, publicS3Client, publicS3Config, isGenerateIcons, encryption, options, encryption ? true : false)];
118
127
  case 4:
119
- fileObj = _c.sent();
128
+ fileObj = _d.sent();
120
129
  if (fileObj) {
121
130
  fileOptions.push(fileObj);
122
131
  }
123
- _c.label = 5;
132
+ _d.label = 5;
124
133
  case 5:
125
134
  _i++;
126
135
  return [3 /*break*/, 3];
127
136
  case 6: return [2 /*return*/, fileOptions];
128
137
  case 7:
129
138
  console.log("I have single file");
139
+ fileSlug = files.originalname
140
+ .replace(/\.[^/.]+$/, "")
141
+ .replace(/\s+/g, "_")
142
+ .replace(/[^a-zA-Z0-9_]/g, "")
143
+ .toLowerCase();
130
144
  if (s3Config.append) {
131
- s3Config.key = s3Config.key + "/" + uuid_1.v4() + "/" + files.originalname;
145
+ s3Config.key = "".concat(s3Config.key, "/").concat((0, uuid_1.v4)(), "/").concat(fileSlug);
132
146
  }
133
- return [4 /*yield*/, processFileUploadToS3(files, s3Client, s3Config, encryption, options, encryption ? true : false)];
147
+ return [4 /*yield*/, processFileUploadToS3(files, s3Client, s3Config, publicS3Client, publicS3Config, isGenerateIcons, encryption, options, encryption ? true : false)];
134
148
  case 8:
135
- fileObj = _c.sent();
149
+ fileObj = _d.sent();
136
150
  return [2 /*return*/, fileObj];
137
151
  }
138
152
  });
139
153
  });
140
154
  };
141
155
  exports.fileUploadToS3 = fileUploadToS3;
142
- var processFileUploadToS3 = function (file, s3Client, s3Config, encryption, options, encrypt) {
156
+ var processFileUploadToS3 = function (file, s3Client, s3Config, publicS3Client, publicS3Config, isGenerateIcons, encryption, options, encrypt) {
143
157
  if (options === void 0) { options = {}; }
144
158
  if (encrypt === void 0) { encrypt = false; }
145
159
  return __awaiter(void 0, void 0, void 0, function () {
146
- var contentType, acl, key, bucket, params, encryptedFilePath, parallelUploads3, data, originalname, mimetype, size, finalObject, error_1;
147
- return __generator(this, function (_a) {
148
- switch (_a.label) {
160
+ var contentType, acl, key, bucket, params, encryptedFilePath, parallelUploads3, data, _a, smallIcon, mediumIcon, largeIcon, originalname, mimetype, size, error_1;
161
+ return __generator(this, function (_b) {
162
+ switch (_b.label) {
149
163
  case 0:
150
- console.log("encryption processFileUploadToS3", encryption);
151
- _a.label = 1;
152
- case 1:
153
- _a.trys.push([1, 6, , 7]);
164
+ _b.trys.push([0, 6, , 7]);
154
165
  contentType = mime_types_1.default.lookup(file.originalname) || "";
155
166
  acl = s3Config.acl, key = s3Config.key, bucket = s3Config.bucket;
156
167
  params = null;
157
168
  encryptedFilePath = null;
158
- if (!encrypt) return [3 /*break*/, 3];
159
- return [4 /*yield*/, encryption_1.cryptFile(file.path, encryption, false)];
169
+ if (!encrypt) return [3 /*break*/, 2];
170
+ return [4 /*yield*/, (0, encryption_1.cryptFile)(file.path, encryption, false)];
171
+ case 1:
172
+ encryptedFilePath = _b.sent();
173
+ params = __assign({ Bucket: bucket, ACL: acl, Key: key, Body: fs_1.default.createReadStream(encryptedFilePath), ContentType: contentType }, options);
174
+ return [3 /*break*/, 3];
160
175
  case 2:
161
- encryptedFilePath = _a.sent();
162
- console.log("encryptedFilePath", encryptedFilePath);
163
- params = __assign({ Bucket: bucket, ACL: acl, Key: key,
164
- //@ts-ignore
165
- Body: fs_1.default.createReadStream(encryptedFilePath), ContentType: contentType }, options);
166
- return [3 /*break*/, 4];
167
- case 3:
168
176
  params = __assign({ Bucket: bucket, ACL: acl, Key: key, Body: fs_1.default.createReadStream(file.path), ContentType: contentType }, options);
169
- _a.label = 4;
170
- case 4:
171
- parallelUploads3 = new lib_storage_1.Upload({
172
- client: s3Client,
173
- params: params,
174
- });
177
+ _b.label = 3;
178
+ case 3:
179
+ parallelUploads3 = new lib_storage_1.Upload({ client: s3Client, params: params });
175
180
  return [4 /*yield*/, parallelUploads3.done()];
181
+ case 4:
182
+ data = _b.sent();
183
+ return [4 /*yield*/, handleIconUpload(file, contentType, publicS3Client, publicS3Config, isGenerateIcons, key, options)];
176
184
  case 5:
177
- data = _a.sent();
185
+ _a = _b.sent(), smallIcon = _a.smallIcon, mediumIcon = _a.mediumIcon, largeIcon = _a.largeIcon;
178
186
  if (data) {
179
- try {
180
- fs_1.default.unlinkSync(file.path);
181
- if (encryptedFilePath) {
182
- fs_1.default.unlinkSync(encryptedFilePath);
183
- }
184
- }
185
- catch (unlinkError) {
186
- console.error("Error deleting local file:", unlinkError);
187
+ if (encryptedFilePath) {
188
+ fs_1.default.unlinkSync(encryptedFilePath);
187
189
  }
190
+ fs_1.default.unlinkSync(file.path);
188
191
  }
189
192
  originalname = file.originalname, mimetype = file.mimetype, size = file.size;
190
- finalObject = {
191
- originalname: originalname,
192
- mimetype: mimetype,
193
- size: size,
194
- key: data.Key,
195
- contentType: contentType,
196
- };
197
- return [2 /*return*/, finalObject];
193
+ return [2 /*return*/, {
194
+ originalname: originalname,
195
+ mimetype: mimetype,
196
+ size: size,
197
+ key: data.Key,
198
+ contentType: contentType,
199
+ smallIcon: smallIcon,
200
+ mediumIcon: mediumIcon,
201
+ largeIcon: largeIcon,
202
+ }];
198
203
  case 6:
199
- error_1 = _a.sent();
200
- console.log("error", error_1);
204
+ error_1 = _b.sent();
205
+ console.error("Error uploading file to S3:", error_1);
201
206
  return [2 /*return*/, null];
202
207
  case 7: return [2 /*return*/];
203
208
  }
204
209
  });
205
210
  });
206
211
  };
212
+ var handleIconUpload = function (file, contentType, publicS3Client, publicS3Config, isGenerateIcons, key, options) {
213
+ if (options === void 0) { options = {}; }
214
+ return __awaiter(void 0, void 0, void 0, function () {
215
+ var filePath, fileSize, iconSizes, iconLabels, iconFiles, bucket_1, acl_1, iconUploads, _a, smallKey, mediumKey, largeKey, iconError_1;
216
+ return __generator(this, function (_b) {
217
+ switch (_b.label) {
218
+ case 0:
219
+ filePath = file.path, fileSize = file.size;
220
+ iconSizes = [200, 300, 500];
221
+ iconLabels = ["small", "medium", "large"];
222
+ _b.label = 1;
223
+ case 1:
224
+ _b.trys.push([1, 6, , 7]);
225
+ if (!(isGenerateIcons &&
226
+ fileSize < 157286400 && //150mb
227
+ ["image/png", "image/jpeg", "image/gif", "application/pdf"].includes(contentType))) return [3 /*break*/, 4];
228
+ return [4 /*yield*/, generateDynamicIcons(filePath, iconSizes, contentType)];
229
+ case 2:
230
+ iconFiles = _b.sent();
231
+ bucket_1 = publicS3Config.bucket, acl_1 = publicS3Config.acl;
232
+ iconUploads = iconFiles.map(function (iconFilePath, index) { return __awaiter(void 0, void 0, void 0, function () {
233
+ var iconLabel, iconKey, iconParams, iconUpload, iconData;
234
+ return __generator(this, function (_a) {
235
+ switch (_a.label) {
236
+ case 0:
237
+ iconLabel = iconLabels[index];
238
+ iconKey = "".concat(key, "/icons/").concat((0, uuid_1.v4)(), "_").concat(iconLabel, ".png");
239
+ iconParams = __assign({ Bucket: bucket_1, ACL: acl_1, Key: iconKey, Body: fs_1.default.createReadStream(iconFilePath), ContentType: "image/png" }, options);
240
+ iconUpload = new lib_storage_1.Upload({
241
+ client: publicS3Client,
242
+ params: iconParams,
243
+ });
244
+ return [4 /*yield*/, iconUpload.done()];
245
+ case 1:
246
+ iconData = _a.sent();
247
+ fs_1.default.unlinkSync(iconFilePath);
248
+ return [2 /*return*/, iconData.Key];
249
+ }
250
+ });
251
+ }); });
252
+ return [4 /*yield*/, Promise.all(iconUploads)];
253
+ case 3:
254
+ _a = _b.sent(), smallKey = _a[0], mediumKey = _a[1], largeKey = _a[2];
255
+ return [2 /*return*/, {
256
+ smallIcon: smallKey
257
+ ? "https://".concat(bucket_1, ".s3.amazonaws.com/").concat(smallKey)
258
+ : null,
259
+ mediumIcon: mediumKey
260
+ ? "https://".concat(bucket_1, ".s3.amazonaws.com/").concat(mediumKey)
261
+ : null,
262
+ largeIcon: largeKey
263
+ ? "https://".concat(bucket_1, ".s3.amazonaws.com/").concat(largeKey)
264
+ : null,
265
+ }];
266
+ case 4: return [2 /*return*/, generateStaticIcons(contentType)];
267
+ case 5: return [3 /*break*/, 7];
268
+ case 6:
269
+ iconError_1 = _b.sent();
270
+ console.error("Error generating or uploading icons:", iconError_1);
271
+ return [2 /*return*/, generateStaticIcons(contentType)];
272
+ case 7: return [2 /*return*/];
273
+ }
274
+ });
275
+ });
276
+ };
277
+ var generateDynamicIcons = function (filePath, sizes, contentType) {
278
+ return Promise.all(sizes.map(function (size) {
279
+ var outputFilePath = "".concat(filePath, "_").concat(size, ".png");
280
+ return new Promise(function (resolve, reject) {
281
+ var gmInstance = (0, gm_1.default)(filePath);
282
+ if (contentType === "application/pdf") {
283
+ gmInstance = gmInstance.selectFrame(0);
284
+ }
285
+ gmInstance.resize(size, size).write(outputFilePath, function (err) {
286
+ if (err) {
287
+ reject(err);
288
+ }
289
+ else {
290
+ resolve(outputFilePath);
291
+ }
292
+ });
293
+ });
294
+ }));
295
+ };
296
+ var generateStaticIcons = function (type) {
297
+ var smallIcon;
298
+ var mediumIcon;
299
+ var largeIcon;
300
+ switch (type) {
301
+ case "image/png":
302
+ case "image/jpeg":
303
+ case "image/gif":
304
+ smallIcon =
305
+ "https://drapcode-static.s3.amazonaws.com/img/placeholder-img.png";
306
+ mediumIcon =
307
+ "https://drapcode-static.s3.amazonaws.com/img/placeholder-img.png";
308
+ largeIcon =
309
+ "https://drapcode-static.s3.amazonaws.com/img/placeholder-img.png";
310
+ break;
311
+ case "application/zip":
312
+ smallIcon = "https://drapcode-static.s3.amazonaws.com/img/zip.png";
313
+ mediumIcon = "https://drapcode-static.s3.amazonaws.com/img/zip.png";
314
+ largeIcon = "https://drapcode-static.s3.amazonaws.com/img/zip.png";
315
+ break;
316
+ case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": //xlsx
317
+ case "application/vnd.ms-excel": //xls
318
+ smallIcon = "https://drapcode-static.s3.amazonaws.com/img/excel.png";
319
+ mediumIcon = "https://drapcode-static.s3.amazonaws.com/img/excel.png";
320
+ largeIcon = "https://drapcode-static.s3.amazonaws.com/img/excel.png ";
321
+ break;
322
+ case "application/pdf":
323
+ smallIcon = "https://drapcode-static.s3.amazonaws.com/img/pdf-file.png";
324
+ mediumIcon = "https://drapcode-static.s3.amazonaws.com/img/pdf-file.png";
325
+ largeIcon = "https://drapcode-static.s3.amazonaws.com/img/pdf-file.png";
326
+ break;
327
+ case "text/csv":
328
+ smallIcon = "https://drapcode-static.s3.amazonaws.com/img/csv.png";
329
+ mediumIcon = "https://drapcode-static.s3.amazonaws.com/img/csv.png";
330
+ largeIcon = "https://drapcode-static.s3.amazonaws.com/img/csv.png";
331
+ break;
332
+ case "application/msword": //doc
333
+ case "application/vnd.openxmlformats-officedocument.wordprocessingml.document": //docx
334
+ smallIcon =
335
+ "https://drapcode-static.s3.amazonaws.com/img/google-docs.png";
336
+ mediumIcon =
337
+ "https://drapcode-static.s3.amazonaws.com/img/google-docs.png";
338
+ largeIcon =
339
+ "https://drapcode-static.s3.amazonaws.com/img/google-docs.png";
340
+ break;
341
+ case "text/plain":
342
+ smallIcon = "https://drapcode-static.s3.amazonaws.com/img/txt.png";
343
+ mediumIcon = "https://drapcode-static.s3.amazonaws.com/img/txt.png";
344
+ largeIcon = "https://drapcode-static.s3.amazonaws.com/img/txt.png";
345
+ break;
346
+ case "application/rtf":
347
+ smallIcon =
348
+ "https://drapcode-static.s3.amazonaws.com/img/rtf-file-symbol.png";
349
+ mediumIcon =
350
+ "https://drapcode-static.s3.amazonaws.com/img/rtf-file-symbol.png";
351
+ largeIcon =
352
+ "https://drapcode-static.s3.amazonaws.com/img/rtf-file-symbol.png";
353
+ break;
354
+ case "text/html":
355
+ smallIcon = "https://drapcode-static.s3.amazonaws.com/img/html.png";
356
+ mediumIcon = "https://drapcode-static.s3.amazonaws.com/img/html.png";
357
+ largeIcon = "https://drapcode-static.s3.amazonaws.com/img/html.png";
358
+ break;
359
+ case "audio/mpeg":
360
+ smallIcon =
361
+ "https://drapcode-static.s3.amazonaws.com/img/placeholder-audio.png";
362
+ mediumIcon =
363
+ "https://drapcode-static.s3.amazonaws.com/img/placeholder-audio.png";
364
+ largeIcon =
365
+ "https://drapcode-static.s3.amazonaws.com/img/placeholder-audio.png";
366
+ break;
367
+ case "video/mpeg": //mpg
368
+ case "video/x-flv": //flv
369
+ case "video/x-msvideo": //avi
370
+ smallIcon = "https://drapcode-static.s3.amazonaws.com/img/video.png";
371
+ mediumIcon = "https://drapcode-static.s3.amazonaws.com/img/video.png";
372
+ largeIcon = "https://drapcode-static.s3.amazonaws.com/img/video.png";
373
+ break;
374
+ case "application/vnd.ms-powerpoint": //ppt
375
+ case "application/vnd.openxmlformats-officedocument.presentationml.presentation": //pptx
376
+ smallIcon = "https://drapcode-static.s3.amazonaws.com/img/powerpoint.png";
377
+ mediumIcon =
378
+ "https://drapcode-static.s3.amazonaws.com/img/powerpoint.png";
379
+ largeIcon = "https://drapcode-static.s3.amazonaws.com/img/powerpoint.png";
380
+ break;
381
+ default:
382
+ smallIcon =
383
+ "https://drapcode-static.s3.amazonaws.com/img/google-docs.png";
384
+ mediumIcon =
385
+ "https://drapcode-static.s3.amazonaws.com/img/google-docs.png";
386
+ largeIcon =
387
+ "https://drapcode-static.s3.amazonaws.com/img/google-docs.png";
388
+ break;
389
+ }
390
+ return { smallIcon: smallIcon, mediumIcon: mediumIcon, largeIcon: largeIcon };
391
+ };
@@ -14,7 +14,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
14
14
  function verb(n) { return function (v) { return step([n, v]); }; }
15
15
  function step(op) {
16
16
  if (f) throw new TypeError("Generator is already executing.");
17
- while (_) try {
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
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
19
  if (y = 0, t) op = [op[0] & 2, t.value];
20
20
  switch (op[0]) {
@@ -56,8 +56,8 @@ var generateToken = function (projectId) { return __awaiter(void 0, void 0, void
56
56
  randomIndex = Math.floor(Math.random() * characters.length);
57
57
  token += characters.charAt(randomIndex);
58
58
  }
59
- token = token + "." + timestamp;
60
- return [4 /*yield*/, drapcode_redis_1.redis_get_method(uniqueSessionId)];
59
+ token = "".concat(token, ".").concat(timestamp);
60
+ return [4 /*yield*/, (0, drapcode_redis_1.redis_get_method)(uniqueSessionId)];
61
61
  case 2:
62
62
  data = _b.sent();
63
63
  if (!data || !data[projectId] || !data[projectId].length) {
@@ -1,6 +1,6 @@
1
1
  export declare const validateString: (str: string) => false | "String should not contain special characters" | "String should not contain any number";
2
2
  export declare const camelize: (str: string) => string;
3
- export declare const clearSpaceAndReformat: (text: string, separator?: string | undefined) => string;
3
+ export declare const clearSpaceAndReformat: (text: string, separator?: string) => string;
4
4
  export declare const isEmpty: (obj: any) => boolean;
5
5
  export declare const isEmptyObject: (object: any) => boolean;
6
6
  export declare const isObject: (item: any) => any;