-
Notifications
You must be signed in to change notification settings - Fork 1
/
file.ts
54 lines (44 loc) · 1.58 KB
/
file.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
import { AxiosRequestConfig } from 'axios'
import HTTP from '../http'
import FormData from '../helpers/formData'
export default class File {
serverId: string
path: string
private http: HTTP
constructor(serverId: string, path: string, http: HTTP) {
this.serverId = serverId
this.path = path
this.http = http
}
public get url(): string {
return `/game-servers/${this.serverId}/files/${this.path}`
}
public async unzip(destination?: string): Promise<void> {
const unzipUrl = `/game-servers/${this.serverId}/unzip/${this.path}`
if (typeof destination !== 'undefined') {
const payload = new URLSearchParams()
payload.append('destination', destination)
await this.http.post(unzipUrl, payload)
} else
await this.http.post(unzipUrl)
}
public async delete(): Promise<void> {
await this.http.delete(this.url)
}
public async move(destination: string): Promise<void> {
const payload = new URLSearchParams()
payload.append('destination', destination)
await this.http.put(this.url, payload)
}
public async download(asText = false, config: AxiosRequestConfig = {}): Promise<Blob | string> {
if (!asText) {
config.responseType = 'blob'
}
return await this.http.get(`${this.url}?as_text=${asText.toString()}`, config)
}
public async upload(data: Blob, config: AxiosRequestConfig = {}): Promise<void> {
const payload = new FormData()
payload.append('file', data)
await this.http.post(`https://upload.dathost.net/api/0.1/game-servers/${this.serverId}/files/${this.path}`, payload, config)
}
}