drapcode-utility 1.2.9 → 1.3.0

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.
@@ -57,6 +57,7 @@ var mime_types_1 = __importDefault(require("mime-types"));
57
57
  var lib_storage_1 = require("@aws-sdk/lib-storage");
58
58
  var encryption_1 = require("../encryption");
59
59
  var encryption_2 = require("../encryption");
60
+ var uuid_1 = require("uuid");
60
61
  var createS3Client = function (awsConfig) {
61
62
  if (!awsConfig) {
62
63
  return null;
@@ -111,7 +112,7 @@ var fileUploadToS3 = function (files, s3Client, s3Config, encryption, options) {
111
112
  index = _a[_i];
112
113
  file = files[index];
113
114
  if (s3Config.append) {
114
- s3Config.key = s3Key + "/" + file.originalname;
115
+ s3Config.key = s3Key + "/" + uuid_1.v4() + "/" + file.originalname;
115
116
  }
116
117
  return [4 /*yield*/, processFileUploadToS3(file, s3Client, s3Config, encryption, options, encryption ? true : false)];
117
118
  case 4:
@@ -127,7 +128,7 @@ var fileUploadToS3 = function (files, s3Client, s3Config, encryption, options) {
127
128
  case 7:
128
129
  console.log("I have single file");
129
130
  if (s3Config.append) {
130
- s3Config.key = s3Config.key + "/" + files.originalname;
131
+ s3Config.key = s3Config.key + "/" + uuid_1.v4() + "/" + files.originalname;
131
132
  }
132
133
  return [4 /*yield*/, processFileUploadToS3(files, s3Client, s3Config, encryption, options, encryption ? true : false)];
133
134
  case 8:
@@ -142,33 +143,49 @@ var processFileUploadToS3 = function (file, s3Client, s3Config, encryption, opti
142
143
  if (options === void 0) { options = {}; }
143
144
  if (encrypt === void 0) { encrypt = false; }
144
145
  return __awaiter(void 0, void 0, void 0, function () {
145
- var contentType, acl, key, bucket, params, encryptedBody, parallelUploads3, data, originalname, mimetype, size, finalObject, error_1;
146
+ var contentType, acl, key, bucket, params, encryptedFilePath, parallelUploads3, data, originalname, mimetype, size, finalObject, error_1;
146
147
  return __generator(this, function (_a) {
147
148
  switch (_a.label) {
148
149
  case 0:
149
- _a.trys.push([0, 5, , 6]);
150
+ console.log("encryption processFileUploadToS3", encryption);
151
+ _a.label = 1;
152
+ case 1:
153
+ _a.trys.push([1, 6, , 7]);
150
154
  contentType = mime_types_1.default.lookup(file.originalname) || "";
151
155
  acl = s3Config.acl, key = s3Config.key, bucket = s3Config.bucket;
152
156
  params = null;
153
- if (!encrypt) return [3 /*break*/, 2];
157
+ encryptedFilePath = null;
158
+ if (!encrypt) return [3 /*break*/, 3];
154
159
  return [4 /*yield*/, encryption_1.cryptFile(file.path, encryption, false)];
155
- case 1:
156
- encryptedBody = _a.sent();
160
+ case 2:
161
+ encryptedFilePath = _a.sent();
162
+ console.log("encryptedFilePath", encryptedFilePath);
157
163
  params = __assign({ Bucket: bucket, ACL: acl, Key: key,
158
164
  //@ts-ignore
159
- Body: fs_1.default.createReadStream(encryptedBody), ContentType: contentType }, options);
160
- return [3 /*break*/, 3];
161
- case 2:
162
- params = __assign({ Bucket: bucket, ACL: acl, Key: key, Body: fs_1.default.createReadStream(file.path), ContentType: contentType }, options);
163
- _a.label = 3;
165
+ Body: fs_1.default.createReadStream(encryptedFilePath), ContentType: contentType }, options);
166
+ return [3 /*break*/, 4];
164
167
  case 3:
168
+ 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:
165
171
  parallelUploads3 = new lib_storage_1.Upload({
166
172
  client: s3Client,
167
173
  params: params,
168
174
  });
169
175
  return [4 /*yield*/, parallelUploads3.done()];
170
- case 4:
176
+ case 5:
171
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
+ }
188
+ }
172
189
  originalname = file.originalname, mimetype = file.mimetype, size = file.size;
173
190
  finalObject = {
174
191
  originalname: originalname,
@@ -178,11 +195,11 @@ var processFileUploadToS3 = function (file, s3Client, s3Config, encryption, opti
178
195
  contentType: contentType,
179
196
  };
180
197
  return [2 /*return*/, finalObject];
181
- case 5:
198
+ case 6:
182
199
  error_1 = _a.sent();
183
200
  console.log("error", error_1);
184
201
  return [2 /*return*/, null];
185
- case 6: return [2 /*return*/];
202
+ case 7: return [2 /*return*/];
186
203
  }
187
204
  });
188
205
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "drapcode-utility",
3
- "version": "1.2.9",
3
+ "version": "1.3.0",
4
4
  "description": "",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",
@@ -21,6 +21,7 @@
21
21
  "devDependencies": {
22
22
  "@types/lodash": "^4.14.179",
23
23
  "@types/mime-types": "^2.1.4",
24
+ "@types/uuid": "^9.0.8",
24
25
  "del-cli": "^5.0.0",
25
26
  "typescript": "^4.0.2"
26
27
  },
@@ -40,6 +41,7 @@
40
41
  "mime-types": "^2.1.35",
41
42
  "moment": "^2.29.0",
42
43
  "stringify-object": "^3.3.0",
44
+ "uuid": "^8.3.2",
43
45
  "voca": "^1.4.0"
44
46
  }
45
47
  }