-
Notifications
You must be signed in to change notification settings - Fork 0
/
load.ts
89 lines (79 loc) · 2.41 KB
/
load.ts
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { dirname, writeAll } from "./deps.ts";
export function getFilenameFromURL(urlString: string): string {
try {
const url = new URL(urlString);
// Split the pathname using '/' and get the last segment (the filename)
const pathnameSegments = url.pathname.split("/");
return pathnameSegments[pathnameSegments.length - 1];
} catch (error) {
console.error("Invalid URL:", error);
return "";
}
}
export function isLocalPathOrURL(pathOrURL: string): boolean {
// Check if the pathOrURL starts with common local file system protocols
return /^(file:|\/|\.\/|\.\.\/)/.test(pathOrURL);
}
export function resolveRelativeURL(
baseURL: string,
relativeFilename: string,
): string {
const baseUrl = new URL(baseURL);
const resolvedURL = new URL(relativeFilename, baseUrl);
return resolvedURL.toString();
}
export async function download(
source: string,
destination: string,
): Promise<void> {
//create cache folder if does not exist
if (!await Deno.stat(dirname(destination)).catch(() => null)) {
// The folder does not exist, so we can create it.
try {
await Deno.mkdir(dirname(destination));
} catch (error) {
console.error(`Error creating cache folder: ${error}`);
}
}
// We use browser fetch API
const response = await fetch(source);
const blob = await response.blob();
// We convert the blob into a typed array
// so we can use it to write the data into the file
const buf = await blob.arrayBuffer();
const data = new Uint8Array(buf);
// We then create a new file and write into it
const file = await Deno.create(destination);
await writeAll(file, data);
// We can finally close the file
Deno.close(file.rid);
}
/**
* Unzip the file
*/
export async function unzip(
filepath: string,
destinationDir: string,
): Promise<void> {
// We execute the command
// The function returns details about the spawned process
const process = new Deno.Command("unzip", {
args: [
"-q",
filepath,
"-d",
destinationDir,
],
});
// create subprocess and collect output
const { code, stdout, stderr } = await process.output();
console.log("---->", code, stdout, stderr);
}
export function set_collection_path() {
let collection_path =
Deno.env.get("ORG_KGRID_JAVASCRIPT_ACTIVATOR_COLLECTION_PATH") ?? "";
if (collection_path == "" || undefined) {
collection_path = "./shelf";
}
return collection_path;
}