From 8bdecb7e0779c258dd9c432de9cbbd49f48bc5de Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Fri, 3 Feb 2023 22:12:39 +0100 Subject: [PATCH] Oops, forgot to commit this. --- gymboard-app/src/api/cdn/index.ts | 80 +++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 gymboard-app/src/api/cdn/index.ts diff --git a/gymboard-app/src/api/cdn/index.ts b/gymboard-app/src/api/cdn/index.ts new file mode 100644 index 0000000..21cda1c --- /dev/null +++ b/gymboard-app/src/api/cdn/index.ts @@ -0,0 +1,80 @@ +import axios from 'axios'; +import { sleep } from 'src/utils'; + +const BASE_URL = 'http://localhost:8082'; + +const api = axios.create({ + baseURL: BASE_URL, +}); + +export enum VideoProcessingStatus { + WAITING = 'WAITING', + IN_PROGRESS = 'IN_PROGRESS', + COMPLETED = 'COMPLETED', + FAILED = 'FAILED', +} + +export interface FileMetadata { + filename: string; + mimeType: string; + size: number; + uploadedAt: string; + availableForDownload: boolean; +} + +export async function uploadVideoToCDN(file: File): Promise { + const response = await api.post('/uploads/video', file, { + headers: { + 'Content-Type': file.type, + }, + }); + return response.data.id; +} + +export async function getVideoProcessingStatus( + id: string +): Promise { + try { + const response = await api.get(`/uploads/video/${id}/status`); + return response.data.status; + } catch (error: any) { + if (error.response && error.response.status === 404) { + return null; + } + throw error; + } +} + +export async function waitUntilVideoProcessingComplete( + id: string +): Promise { + let failureCount = 0; + let attemptCount = 0; + while (failureCount < 5 && attemptCount < 60) { + await sleep(1000); + attemptCount++; + try { + const status = await getVideoProcessingStatus(id); + failureCount = 0; + if ( + status === VideoProcessingStatus.COMPLETED || + status === VideoProcessingStatus.FAILED + ) { + return status; + } + } catch (error: any) { + console.log(error); + failureCount++; + } + } + throw new Error('Failed to wait for processing to finish.'); +} + +export async function getFileMetadata(id: string): Promise { + const response = await api.get(`/files/${id}/metadata`); + return response.data; +} + +export function getFileUrl(id: string): string { + return BASE_URL + '/files/' + id; +}