feat: implement document api call
This commit is contained in:
parent
f303ed3041
commit
d7d0aa9739
2 changed files with 100 additions and 0 deletions
80
src/api/document.ts
Normal file
80
src/api/document.ts
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
import type { StorageFile } from '@/interface/response/storage'
|
||||
|
||||
const API_URI = import.meta.env.VITE_API_URI_CONFIG
|
||||
|
||||
export async function getDocumentList(volume: string, id: string) {
|
||||
const res = await fetch(`${API_URI}/document/${volume}/${id}`, {
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
},
|
||||
})
|
||||
|
||||
if (res && res.ok) {
|
||||
return (await res.json()) as StorageFile
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
export async function getDocumentInfo(
|
||||
volume: string,
|
||||
id: string,
|
||||
file: string
|
||||
) {
|
||||
const res = await fetch(`${API_URI}/document/${volume}/${id}/${file}`, {
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
},
|
||||
})
|
||||
|
||||
if (res && res.ok) {
|
||||
return (await res.json()) as StorageFile & { downloadUrl: string }
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
const util = {
|
||||
formatBytes: (bytes: number, decimals = 2) => {
|
||||
if (!+bytes) return '0 Bytes'
|
||||
|
||||
const k = 1024
|
||||
const dm = decimals < 0 ? 0 : decimals
|
||||
const sizes = [
|
||||
'Bytes',
|
||||
'KiB',
|
||||
'MiB',
|
||||
'GiB',
|
||||
'TiB',
|
||||
'PiB',
|
||||
'EiB',
|
||||
'ZiB',
|
||||
'YiB',
|
||||
]
|
||||
const i = Math.floor(Math.log(bytes) / Math.log(k))
|
||||
|
||||
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`
|
||||
},
|
||||
download: async (url: string, name?: string) => {
|
||||
await fetch(url, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
})
|
||||
.then((res) => res.blob())
|
||||
.then((blob) => {
|
||||
const a = document.createElement('a')
|
||||
a.href = window.URL.createObjectURL(blob)
|
||||
a.download = name ?? 'download'
|
||||
a.click()
|
||||
a.remove()
|
||||
})
|
||||
},
|
||||
}
|
||||
|
||||
const doc = {
|
||||
list: getDocumentList,
|
||||
info: getDocumentInfo,
|
||||
util,
|
||||
}
|
||||
|
||||
export default doc
|
||||
Loading…
Add table
Add a link
Reference in a new issue