drapcode-utility 1.5.2 → 1.5.3

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.
@@ -12,7 +12,7 @@ interface S3Config {
12
12
  append: boolean;
13
13
  }
14
14
  export declare const createS3Client: (awsConfig: AWSConfig) => S3Client | null;
15
- export declare const fileUploadToS3: (files: any, s3Client: S3Client, s3Config: S3Config, encryption: Encryption, options?: {}) => Promise<{
15
+ export declare const fileUploadToS3: (files: any, s3Client: S3Client, s3Config: S3Config, publicS3Client: S3Client, publicS3Config: S3Config, encryption: Encryption, options?: {}) => Promise<{
16
16
  status: string;
17
17
  data: string;
18
18
  message: string;
@@ -22,11 +22,17 @@ export declare const fileUploadToS3: (files: any, s3Client: S3Client, s3Config:
22
22
  size: any;
23
23
  key: string | undefined;
24
24
  contentType: string;
25
+ smallIcon: string | null;
26
+ mediumIcon: string | null;
27
+ largeIcon: string | null;
25
28
  } | {
26
29
  originalname: any;
27
30
  mimetype: any;
28
31
  size: any;
29
32
  key: string | undefined;
30
33
  contentType: string;
34
+ smallIcon: string | null;
35
+ mediumIcon: string | null;
36
+ largeIcon: string | null;
31
37
  }[] | null>;
32
38
  export {};
@@ -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,10 +74,10 @@ 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, 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
+ var awsConfig, encryptionType, dataKey, accessKeyId, secretAccessKey, region, config, plainTextData, fileOptions, s3Key, _a, _b, _i, index, file, fileSlug, fileObj, fileSlug, fileObj;
80
81
  return __generator(this, function (_c) {
81
82
  switch (_c.label) {
82
83
  case 0:
@@ -111,10 +112,15 @@ var fileUploadToS3 = function (files, s3Client, s3Config, encryption, options) {
111
112
  if (!(_i < _a.length)) return [3 /*break*/, 6];
112
113
  index = _a[_i];
113
114
  file = files[index];
115
+ fileSlug = file.originalname
116
+ .replace(/\.[^/.]+$/, "")
117
+ .replace(/\s+/g, "_")
118
+ .replace(/[^a-zA-Z0-9_]/g, "")
119
+ .toLowerCase();
114
120
  if (s3Config.append) {
115
- s3Config.key = s3Key + "/" + uuid_1.v4() + "/" + file.originalname;
121
+ s3Config.key = s3Key + "/" + uuid_1.v4() + "/" + fileSlug;
116
122
  }
117
- return [4 /*yield*/, processFileUploadToS3(file, s3Client, s3Config, encryption, options, encryption ? true : false)];
123
+ return [4 /*yield*/, processFileUploadToS3(file, s3Client, s3Config, publicS3Client, publicS3Config, encryption, options, encryption ? true : false)];
118
124
  case 4:
119
125
  fileObj = _c.sent();
120
126
  if (fileObj) {
@@ -127,10 +133,15 @@ var fileUploadToS3 = function (files, s3Client, s3Config, encryption, options) {
127
133
  case 6: return [2 /*return*/, fileOptions];
128
134
  case 7:
129
135
  console.log("I have single file");
136
+ fileSlug = files.originalname
137
+ .replace(/\.[^/.]+$/, "")
138
+ .replace(/\s+/g, "_")
139
+ .replace(/[^a-zA-Z0-9_]/g, "")
140
+ .toLowerCase();
130
141
  if (s3Config.append) {
131
- s3Config.key = s3Config.key + "/" + uuid_1.v4() + "/" + files.originalname;
142
+ s3Config.key = s3Config.key + "/" + uuid_1.v4() + "/" + fileSlug;
132
143
  }
133
- return [4 /*yield*/, processFileUploadToS3(files, s3Client, s3Config, encryption, options, encryption ? true : false)];
144
+ return [4 /*yield*/, processFileUploadToS3(files, s3Client, s3Config, publicS3Client, publicS3Config, encryption, options, encryption ? true : false)];
134
145
  case 8:
135
146
  fileObj = _c.sent();
136
147
  return [2 /*return*/, fileObj];
@@ -139,68 +150,232 @@ var fileUploadToS3 = function (files, s3Client, s3Config, encryption, options) {
139
150
  });
140
151
  };
141
152
  exports.fileUploadToS3 = fileUploadToS3;
142
- var processFileUploadToS3 = function (file, s3Client, s3Config, encryption, options, encrypt) {
153
+ var processFileUploadToS3 = function (file, s3Client, s3Config, publicS3Client, publicS3Config, encryption, options, encrypt) {
143
154
  if (options === void 0) { options = {}; }
144
155
  if (encrypt === void 0) { encrypt = false; }
145
156
  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) {
157
+ var contentType, acl, key, bucket, params, encryptedFilePath, parallelUploads3, data, _a, smallIcon, mediumIcon, largeIcon, originalname, mimetype, size, error_1;
158
+ return __generator(this, function (_b) {
159
+ switch (_b.label) {
149
160
  case 0:
150
- console.log("encryption processFileUploadToS3", encryption);
151
- _a.label = 1;
152
- case 1:
153
- _a.trys.push([1, 6, , 7]);
161
+ _b.trys.push([0, 6, , 7]);
154
162
  contentType = mime_types_1.default.lookup(file.originalname) || "";
155
163
  acl = s3Config.acl, key = s3Config.key, bucket = s3Config.bucket;
156
164
  params = null;
157
165
  encryptedFilePath = null;
158
- if (!encrypt) return [3 /*break*/, 3];
166
+ if (!encrypt) return [3 /*break*/, 2];
159
167
  return [4 /*yield*/, encryption_1.cryptFile(file.path, encryption, false)];
168
+ case 1:
169
+ encryptedFilePath = _b.sent();
170
+ params = __assign({ Bucket: bucket, ACL: acl, Key: key, Body: fs_1.default.createReadStream(encryptedFilePath), ContentType: contentType }, options);
171
+ return [3 /*break*/, 3];
160
172
  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
173
  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
- });
174
+ _b.label = 3;
175
+ case 3:
176
+ parallelUploads3 = new lib_storage_1.Upload({ client: s3Client, params: params });
175
177
  return [4 /*yield*/, parallelUploads3.done()];
178
+ case 4:
179
+ data = _b.sent();
180
+ return [4 /*yield*/, handleIconUpload(file.path, contentType, publicS3Client, publicS3Config, key, options)];
176
181
  case 5:
177
- data = _a.sent();
178
- 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
- }
182
+ _a = _b.sent(), smallIcon = _a.smallIcon, mediumIcon = _a.mediumIcon, largeIcon = _a.largeIcon;
183
+ if (encryptedFilePath) {
184
+ fs_1.default.unlinkSync(encryptedFilePath);
188
185
  }
186
+ fs_1.default.unlinkSync(file.path);
189
187
  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];
188
+ return [2 /*return*/, {
189
+ originalname: originalname,
190
+ mimetype: mimetype,
191
+ size: size,
192
+ key: data.Key,
193
+ contentType: contentType,
194
+ smallIcon: smallIcon,
195
+ mediumIcon: mediumIcon,
196
+ largeIcon: largeIcon,
197
+ }];
198
198
  case 6:
199
- error_1 = _a.sent();
200
- console.log("error", error_1);
199
+ error_1 = _b.sent();
200
+ console.error("Error uploading file to S3:", error_1);
201
201
  return [2 /*return*/, null];
202
202
  case 7: return [2 /*return*/];
203
203
  }
204
204
  });
205
205
  });
206
206
  };
207
+ var handleIconUpload = function (filePath, contentType, publicS3Client, publicS3Config, key, options) {
208
+ if (options === void 0) { options = {}; }
209
+ return __awaiter(void 0, void 0, void 0, function () {
210
+ var iconSizes, iconLabels, iconFiles, bucket_1, acl_1, iconUploads, _a, smallKey, mediumKey, largeKey, iconError_1;
211
+ return __generator(this, function (_b) {
212
+ switch (_b.label) {
213
+ case 0:
214
+ iconSizes = [200, 300, 500];
215
+ iconLabels = ["small", "medium", "large"];
216
+ _b.label = 1;
217
+ case 1:
218
+ _b.trys.push([1, 6, , 7]);
219
+ if (!["image/png", "image/jpeg", "image/gif", "application/pdf"].includes(contentType)) return [3 /*break*/, 4];
220
+ return [4 /*yield*/, generateDynamicIcons(filePath, iconSizes)];
221
+ case 2:
222
+ iconFiles = _b.sent();
223
+ bucket_1 = publicS3Config.bucket, acl_1 = publicS3Config.acl;
224
+ iconUploads = iconFiles.map(function (iconFilePath, index) { return __awaiter(void 0, void 0, void 0, function () {
225
+ var iconLabel, iconKey, iconParams, iconUpload, iconData;
226
+ return __generator(this, function (_a) {
227
+ switch (_a.label) {
228
+ case 0:
229
+ iconLabel = iconLabels[index];
230
+ iconKey = key + "/icons/" + uuid_1.v4() + "_" + iconLabel + ".png";
231
+ iconParams = __assign({ Bucket: bucket_1, ACL: acl_1, Key: iconKey, Body: fs_1.default.createReadStream(iconFilePath), ContentType: "image/png" }, options);
232
+ iconUpload = new lib_storage_1.Upload({
233
+ client: publicS3Client,
234
+ params: iconParams,
235
+ });
236
+ return [4 /*yield*/, iconUpload.done()];
237
+ case 1:
238
+ iconData = _a.sent();
239
+ fs_1.default.unlinkSync(iconFilePath);
240
+ return [2 /*return*/, iconData.Key];
241
+ }
242
+ });
243
+ }); });
244
+ return [4 /*yield*/, Promise.all(iconUploads)];
245
+ case 3:
246
+ _a = _b.sent(), smallKey = _a[0], mediumKey = _a[1], largeKey = _a[2];
247
+ return [2 /*return*/, {
248
+ smallIcon: smallKey
249
+ ? "https://" + bucket_1 + ".s3.amazonaws.com/" + smallKey
250
+ : null,
251
+ mediumIcon: mediumKey
252
+ ? "https://" + bucket_1 + ".s3.amazonaws.com/" + mediumKey
253
+ : null,
254
+ largeIcon: largeKey
255
+ ? "https://" + bucket_1 + ".s3.amazonaws.com/" + largeKey
256
+ : null,
257
+ }];
258
+ case 4: return [2 /*return*/, generateStaticIcons(contentType)];
259
+ case 5: return [3 /*break*/, 7];
260
+ case 6:
261
+ iconError_1 = _b.sent();
262
+ console.error("Error generating or uploading icons:", iconError_1);
263
+ return [2 /*return*/, generateStaticIcons(contentType)];
264
+ case 7: return [2 /*return*/];
265
+ }
266
+ });
267
+ });
268
+ };
269
+ var generateDynamicIcons = function (filePath, sizes) {
270
+ return Promise.all(sizes.map(function (size) {
271
+ var outputFilePath = filePath + "_" + size + ".png";
272
+ return new Promise(function (resolve, reject) {
273
+ gm_1.default(filePath)
274
+ .resize(size, size)
275
+ .write(outputFilePath, function (err) {
276
+ if (err) {
277
+ reject(err);
278
+ }
279
+ else {
280
+ resolve(outputFilePath);
281
+ }
282
+ });
283
+ });
284
+ }));
285
+ };
286
+ var generateStaticIcons = function (type) {
287
+ var smallIcon;
288
+ var mediumIcon;
289
+ var largeIcon;
290
+ switch (type) {
291
+ case "image/png":
292
+ case "image/jpeg":
293
+ case "image/gif":
294
+ smallIcon =
295
+ "https://drapcode-static.s3.amazonaws.com/img/placeholder-img.png";
296
+ mediumIcon =
297
+ "https://drapcode-static.s3.amazonaws.com/img/placeholder-img.png";
298
+ largeIcon =
299
+ "https://drapcode-static.s3.amazonaws.com/img/placeholder-img.png";
300
+ break;
301
+ case "application/zip":
302
+ smallIcon = "https://drapcode-static.s3.amazonaws.com/img/zip.png";
303
+ mediumIcon = "https://drapcode-static.s3.amazonaws.com/img/zip.png";
304
+ largeIcon = "https://drapcode-static.s3.amazonaws.com/img/zip.png";
305
+ break;
306
+ case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": //xlsx
307
+ case "application/vnd.ms-excel": //xls
308
+ smallIcon = "https://drapcode-static.s3.amazonaws.com/img/excel.png";
309
+ mediumIcon = "https://drapcode-static.s3.amazonaws.com/img/excel.png";
310
+ largeIcon = "https://drapcode-static.s3.amazonaws.com/img/excel.png ";
311
+ break;
312
+ case "application/pdf":
313
+ smallIcon = "https://drapcode-static.s3.amazonaws.com/img/pdf-file.png";
314
+ mediumIcon = "https://drapcode-static.s3.amazonaws.com/img/pdf-file.png";
315
+ largeIcon = "https://drapcode-static.s3.amazonaws.com/img/pdf-file.png";
316
+ break;
317
+ case "text/csv":
318
+ smallIcon = "https://drapcode-static.s3.amazonaws.com/img/csv.png";
319
+ mediumIcon = "https://drapcode-static.s3.amazonaws.com/img/csv.png";
320
+ largeIcon = "https://drapcode-static.s3.amazonaws.com/img/csv.png";
321
+ break;
322
+ case "application/msword": //doc
323
+ case "application/vnd.openxmlformats-officedocument.wordprocessingml.document": //docx
324
+ smallIcon =
325
+ "https://drapcode-static.s3.amazonaws.com/img/google-docs.png";
326
+ mediumIcon =
327
+ "https://drapcode-static.s3.amazonaws.com/img/google-docs.png";
328
+ largeIcon =
329
+ "https://drapcode-static.s3.amazonaws.com/img/google-docs.png";
330
+ break;
331
+ case "text/plain":
332
+ smallIcon = "https://drapcode-static.s3.amazonaws.com/img/txt.png";
333
+ mediumIcon = "https://drapcode-static.s3.amazonaws.com/img/txt.png";
334
+ largeIcon = "https://drapcode-static.s3.amazonaws.com/img/txt.png";
335
+ break;
336
+ case "application/rtf":
337
+ smallIcon =
338
+ "https://drapcode-static.s3.amazonaws.com/img/rtf-file-symbol.png";
339
+ mediumIcon =
340
+ "https://drapcode-static.s3.amazonaws.com/img/rtf-file-symbol.png";
341
+ largeIcon =
342
+ "https://drapcode-static.s3.amazonaws.com/img/rtf-file-symbol.png";
343
+ break;
344
+ case "text/html":
345
+ smallIcon = "https://drapcode-static.s3.amazonaws.com/img/html.png";
346
+ mediumIcon = "https://drapcode-static.s3.amazonaws.com/img/html.png";
347
+ largeIcon = "https://drapcode-static.s3.amazonaws.com/img/html.png";
348
+ break;
349
+ case "audio/mpeg":
350
+ smallIcon =
351
+ "https://drapcode-static.s3.amazonaws.com/img/placeholder-audio.png";
352
+ mediumIcon =
353
+ "https://drapcode-static.s3.amazonaws.com/img/placeholder-audio.png";
354
+ largeIcon =
355
+ "https://drapcode-static.s3.amazonaws.com/img/placeholder-audio.png";
356
+ break;
357
+ case "video/mpeg": //mpg
358
+ case "video/x-flv": //flv
359
+ case "video/x-msvideo": //avi
360
+ smallIcon = "https://drapcode-static.s3.amazonaws.com/img/video.png";
361
+ mediumIcon = "https://drapcode-static.s3.amazonaws.com/img/video.png";
362
+ largeIcon = "https://drapcode-static.s3.amazonaws.com/img/video.png";
363
+ break;
364
+ case "application/vnd.ms-powerpoint": //ppt
365
+ case "application/vnd.openxmlformats-officedocument.presentationml.presentation": //pptx
366
+ smallIcon = "https://drapcode-static.s3.amazonaws.com/img/powerpoint.png";
367
+ mediumIcon =
368
+ "https://drapcode-static.s3.amazonaws.com/img/powerpoint.png";
369
+ largeIcon = "https://drapcode-static.s3.amazonaws.com/img/powerpoint.png";
370
+ break;
371
+ default:
372
+ smallIcon =
373
+ "https://drapcode-static.s3.amazonaws.com/img/google-docs.png";
374
+ mediumIcon =
375
+ "https://drapcode-static.s3.amazonaws.com/img/google-docs.png";
376
+ largeIcon =
377
+ "https://drapcode-static.s3.amazonaws.com/img/google-docs.png";
378
+ break;
379
+ }
380
+ return { smallIcon: smallIcon, mediumIcon: mediumIcon, largeIcon: largeIcon };
381
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "drapcode-utility",
3
- "version": "1.5.2",
3
+ "version": "1.5.3",
4
4
  "description": "",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",
@@ -31,12 +31,14 @@
31
31
  "@aws-sdk/lib-storage": "^3.499.0",
32
32
  "@types/cookie-session": "^2.0.41",
33
33
  "@types/express": "^4.17.7",
34
+ "@types/gm": "^1.25.4",
34
35
  "@types/voca": "^1.4.2",
35
36
  "axios": "^1.1.2",
36
37
  "drapcode-constant": "^1.4.5",
37
38
  "drapcode-logger": "^1.1.6",
38
39
  "drapcode-redis": "^1.0.5",
39
40
  "express": "^4.17.1",
41
+ "gm": "^1.25.0",
40
42
  "lodash": "^4.17.21",
41
43
  "mime-types": "^2.1.35",
42
44
  "moment": "^2.29.0",