Skip to content

Commit

Permalink
moved uploadFile and postFormData
Browse files Browse the repository at this point in the history
  • Loading branch information
mono424 committed Sep 14, 2023
1 parent f21f0db commit 0b939c6
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 39 deletions.
11 changes: 5 additions & 6 deletions web/ts/api/admin-lecture-list.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {del, get, patch, post, put} from "../utilities/fetch-wrappers";
import {del, get, patch, post, postFormData, PostFormDataListener, put, uploadFile} from "../utilities/fetch-wrappers";
import { StatusCodes } from "http-status-codes";
import { Delete, patchData, postData, postFormData, putData, uploadFile, UploadFileListener } from "../global";

export interface UpdateLectureMetaRequest {
name?: string;
Expand Down Expand Up @@ -232,7 +231,7 @@ export const AdminLectureList = {
* @param section
*/
updateSection: async (lectureId: number, section: VideoSection): Promise<void> => {
const res = await putData(`/api/stream/${lectureId}/sections/${section.id}`, {
const res = await put(`/api/stream/${lectureId}/sections/${section.id}`, {
Description: section.description,
StartHours: section.startHours,
StartMinutes: section.startMinutes,
Expand Down Expand Up @@ -276,7 +275,7 @@ export const AdminLectureList = {
lectureId: number,
videoType: string,
file: File,
listener: UploadFileListener = {},
listener: PostFormDataListener = {},
) => {
await uploadFile(
`/api/course/${courseId}/uploadVODMedia?streamID=${lectureId}&videoType=${videoType}`,
Expand All @@ -296,7 +295,7 @@ export const AdminLectureList = {
courseId: number,
lectureId: number,
file: File,
listener: UploadFileListener = {},
listener: PostFormDataListener = {},
) => {
return await uploadFile(`/api/stream/${lectureId}/files?type=file`, file, listener);
},
Expand All @@ -312,7 +311,7 @@ export const AdminLectureList = {
courseId: number,
lectureId: number,
url: string,
listener: UploadFileListener = {},
listener: PostFormDataListener = {},
) => {
const vodUploadFormData = new FormData();
vodUploadFormData.append("file_url", url);
Expand Down
33 changes: 0 additions & 33 deletions web/ts/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,39 +45,6 @@ export async function Delete(url = "") {
});
}

export interface UploadFileListener {
onProgress?: (progress: number) => void;
}

export function uploadFile(url: string, file: File, listener: UploadFileListener = {}): Promise<XMLHttpRequest> {
const vodUploadFormData = new FormData();
vodUploadFormData.append("file", file);
return postFormData(url, vodUploadFormData, listener);
}

export function postFormData(url: string, data: FormData, listener: UploadFileListener = {}): Promise<XMLHttpRequest> {
const xhr = new XMLHttpRequest();
return new Promise((resolve, reject) => {
xhr.onloadend = () => {
if (xhr.status === 200) {
resolve(xhr);
} else {
reject(xhr);
}
};
xhr.upload.onprogress = (e: ProgressEvent) => {
if (!e.lengthComputable) {
return;
}
if (listener.onProgress) {
listener.onProgress(Math.floor(100 * (e.loaded / e.total)));
}
};
xhr.open("POST", url);
xhr.send(data);
});
}

export function sendFormData(url, formData: FormData) {
const HttpReq = new XMLHttpRequest();
HttpReq.open("POST", url, false);
Expand Down
49 changes: 49 additions & 0 deletions web/ts/utilities/fetch-wrappers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {postFormData} from "../global";

/**
* Wrapper for Javascript's fetch function for GET
* @param {string} url URL to fetch
Expand Down Expand Up @@ -42,6 +44,41 @@ export async function post(url: string, body: object = {}) {
});
}


export interface PostFormDataListener {
onProgress?: (progress: number) => void;
}

/**
* Wrapper for XMLHttpRequest to send form data via POST
* @param {string} url URL to send to
* @param {FormData} body form-data
* @param {PostFormDataListener} listener attached progress listeners
* @return {Promise<XMLHttpRequest>}
*/
export function postFormData(url: string, body: FormData, listener: PostFormDataListener = {}): Promise<XMLHttpRequest> {
const xhr = new XMLHttpRequest();
return new Promise((resolve, reject) => {
xhr.onloadend = () => {
if (xhr.status === 200) {
resolve(xhr);
} else {
reject(xhr);
}
};
xhr.upload.onprogress = (e: ProgressEvent) => {
if (!e.lengthComputable) {
return;
}
if (listener.onProgress) {
listener.onProgress(Math.floor(100 * (e.loaded / e.total)));
}
};
xhr.open("POST", url);
xhr.send(body);
});
}

/**
* Wrapper for Javascript's fetch function for PUT
* @param {string} url URL to fetch
Expand Down Expand Up @@ -92,3 +129,15 @@ export async function patch(url = "", body = {}) {
export async function del(url: string) {
return await fetch(url, { method: "DELETE" });
}

/**
* Wrapper for XMLHttpRequest to upload a file
* @param url URL to upload to
* @param file File to be uploaded
* @param listener Upload progress listeners
*/
export function uploadFile(url: string, file: File, listener: PostFormDataListener = {}): Promise<XMLHttpRequest> {
const vodUploadFormData = new FormData();
vodUploadFormData.append("file", file);
return postFormData(url, vodUploadFormData, listener);
}

0 comments on commit 0b939c6

Please sign in to comment.