drapcode-utility 1.1.7 → 1.1.8
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.
- package/build/encryption/file.d.ts +4 -0
- package/build/encryption/file.js +134 -0
- package/build/encryption/index.d.ts +5 -0
- package/build/encryption/index.js +31 -1
- package/build/index.d.ts +1 -0
- package/build/index.js +1 -0
- package/build/utils/s3-util.d.ts +28 -0
- package/build/utils/s3-util.js +173 -0
- package/package.json +5 -1
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { Encryption } from "./model";
|
|
2
|
+
export declare const processFileEncryptionDecryption: (data: string, encryption: Encryption, decrypt: boolean) => Promise<string>;
|
|
3
|
+
export declare const encryptFile: (filePath: string, key: string) => Promise<string>;
|
|
4
|
+
export declare const decryptFile: (encryptedFilePath: string, key: string) => Promise<string>;
|
|
@@ -0,0 +1,134 @@
|
|
|
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 (g && (g = 0, op[0] && (_ = 0)), _) 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 __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
|
+
};
|
|
41
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
+
exports.decryptFile = exports.encryptFile = exports.processFileEncryptionDecryption = void 0;
|
|
43
|
+
var fs_1 = __importDefault(require("fs"));
|
|
44
|
+
var crypto_1 = __importDefault(require("crypto"));
|
|
45
|
+
var defaultAlgorithm = "aes-256-cbc";
|
|
46
|
+
var processFileEncryptionDecryption = function (data, encryption, decrypt) { return __awaiter(void 0, void 0, void 0, function () {
|
|
47
|
+
var encryptionType, dataKey, _a, result, _b;
|
|
48
|
+
return __generator(this, function (_c) {
|
|
49
|
+
switch (_c.label) {
|
|
50
|
+
case 0:
|
|
51
|
+
encryptionType = encryption.encryptionType, dataKey = encryption.dataKey;
|
|
52
|
+
if (!data)
|
|
53
|
+
return [2 /*return*/, data];
|
|
54
|
+
_a = encryptionType;
|
|
55
|
+
switch (_a) {
|
|
56
|
+
case "KMS": return [3 /*break*/, 1];
|
|
57
|
+
}
|
|
58
|
+
return [3 /*break*/, 6];
|
|
59
|
+
case 1:
|
|
60
|
+
if (!decrypt) return [3 /*break*/, 3];
|
|
61
|
+
return [4 /*yield*/, (0, exports.decryptFile)(data, dataKey)];
|
|
62
|
+
case 2:
|
|
63
|
+
_b = _c.sent();
|
|
64
|
+
return [3 /*break*/, 5];
|
|
65
|
+
case 3: return [4 /*yield*/, (0, exports.encryptFile)(data, dataKey)];
|
|
66
|
+
case 4:
|
|
67
|
+
_b = _c.sent();
|
|
68
|
+
_c.label = 5;
|
|
69
|
+
case 5:
|
|
70
|
+
result = _b;
|
|
71
|
+
return [2 /*return*/, result];
|
|
72
|
+
case 6: return [2 /*return*/, data];
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
}); };
|
|
76
|
+
exports.processFileEncryptionDecryption = processFileEncryptionDecryption;
|
|
77
|
+
var encryptFile = function (filePath, key) { return __awaiter(void 0, void 0, void 0, function () {
|
|
78
|
+
var iv, keyBuffer, fileContent, cipher, encryptedDataBuffer, encryptedFilePath, error_1;
|
|
79
|
+
return __generator(this, function (_a) {
|
|
80
|
+
switch (_a.label) {
|
|
81
|
+
case 0:
|
|
82
|
+
_a.trys.push([0, 3, , 4]);
|
|
83
|
+
iv = Buffer.from("i4mboZDwaNEC38YCzi77lw==", "base64");
|
|
84
|
+
keyBuffer = Buffer.from(key, "base64");
|
|
85
|
+
return [4 /*yield*/, fs_1.default.promises.readFile(filePath)];
|
|
86
|
+
case 1:
|
|
87
|
+
fileContent = _a.sent();
|
|
88
|
+
cipher = crypto_1.default.createCipheriv(defaultAlgorithm, keyBuffer, iv);
|
|
89
|
+
encryptedDataBuffer = cipher.update(fileContent);
|
|
90
|
+
encryptedDataBuffer = Buffer.concat([encryptedDataBuffer, cipher.final()]);
|
|
91
|
+
encryptedFilePath = filePath + ".enc";
|
|
92
|
+
return [4 /*yield*/, fs_1.default.promises.writeFile(encryptedFilePath, encryptedDataBuffer)];
|
|
93
|
+
case 2:
|
|
94
|
+
_a.sent();
|
|
95
|
+
console.log("File encrypted successfully.");
|
|
96
|
+
return [2 /*return*/, encryptedFilePath];
|
|
97
|
+
case 3:
|
|
98
|
+
error_1 = _a.sent();
|
|
99
|
+
console.log("\n Error: ", error_1);
|
|
100
|
+
throw error_1;
|
|
101
|
+
case 4: return [2 /*return*/];
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
}); };
|
|
105
|
+
exports.encryptFile = encryptFile;
|
|
106
|
+
var decryptFile = function (encryptedFilePath, key) { return __awaiter(void 0, void 0, void 0, function () {
|
|
107
|
+
var iv, keyBuffer, encryptedData, decipher, decryptedBuffer, decryptedFilePath, error_2;
|
|
108
|
+
return __generator(this, function (_a) {
|
|
109
|
+
switch (_a.label) {
|
|
110
|
+
case 0:
|
|
111
|
+
_a.trys.push([0, 3, , 4]);
|
|
112
|
+
iv = Buffer.from("i4mboZDwaNEC38YCzi77lw==", "base64");
|
|
113
|
+
keyBuffer = Buffer.from(key, "base64");
|
|
114
|
+
return [4 /*yield*/, fs_1.default.promises.readFile(encryptedFilePath)];
|
|
115
|
+
case 1:
|
|
116
|
+
encryptedData = _a.sent();
|
|
117
|
+
decipher = crypto_1.default.createDecipheriv(defaultAlgorithm, keyBuffer, iv);
|
|
118
|
+
decryptedBuffer = decipher.update(encryptedData);
|
|
119
|
+
decryptedBuffer = Buffer.concat([decryptedBuffer, decipher.final()]);
|
|
120
|
+
decryptedFilePath = "".concat(encryptedFilePath.slice(0, -4), ".dec");
|
|
121
|
+
return [4 /*yield*/, fs_1.default.promises.writeFile(decryptedFilePath, decryptedBuffer)];
|
|
122
|
+
case 2:
|
|
123
|
+
_a.sent();
|
|
124
|
+
console.log("File decrypted successfully.");
|
|
125
|
+
return [2 /*return*/, decryptedFilePath];
|
|
126
|
+
case 3:
|
|
127
|
+
error_2 = _a.sent();
|
|
128
|
+
console.log("Error:", error_2);
|
|
129
|
+
throw error_2;
|
|
130
|
+
case 4: return [2 /*return*/];
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
}); };
|
|
134
|
+
exports.decryptFile = decryptFile;
|
|
@@ -46,3 +46,8 @@ export declare const drapcodeEncryptDecrypt: (data: string, encrypt: boolean) =>
|
|
|
46
46
|
data: string;
|
|
47
47
|
message: string;
|
|
48
48
|
}>;
|
|
49
|
+
export declare const cryptFile: (filePath: any, encryption: Encryption, decrypt: boolean) => Promise<string | {
|
|
50
|
+
status: string;
|
|
51
|
+
data: string;
|
|
52
|
+
message: string;
|
|
53
|
+
}>;
|
|
@@ -36,8 +36,9 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
36
36
|
}
|
|
37
37
|
};
|
|
38
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
exports.drapcodeEncryptDecrypt = exports.decryptDataWithKMS = exports.encryptDataWithKMS = exports.createKMSDataKey = exports.processDataEncryptionDecryption = exports.processItemEncryptDecrypt = exports.crypt = void 0;
|
|
39
|
+
exports.cryptFile = exports.drapcodeEncryptDecrypt = exports.decryptDataWithKMS = exports.encryptDataWithKMS = exports.createKMSDataKey = exports.processDataEncryptionDecryption = exports.processItemEncryptDecrypt = exports.crypt = void 0;
|
|
40
40
|
var crypt_1 = require("./crypt");
|
|
41
|
+
var file_1 = require("./file");
|
|
41
42
|
var KMS_1 = require("./KMS");
|
|
42
43
|
var crypt = function (data, fields, encryption, decrypt) { return __awaiter(void 0, void 0, void 0, function () {
|
|
43
44
|
var _a, accessKeyId, secretAccessKey, region, config, plainTextData, promises;
|
|
@@ -264,3 +265,32 @@ var drapcodeEncryptDecrypt = function (data, encrypt) { return __awaiter(void 0,
|
|
|
264
265
|
});
|
|
265
266
|
}); };
|
|
266
267
|
exports.drapcodeEncryptDecrypt = drapcodeEncryptDecrypt;
|
|
268
|
+
var cryptFile = function (filePath, encryption, decrypt) { return __awaiter(void 0, void 0, void 0, function () {
|
|
269
|
+
var awsConfig, encryptionType, dataKey, accessKeyId, secretAccessKey, region, config, plainTextData, data;
|
|
270
|
+
return __generator(this, function (_a) {
|
|
271
|
+
switch (_a.label) {
|
|
272
|
+
case 0:
|
|
273
|
+
awsConfig = encryption.awsConfig, encryptionType = encryption.encryptionType, dataKey = encryption.dataKey;
|
|
274
|
+
if (!(encryptionType === "KMS")) return [3 /*break*/, 2];
|
|
275
|
+
accessKeyId = awsConfig.accessKeyId, secretAccessKey = awsConfig.secretAccessKey, region = awsConfig.region;
|
|
276
|
+
config = {
|
|
277
|
+
region: region,
|
|
278
|
+
accessKeyId: accessKeyId,
|
|
279
|
+
secretAccessKey: secretAccessKey,
|
|
280
|
+
};
|
|
281
|
+
return [4 /*yield*/, (0, KMS_1.processKMSDecryption)(config, dataKey, {})];
|
|
282
|
+
case 1:
|
|
283
|
+
plainTextData = _a.sent();
|
|
284
|
+
if (plainTextData.status === "FAILED") {
|
|
285
|
+
return [2 /*return*/, plainTextData];
|
|
286
|
+
}
|
|
287
|
+
encryption.dataKey = plainTextData.data;
|
|
288
|
+
_a.label = 2;
|
|
289
|
+
case 2: return [4 /*yield*/, (0, file_1.processFileEncryptionDecryption)(filePath, encryption, decrypt)];
|
|
290
|
+
case 3:
|
|
291
|
+
data = _a.sent();
|
|
292
|
+
return [2 /*return*/, data];
|
|
293
|
+
}
|
|
294
|
+
});
|
|
295
|
+
}); };
|
|
296
|
+
exports.cryptFile = cryptFile;
|
package/build/index.d.ts
CHANGED
|
@@ -13,5 +13,6 @@ export * from "./utils/util";
|
|
|
13
13
|
export * from "./utils/uuid-generator";
|
|
14
14
|
export * from "./utils/check-error";
|
|
15
15
|
export * from "./utils/prepare-query";
|
|
16
|
+
export * from "./utils/s3-util";
|
|
16
17
|
export * from "./encryption/index";
|
|
17
18
|
export * from "./format-fields/index";
|
package/build/index.js
CHANGED
|
@@ -29,5 +29,6 @@ __exportStar(require("./utils/util"), exports);
|
|
|
29
29
|
__exportStar(require("./utils/uuid-generator"), exports);
|
|
30
30
|
__exportStar(require("./utils/check-error"), exports);
|
|
31
31
|
__exportStar(require("./utils/prepare-query"), exports);
|
|
32
|
+
__exportStar(require("./utils/s3-util"), exports);
|
|
32
33
|
__exportStar(require("./encryption/index"), exports);
|
|
33
34
|
__exportStar(require("./format-fields/index"), exports);
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { S3Client, ObjectCannedACL } from "@aws-sdk/client-s3";
|
|
2
|
+
import { Encryption } from "../encryption/model";
|
|
3
|
+
interface AWSConfig {
|
|
4
|
+
region: string;
|
|
5
|
+
accessKey: string;
|
|
6
|
+
accessSecret: string;
|
|
7
|
+
}
|
|
8
|
+
interface S3Config {
|
|
9
|
+
acl: ObjectCannedACL;
|
|
10
|
+
key: string;
|
|
11
|
+
bucket: string;
|
|
12
|
+
append: boolean;
|
|
13
|
+
}
|
|
14
|
+
export declare const createS3Client: (awsConfig: AWSConfig) => S3Client | null;
|
|
15
|
+
export declare const fileUploadToS3: (files: any, s3Client: S3Client, s3Config: S3Config, encryption: Encryption, options?: {}) => Promise<{
|
|
16
|
+
originalname: any;
|
|
17
|
+
mimetype: any;
|
|
18
|
+
size: any;
|
|
19
|
+
key: string | undefined;
|
|
20
|
+
contentType: string;
|
|
21
|
+
} | {
|
|
22
|
+
originalname: any;
|
|
23
|
+
mimetype: any;
|
|
24
|
+
size: any;
|
|
25
|
+
key: string | undefined;
|
|
26
|
+
contentType: string;
|
|
27
|
+
}[] | null>;
|
|
28
|
+
export {};
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
16
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
17
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
18
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
19
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
24
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
+
function step(op) {
|
|
27
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
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
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
+
switch (op[0]) {
|
|
32
|
+
case 0: case 1: t = op; break;
|
|
33
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
+
default:
|
|
37
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
+
if (t[2]) _.ops.pop();
|
|
42
|
+
_.trys.pop(); continue;
|
|
43
|
+
}
|
|
44
|
+
op = body.call(thisArg, _);
|
|
45
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
50
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
51
|
+
};
|
|
52
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
53
|
+
exports.fileUploadToS3 = exports.createS3Client = void 0;
|
|
54
|
+
var client_s3_1 = require("@aws-sdk/client-s3");
|
|
55
|
+
var fs_1 = __importDefault(require("fs"));
|
|
56
|
+
var mime_types_1 = __importDefault(require("mime-types"));
|
|
57
|
+
var lib_storage_1 = require("@aws-sdk/lib-storage");
|
|
58
|
+
var encryption_1 = require("../encryption");
|
|
59
|
+
var createS3Client = function (awsConfig) {
|
|
60
|
+
if (!awsConfig) {
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
var region = awsConfig.region, accessKey = awsConfig.accessKey, accessSecret = awsConfig.accessSecret;
|
|
64
|
+
var s3client = new client_s3_1.S3Client({
|
|
65
|
+
region: region,
|
|
66
|
+
credentials: {
|
|
67
|
+
accessKeyId: accessKey,
|
|
68
|
+
secretAccessKey: accessSecret,
|
|
69
|
+
},
|
|
70
|
+
});
|
|
71
|
+
return s3client;
|
|
72
|
+
};
|
|
73
|
+
exports.createS3Client = createS3Client;
|
|
74
|
+
var fileUploadToS3 = function (files, s3Client, s3Config, encryption, options) {
|
|
75
|
+
if (options === void 0) { options = {}; }
|
|
76
|
+
return __awaiter(void 0, void 0, void 0, function () {
|
|
77
|
+
var fileOptions, s3Key, _a, _b, _c, _i, index, file, fileObj, fileObj;
|
|
78
|
+
return __generator(this, function (_d) {
|
|
79
|
+
switch (_d.label) {
|
|
80
|
+
case 0:
|
|
81
|
+
if (!Array.isArray(files)) return [3 /*break*/, 5];
|
|
82
|
+
console.log("I have multiple file");
|
|
83
|
+
fileOptions = [];
|
|
84
|
+
s3Key = s3Config.key;
|
|
85
|
+
_a = files;
|
|
86
|
+
_b = [];
|
|
87
|
+
for (_c in _a)
|
|
88
|
+
_b.push(_c);
|
|
89
|
+
_i = 0;
|
|
90
|
+
_d.label = 1;
|
|
91
|
+
case 1:
|
|
92
|
+
if (!(_i < _b.length)) return [3 /*break*/, 4];
|
|
93
|
+
_c = _b[_i];
|
|
94
|
+
if (!(_c in _a)) return [3 /*break*/, 3];
|
|
95
|
+
index = _c;
|
|
96
|
+
file = files[index];
|
|
97
|
+
if (s3Config.append) {
|
|
98
|
+
s3Config.key = "".concat(s3Key, "/").concat(file.originalname);
|
|
99
|
+
}
|
|
100
|
+
return [4 /*yield*/, processFileUploadToS3(file, s3Client, s3Config, encryption, options, encryption ? true : false)];
|
|
101
|
+
case 2:
|
|
102
|
+
fileObj = _d.sent();
|
|
103
|
+
if (fileObj) {
|
|
104
|
+
fileOptions.push(fileObj);
|
|
105
|
+
}
|
|
106
|
+
_d.label = 3;
|
|
107
|
+
case 3:
|
|
108
|
+
_i++;
|
|
109
|
+
return [3 /*break*/, 1];
|
|
110
|
+
case 4: return [2 /*return*/, fileOptions];
|
|
111
|
+
case 5:
|
|
112
|
+
console.log("I have single file");
|
|
113
|
+
if (s3Config.append) {
|
|
114
|
+
s3Config.key = "".concat(s3Config.key, "/").concat(files.originalname);
|
|
115
|
+
}
|
|
116
|
+
return [4 /*yield*/, processFileUploadToS3(files, s3Client, s3Config, encryption, options, encryption ? true : false)];
|
|
117
|
+
case 6:
|
|
118
|
+
fileObj = _d.sent();
|
|
119
|
+
return [2 /*return*/, fileObj];
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
};
|
|
124
|
+
exports.fileUploadToS3 = fileUploadToS3;
|
|
125
|
+
var processFileUploadToS3 = function (file, s3Client, s3Config, encryption, options, encrypt) {
|
|
126
|
+
if (options === void 0) { options = {}; }
|
|
127
|
+
if (encrypt === void 0) { encrypt = false; }
|
|
128
|
+
return __awaiter(void 0, void 0, void 0, function () {
|
|
129
|
+
var contentType, acl, key, bucket, params, encryptedBody, parallelUploads3, data, originalname, mimetype, size, finalObject, error_1;
|
|
130
|
+
return __generator(this, function (_a) {
|
|
131
|
+
switch (_a.label) {
|
|
132
|
+
case 0:
|
|
133
|
+
_a.trys.push([0, 5, , 6]);
|
|
134
|
+
contentType = mime_types_1.default.lookup(file.originalname) || "";
|
|
135
|
+
acl = s3Config.acl, key = s3Config.key, bucket = s3Config.bucket;
|
|
136
|
+
params = null;
|
|
137
|
+
if (!encrypt) return [3 /*break*/, 2];
|
|
138
|
+
return [4 /*yield*/, (0, encryption_1.cryptFile)(file.path, encryption, false)];
|
|
139
|
+
case 1:
|
|
140
|
+
encryptedBody = _a.sent();
|
|
141
|
+
params = __assign({ Bucket: bucket, ACL: acl, Key: key,
|
|
142
|
+
//@ts-ignore
|
|
143
|
+
Body: fs_1.default.createReadStream(encryptedBody), ContentType: contentType }, options);
|
|
144
|
+
return [3 /*break*/, 3];
|
|
145
|
+
case 2:
|
|
146
|
+
params = __assign({ Bucket: bucket, ACL: acl, Key: key, Body: fs_1.default.createReadStream(file.path), ContentType: contentType }, options);
|
|
147
|
+
_a.label = 3;
|
|
148
|
+
case 3:
|
|
149
|
+
parallelUploads3 = new lib_storage_1.Upload({
|
|
150
|
+
client: s3Client,
|
|
151
|
+
params: params,
|
|
152
|
+
});
|
|
153
|
+
return [4 /*yield*/, parallelUploads3.done()];
|
|
154
|
+
case 4:
|
|
155
|
+
data = _a.sent();
|
|
156
|
+
originalname = file.originalname, mimetype = file.mimetype, size = file.size;
|
|
157
|
+
finalObject = {
|
|
158
|
+
originalname: originalname,
|
|
159
|
+
mimetype: mimetype,
|
|
160
|
+
size: size,
|
|
161
|
+
key: data.Key,
|
|
162
|
+
contentType: contentType,
|
|
163
|
+
};
|
|
164
|
+
return [2 /*return*/, finalObject];
|
|
165
|
+
case 5:
|
|
166
|
+
error_1 = _a.sent();
|
|
167
|
+
console.log("error", error_1);
|
|
168
|
+
return [2 /*return*/, null];
|
|
169
|
+
case 6: return [2 /*return*/];
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "drapcode-utility",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.8",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "build/index.js",
|
|
6
6
|
"types": "build/index.d.ts",
|
|
@@ -20,11 +20,14 @@
|
|
|
20
20
|
"license": "ISC",
|
|
21
21
|
"devDependencies": {
|
|
22
22
|
"@types/lodash": "^4.14.179",
|
|
23
|
+
"@types/mime-types": "^2.1.4",
|
|
23
24
|
"del-cli": "^5.0.0",
|
|
24
25
|
"typescript": "^4.0.2"
|
|
25
26
|
},
|
|
26
27
|
"dependencies": {
|
|
27
28
|
"@aws-sdk/client-kms": "^3.540.0",
|
|
29
|
+
"@aws-sdk/client-s3": "^3.499.0",
|
|
30
|
+
"@aws-sdk/lib-storage": "^3.499.0",
|
|
28
31
|
"@types/cookie-session": "^2.0.41",
|
|
29
32
|
"@types/express": "^4.17.7",
|
|
30
33
|
"@types/voca": "^1.4.2",
|
|
@@ -34,6 +37,7 @@
|
|
|
34
37
|
"drapcode-redis": "^1.0.4",
|
|
35
38
|
"express": "^4.17.1",
|
|
36
39
|
"lodash": "^4.17.21",
|
|
40
|
+
"mime-types": "^2.1.35",
|
|
37
41
|
"moment": "^2.29.0",
|
|
38
42
|
"stringify-object": "^3.3.0",
|
|
39
43
|
"voca": "^1.4.0"
|