[Refactor] Rename hooks/ to composables/ (#2437)

This commit is contained in:
Chenlei Hu
2025-02-05 15:05:56 -05:00
committed by GitHub
parent c6ef107111
commit 6525ae7cf4
41 changed files with 40 additions and 34 deletions

View File

@@ -0,0 +1,44 @@
import { onMounted, ref } from 'vue'
export function useDownload(url: string, fileName?: string) {
const fileSize = ref<number | null>(null)
const fetchFileSize = async (): Promise<number | null> => {
try {
const response = await fetch(url, { method: 'HEAD' })
if (!response.ok) throw new Error('Failed to fetch file size')
const size = response.headers.get('content-length')
if (size) {
return parseInt(size)
} else {
console.error('"content-length" header not found')
return null
}
} catch (e) {
console.error('Error fetching file size:', e)
return null
}
}
/**
* Trigger browser download
*/
const triggerBrowserDownload = () => {
const link = document.createElement('a')
link.href = url
link.download = fileName || url.split('/').pop() || 'download'
link.target = '_blank' // Opens in new tab if download attribute is not supported
link.rel = 'noopener noreferrer' // Security best practice for _blank links
link.click()
}
onMounted(async () => {
fileSize.value = await fetchFileSize()
})
return {
triggerBrowserDownload,
fileSize
}
}