update download function
This commit is contained in:
@@ -117,4 +117,27 @@ export async function viewFileInfo(fileId: string) {
|
||||
} catch (error) {
|
||||
return error
|
||||
}
|
||||
}
|
||||
|
||||
export async function downloadFile(fileId: string, fileName: string) {
|
||||
const token = await getSessionData('token')
|
||||
|
||||
const response = await fetch(`${API_BASE_URL}/api/v1/files/download/${fileId}`, {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`,
|
||||
},
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
throw 'failed-to-download-file';
|
||||
}
|
||||
|
||||
const arrayBuffer = await response.arrayBuffer()
|
||||
const base64 = Buffer.from(arrayBuffer).toString('base64')
|
||||
|
||||
return {
|
||||
data: base64,
|
||||
contentType: response.headers.get('content-type') || 'application/octet-stream',
|
||||
fileName: fileName
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user