多维表格
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

39 lines
959 B

import { openLink, useGlobal } from '#imports'
const useAttachment = () => {
const { appInfo } = useGlobal()
const getPossibleAttachmentSrc = (item: Record<string, any>) => {
const res: string[] = []
if (item?.path) res.push(`${appInfo.value.ncSiteUrl}/${item.path}`)
if (item?.url) res.push(item.url)
return res
}
const getAttachmentSrc = async (item: Record<string, any>) => {
if (item?.data) {
return item.data
}
const sources = getPossibleAttachmentSrc(item)
for (const source of sources) {
// test if the source is accessible or not
const res = await fetch(source, { method: 'HEAD' })
if (res.ok) {
return source
}
}
return null
}
const openAttachment = async (item: Record<string, any>) => {
openLink(await getAttachmentSrc(item))
}
return {
getAttachmentSrc,
getPossibleAttachmentSrc,
openAttachment,
}
}
export default useAttachment