-
Notifications
You must be signed in to change notification settings - Fork 204
/
Copy pathTestPlatform.ts
87 lines (82 loc) · 2.82 KB
/
TestPlatform.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
import { IOHelper } from '@src/io/IOHelper';
/**
* @partial
*/
export class TestPlatform {
/**
* @target web
* @partial
*/
public static saveFile(name: string, data: Uint8Array): Promise<void> {
return new Promise<void>((resolve, reject) => {
let x: XMLHttpRequest = new XMLHttpRequest();
x.open('POST', 'http://localhost:8090/save-file/', true);
x.onload = () => {
resolve();
};
x.onerror = () => {
reject();
};
const form = new FormData();
form.append('file', new Blob([data]), name);
x.send(form);
});
}
/**
* @target web
* @partial
*/
public static loadFile(path: string): Promise<Uint8Array> {
return new Promise<Uint8Array>((resolve, reject) => {
let x: XMLHttpRequest = new XMLHttpRequest();
x.open('GET', '/base/' + path, true, null, null);
x.responseType = 'arraybuffer';
x.onreadystatechange = () => {
if (x.readyState === XMLHttpRequest.DONE) {
if (x.status === 200) {
let ab: ArrayBuffer = x.response;
let data: Uint8Array = new Uint8Array(ab);
resolve(data);
} else {
let response: string = x.response;
reject('Could not find file: ' + path + ', received:' + response);
}
}
};
x.send();
});
}
/**
* @target web
* @partial
*/
public static listDirectory(path: string): Promise<string[]> {
return new Promise<string[]>((resolve, reject) => {
let x: XMLHttpRequest = new XMLHttpRequest();
x.open('GET', 'http://localhost:8090/list-files?dir=' + path, true, null, null);
x.responseType = 'text';
x.onreadystatechange = () => {
if (x.readyState === XMLHttpRequest.DONE) {
if (x.status === 200) {
resolve(JSON.parse(x.responseText));
} else {
reject('Could not find path: ' + path + ', received:' + x.responseText);
}
}
};
x.send();
});
}
public static async loadFileAsString(path: string): Promise<string> {
const data = await TestPlatform.loadFile(path);
return IOHelper.toString(data, 'UTF-8');
}
public static changeExtension(file: string, extension: string): string {
let lastDot: number = file.lastIndexOf('.');
if (lastDot === -1) {
return file + extension;
} else {
return file.substr(0, lastDot) + extension;
}
}
}