add view de
This commit is contained in:
@@ -694,6 +694,13 @@ export interface ExamSubmission {
|
||||
gitPublishUrl?: string;
|
||||
}
|
||||
|
||||
function decodeBase64ToArrayBuffer(b64: string): ArrayBuffer {
|
||||
const bin = atob(b64);
|
||||
const out = new Uint8Array(bin.length);
|
||||
for (let i = 0; i < bin.length; i++) out[i] = bin.charCodeAt(i);
|
||||
return out.buffer;
|
||||
}
|
||||
|
||||
export const apiExam = {
|
||||
list: async (): Promise<{ data: ExamRoomItem[] }> => {
|
||||
const res = await staffFetch('/exam-rooms');
|
||||
@@ -798,6 +805,38 @@ export const apiExam = {
|
||||
if (!res.ok) await parseError(res, 'Xóa gói đề thất bại');
|
||||
return res.json();
|
||||
},
|
||||
fetchPaperPdfBytes: async (examId: number, paperId: number): Promise<ArrayBuffer> => {
|
||||
const res = await staffFetch(`/exam-rooms/${examId}/papers/${paperId}/view`);
|
||||
if (!res.ok) await parseError(res, 'Không mở được đề PDF');
|
||||
const json = await res.json() as { data: string };
|
||||
if (!json.data) throw new Error('Server không trả dữ liệu PDF');
|
||||
return decodeBase64ToArrayBuffer(json.data);
|
||||
},
|
||||
fetchPaperResourceBytes: async (examId: number, paperId: number, resourceId: number): Promise<ArrayBuffer> => {
|
||||
const res = await staffFetch(`/exam-rooms/${examId}/papers/${paperId}/resources/${resourceId}/view`);
|
||||
if (!res.ok) await parseError(res, 'Không tải tài nguyên');
|
||||
const json = await res.json() as { data: string };
|
||||
if (!json.data) throw new Error('Server không trả dữ liệu file');
|
||||
return decodeBase64ToArrayBuffer(json.data);
|
||||
},
|
||||
downloadPaperFile: async (examId: number, paperId: number, opts?: { resourceId?: number; fileName?: string }) => {
|
||||
const params = new URLSearchParams();
|
||||
if (opts?.resourceId) {
|
||||
params.set('kind', 'resource');
|
||||
params.set('fileId', String(opts.resourceId));
|
||||
} else {
|
||||
params.set('kind', 'pdf');
|
||||
}
|
||||
const res = await staffFetch(`/exam-rooms/${examId}/papers/${paperId}/download?${params}`);
|
||||
if (!res.ok) await parseError(res, 'Tải file thất bại');
|
||||
const buf = await res.arrayBuffer();
|
||||
const url = URL.createObjectURL(new Blob([buf]));
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = opts?.fileName || 'de-thi.pdf';
|
||||
a.click();
|
||||
URL.revokeObjectURL(url);
|
||||
},
|
||||
assignRandom: async (id: number) => {
|
||||
const res = await staffFetch(`/exam-rooms/${id}/assign-random`, { method: 'POST' });
|
||||
if (!res.ok) await parseError(res, 'Chia gói đề thất bại');
|
||||
|
||||
Reference in New Issue
Block a user