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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
import formidable from 'formidable';
import { ClientUtils } from '../ClientUtils';
import { Utils } from '../Utils';
import { Upload } from '../server/SharedMediaTypes';
/**
* Networking is repsonsible for connecting the client to the server. Networking
* mainly provides methods that the client can use to begin the process of
* interacting with the server, such as fetching or uploading files.
*/
export namespace Networking {
export async function FetchFromServer(relativeRoute: string) {
return (await fetch(relativeRoute)).text();
}
export function PostToServer(relativeRoute: string, body?: unknown) {
return fetch(ClientUtils.prepend(relativeRoute), {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: body ? JSON.stringify(body) : undefined,
}).then(async response => {
if (response.ok) return response.json() as object;
return await response.text().then(text => ({ error: '' + response.status + ':' + response.statusText + '-' + text }));
});
}
/**
* FileGuidPair attaches a guid to a file that is being uploaded,
* allowing the client to track the upload progress.
*
* When files are dragged to the canvas, the overWriteDoc's ID is
* used as the guid. Otherwise, a new guid is generated.
*/
export interface FileGuidPair {
file: File | Blob;
guid?: string;
}
/**
* Handles uploading basic file types to server and makes the API call to "/uploadFormData" endpoint
* with the mapping of guid to files as parameters.
*
* @param fileguidpairs the files and corresponding guids to be uploaded to the server
* @param browndash whether the endpoint should be invoked on the browndash server
* @returns the response as a json from the server
*/
export async function UploadFilesToServer<T extends Upload.FileInformation = Upload.FileInformation>(fileguidpairs: FileGuidPair | FileGuidPair[], browndash?: boolean): Promise<Upload.FileResponse<T>[]> {
const formData = new FormData();
if (Array.isArray(fileguidpairs)) {
if (!fileguidpairs.length) {
return [];
}
const maxFileSize = 50000000;
if (fileguidpairs.some(f => f.file.size > maxFileSize)) {
return new Promise<Upload.FileResponse<T>[]>(res => res([{ source: { newFilename: '', mimetype: '' } as formidable.File, result: new Error(`max file size (${maxFileSize / 1000000}MB) exceeded`) }]));
}
formData.set('fileguids', fileguidpairs.map(pair => pair.guid).join(';'));
formData.set('filesize', fileguidpairs.reduce((sum, pair) => sum + pair.file.size, 0).toString());
// If the fileguidpair has a guid to use (From the overwriteDoc) use that guid. Otherwise, generate a new guid.
fileguidpairs.forEach(fileguidpair => formData.append(fileguidpair.guid ?? Utils.GenerateGuid(), fileguidpair.file));
} else {
// Handle the case where fileguidpairs is a single file.
const guids = fileguidpairs.guid ?? Utils.GenerateGuid();
formData.set('fileguids', guids);
formData.set('filesize', fileguidpairs.file.size.toString());
formData.append(guids, fileguidpairs.file);
}
const parameters = {
method: 'POST',
body: formData,
};
const endpoint = browndash ? '[insert endpoint allowing local => browndash]' : '/uploadFormData';
const response = await fetch(endpoint, parameters);
return response.json().then((json: Upload.FileResponse<T>[]) =>
json.map(fileresponse => {
if ('message' in fileresponse.result) fileresponse.result = new Error(fileresponse.result.message);
return fileresponse;
})
);
}
export async function UploadYoutubeToServer<T extends Upload.FileInformation = Upload.FileInformation>(videoId: string, overwriteId?: string): Promise<Upload.FileResponse<T>[]> {
const parameters = {
method: 'POST',
body: JSON.stringify({ videoId, overwriteId }),
json: true,
};
const response = await fetch('/uploadYoutubeVideo', parameters);
return response.json();
}
export async function QueryYoutubeProgress(videoId: string): Promise<{ progress: string }> {
const parameters = {
method: 'POST',
body: JSON.stringify({ videoId }),
json: true,
};
const response = await fetch('/queryYoutubeProgress', parameters);
return response.json();
}
}
|