-
Notifications
You must be signed in to change notification settings - Fork 0
/
material.ts
74 lines (69 loc) · 1.71 KB
/
material.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
import { Device } from "./device";
import { checkError } from "./utils";
import { parse as parseDate } from "date-format-parse";
export type File = {
type: "file"
code: string
/** A user-friendly name (not necessarily a valid filename) */
name: string
/** The filename for internal usage */
filename: string
mime_type: string
/** The size in kb */
size: number
/** The creation date as Unix epoch */
creation_date: number
}
export type Directory = {
type: "dir"
code: string
name: string
children: MaterialItem[]
}
export type MaterialItem = File | Directory;
/**
* Parses an item from an API response.
*
* @param item the item as a JS object
* @internal
*/
export function parseMaterial(item: any): MaterialItem { // eslint-disable-line @typescript-eslint/explicit-module-boundary-types
switch (item.tipo) {
case "FILE":
return {
type: "file",
code: item.code,
name: item.descrizione,
filename: item.nomefile,
mime_type: item.cont_type,
size: item.size_kb,
creation_date: parseDate(item.data_ins, "YYYY/MM/DD hh:mm:ss").getTime(),
};
case "DIR":
return {
type: "dir",
code: item.code,
name: item.descrizione,
children: item.files.map(it => parseMaterial(it)),
};
default:
throw new Error("Unknown file type " + item.tipo);
}
}
/**
* Gets a download URL for either a File object or a file code
*
* @param device
* @param file
*/
export async function getDownloadURL(device: Device, file: File | number): Promise<string> {
let code;
if (typeof file == "object") {
code = (file as File).code;
} else {
code = file;
}
const data = await device.post("download.php", { code });
checkError(data);
return data.data.directurl;
}