1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.importCertificate = void 0;
|
4 | const fs_extra_1 = require("fs-extra");
|
5 | const os_1 = require("os");
|
6 | const path = require("path");
|
7 | const builder_util_1 = require("builder-util");
|
8 | const fs_1 = require("builder-util/out/fs");
|
9 | const binDownload_1 = require("../binDownload");
|
10 |
|
11 | async function importCertificate(cscLink, tmpDir, currentDir) {
|
12 | var _a, _b;
|
13 | cscLink = cscLink.trim();
|
14 | let file = null;
|
15 | if ((cscLink.length > 3 && cscLink[1] === ":") || cscLink.startsWith("/") || cscLink.startsWith(".")) {
|
16 | file = cscLink;
|
17 | }
|
18 | else if (cscLink.startsWith("file://")) {
|
19 | file = cscLink.substring("file://".length);
|
20 | }
|
21 | else if (cscLink.startsWith("~/")) {
|
22 | file = path.join((0, os_1.homedir)(), cscLink.substring("~/".length));
|
23 | }
|
24 | else if (cscLink.startsWith("https://")) {
|
25 | const tempFile = await tmpDir.getTempFile({ suffix: ".p12" });
|
26 | await (0, binDownload_1.download)(cscLink, tempFile);
|
27 | return tempFile;
|
28 | }
|
29 | else {
|
30 | const mimeType = (_a = /data:.*;base64,/.exec(cscLink)) === null || _a === void 0 ? void 0 : _a[0];
|
31 | if (mimeType || cscLink.length > 2048 || cscLink.endsWith("=")) {
|
32 | const tempFile = await tmpDir.getTempFile({ suffix: ".p12" });
|
33 | await (0, fs_extra_1.outputFile)(tempFile, Buffer.from(cscLink.substring((_b = mimeType === null || mimeType === void 0 ? void 0 : mimeType.length) !== null && _b !== void 0 ? _b : 0), "base64"));
|
34 | return tempFile;
|
35 | }
|
36 | file = cscLink;
|
37 | }
|
38 | file = path.resolve(currentDir, file);
|
39 | const stat = await (0, fs_1.statOrNull)(file);
|
40 | if (stat == null) {
|
41 | throw new builder_util_1.InvalidConfigurationError(`${file} doesn't exist`);
|
42 | }
|
43 | else if (!stat.isFile()) {
|
44 | throw new builder_util_1.InvalidConfigurationError(`${file} not a file`);
|
45 | }
|
46 | else {
|
47 | return file;
|
48 | }
|
49 | }
|
50 | exports.importCertificate = importCertificate;
|
51 |
|
\ | No newline at end of file |