fix: download bunny materials through academy
This commit is contained in:
@@ -126,6 +126,26 @@ export function clearSession() {
|
||||
localStorage.removeItem(SESSION_KEY);
|
||||
}
|
||||
|
||||
export async function downloadCourseAsset(assetId: string, filename: string) {
|
||||
const session = getSession();
|
||||
const headers = new Headers();
|
||||
if (session) headers.set('Authorization', `Bearer ${session.token}`);
|
||||
const response = await fetch(`${API_URL}/courses/assets/${assetId}/download`, { headers });
|
||||
if (!response.ok) {
|
||||
const body = await response.json().catch(() => ({}));
|
||||
throw new ApiError(body.error || 'Não foi possível baixar este material.', response.status);
|
||||
}
|
||||
const blob = await response.blob();
|
||||
const url = URL.createObjectURL(blob);
|
||||
const link = document.createElement('a');
|
||||
link.href = url;
|
||||
link.download = filename;
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
link.remove();
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
|
||||
export async function apiRequest<T>(path: string, options: RequestInit = {}): Promise<T> {
|
||||
const session = getSession();
|
||||
const headers = new Headers(options.headers);
|
||||
|
||||
Reference in New Issue
Block a user