-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.js
More file actions
83 lines (74 loc) · 1.77 KB
/
lib.js
File metadata and controls
83 lines (74 loc) · 1.77 KB
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
/**
* A number, or a string containing a number.
* @typedef {{ path: string}} Storage
*/
export class CCAPI {
/**
* @type {`${number}.${number}.${number}.${number}:${number}`}
*/
host = undefined;
/**
* @param {`${number}.${number}.${number}.${number}:${number}`} ip
*/
constructor(ip) {
this.host = ip;
}
// abstract fetch function, so the strategy used to fetch can be chosen from the outside too.
async fetch(endpoint) {
return fetch(`http://${this.host}${endpoint}`, {}).then(async (res) => {
if (res.ok) {
switch (res.headers.get("Content-Type")) {
case "application/json":
return res.json();
default:
return res.blob();
}
}
throw new Error(`${res.status} ${res.statusText} ${await res.text()}`);
});
}
async index() {
return await this.fetch("/ccapi");
}
/**
* @returns {Promise<Storage[]>}
*/
async storage() {
return await this.fetch("/ccapi/ver110/devicestatus/storage").then(
(data) => {
return data.storagelist;
},
);
}
/**
* @param {Storage} storage
*/
async files(storage) {
const folders = await this.fetch(storage.path);
/** @type {string[]} */
const filePaths = [];
for (const folder of folders.path) {
const files = await this.fetch(folder);
filePaths.push(...files.path);
}
return filePaths;
}
/**
* @param {string} filePath
*/
async thumbnail(filePath) {
return await this.fetch(`${filePath}?kind=thumbnail`);
}
/**
* @param {string} filePath
*/
async info(filePath) {
return await this.fetch(`${filePath}?kind=info`);
}
/**
* @param {string} filePath
*/
async original(filePath) {
return await this.fetch(filePath);
}
}