-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathFileClient.ts
More file actions
44 lines (40 loc) · 1.36 KB
/
Copy pathFileClient.ts
File metadata and controls
44 lines (40 loc) · 1.36 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
import FormData from "form-data";
import { platformDeps } from "../platform";
import { UnsupportedPlatformError } from "../platform/UnsupportedPlatformError";
import { BaseClient } from "./BaseClient";
export class FileClient extends BaseClient {
public async uploadFile(params: {
file: { name: string; data: unknown } | { path: string };
}): Promise<{ fileKey: string }> {
const path = this.buildPathWithGuestSpaceId({
endpointName: "file",
});
const formData = new FormData();
if ("path" in params.file) {
try {
const { name, data } = await platformDeps.readFileFromPath(
params.file.path,
);
formData.append("file", data, name);
} catch (e) {
if (e instanceof UnsupportedPlatformError) {
throw new Error(
`uploadFile doesn't allow to accept a file path in ${e.platform} environment.`,
);
}
throw e;
}
} else {
const { name, data } = params.file;
const fileData = platformDeps.buildFormDataValue(data, name);
formData.append("file", fileData, name);
}
return this.client.postData(path, formData);
}
public downloadFile(params: { fileKey: string }): Promise<ArrayBuffer> {
const path = this.buildPathWithGuestSpaceId({
endpointName: "file",
});
return this.client.getData(path, params);
}
}