-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTervisUtilityBackendJS.js
65 lines (60 loc) · 1.67 KB
/
TervisUtilityBackendJS.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import uuidv4 from 'uuid/v4.js'
import got from 'got'
import fs from 'fs-extra'
import {Invoke_ProcessTemplate} from "@tervis/tervisutilityjs"
export async function Invoke_ProcessTemplateFile ({
$TemplateFilePath,
$TemplateVariables
}) {
let $TemplateContent = await fs.readFile($TemplateFilePath, "utf-8")
var $Result = Invoke_ProcessTemplate({ $TemplateContent, $TemplateVariables })
return $Result
}
export function New_TemporaryDirectory({
$TemporaryFolderType
}) {
let $GUID = uuidv4()
let $TemporaryFolderRoot = (() => {
if ($TemporaryFolderType === "System") {
return `C:\\windows\\temp`
}
})()
return `${$TemporaryFolderRoot}\\${$GUID}`
}
export async function Invoke_FileDownload({
$URI,
$OutFile
}) {
return new Promise((resolve, reject) => {
const options = {
url: $URI,
timeout: 120000,
stream: true,
}
const stream = got(options).pause();
let fileStream;
stream.on("error", error => {
if (fileStream) {
fileStream.destroy();
}
reject(error);
});
stream.on("data", data => {
fileStream.write(data);
});
stream.on("end", () => {
fileStream.end();
});
stream.on("response", function () {
fileStream = fs.createWriteStream($OutFile);
fileStream.on("error", error => {
stream.destroy();
reject(error);
})
fileStream.on("close", () => {
resolve($OutFile);
});
this.resume();
});
});
}