aboutsummaryrefslogtreecommitdiff
path: root/src/client/Network.ts
blob: 19eff3b3b6e01e554fd5f735b6966663ed4b410e (plain)
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
import { Utils } from '../Utils';
import requestPromise = require('request-promise');
import { Upload } from '../server/SharedMediaTypes';

export namespace Networking {
    export async function FetchFromServer(relativeRoute: string) {
        return (await fetch(relativeRoute)).text();
    }

    export async function PostToServer(relativeRoute: string, body?: any) {
        const options = {
            uri: Utils.prepend(relativeRoute),
            method: 'POST',
            body,
            json: true,
        };
        return requestPromise.post(options);
    }

    /**
     * Handles uploading basic file types to server and makes the API call to "/uploadFormData" endpoint
     * with the mapping of GUID to filem as parameters.
     *
     * @param files the files to be uploaded to the server
     * @returns the response as a json from the server
     */
    export async function UploadFilesToServer<T extends Upload.FileInformation = Upload.FileInformation>(files: File | File[]): Promise<Upload.FileResponse<T>[]> {
        const formData = new FormData();
        if (Array.isArray(files)) {
            if (!files.length) {
                return [];
            }
            const maxFileSize = 50000000;
            if (files.some(f => f.size > maxFileSize)) {
                return new Promise<any>(res =>
                    res([
                        {
                            source: { name: '', type: '', size: 0, toJson: () => ({ name: '', type: '' }) },
                            result: { name: '', message: `max file size (${maxFileSize / 1000000}MB) exceeded` },
                        },
                    ])
                );
            }
            files.forEach(file => formData.append(Utils.GenerateGuid(), file));
        } else {
            formData.append(Utils.GenerateGuid(), files);
        }
        const parameters = {
            method: 'POST',
            body: formData,
        };
        const response = await fetch('/uploadFormData', parameters);
        return response.json();
    }

    export async function UploadYoutubeToServer<T extends Upload.FileInformation = Upload.FileInformation>(videoId: string): Promise<Upload.FileResponse<T>[]> {
        const parameters = {
            method: 'POST',
            body: JSON.stringify({ videoId }),
            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();
    }
}