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(files: File | File[]): Promise[]> { 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(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(videoId: string): Promise[]> { 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(); } }