20260518172616
This commit is contained in:
1 parent
2e704bbbb0
commit
5cfc87eee8
8 files changed
+500
-276
No files matched your search
@@ -126,35 +126,63 @@ export function formatFileSize(size: number): string {
|
||||
|
||||
/** 获取文件名:从 URL 提取最后一段 */
|
||||
function getFileName(fileUrl: string) {
|
||||
return decodeURIComponent(fileUrl.split('/').pop() || '未命名文件')
|
||||
return decodeURIComponent(
|
||||
fileUrl.split('/').pop() || '未命名文件',
|
||||
)
|
||||
}
|
||||
|
||||
/** 获取文件后缀 */
|
||||
export function getFileExtension(fileUrl: string) {
|
||||
const clean = fileUrl.split('?')[0].split('#')[0].trim()
|
||||
/** 获取文件后缀(根据文件名) */
|
||||
export function getFileExtension(fileName: string) {
|
||||
const clean = fileName
|
||||
.split('?')[0]
|
||||
.split('#')[0]
|
||||
.trim()
|
||||
|
||||
const lastDot = clean.lastIndexOf('.')
|
||||
return lastDot === -1 ? '' : clean.substring(lastDot + 1).toLowerCase()
|
||||
|
||||
return lastDot === -1
|
||||
? ''
|
||||
: clean.substring(lastDot + 1).toLowerCase()
|
||||
}
|
||||
|
||||
/** 文件类型判断 */
|
||||
export function isImage(fileUrl: string) {
|
||||
return ['jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp'].includes(getFileExtension(fileUrl))
|
||||
/** 文件类型判断(根据文件名) */
|
||||
export function isImage(fileName: string) {
|
||||
return [
|
||||
'jpg',
|
||||
'jpeg',
|
||||
'png',
|
||||
'gif',
|
||||
'webp',
|
||||
'bmp',
|
||||
].includes(getFileExtension(fileName))
|
||||
}
|
||||
|
||||
export function isVideo(fileUrl: string) {
|
||||
return ['mp4', 'avi', 'mov', 'mkv', 'flv', 'wmv', '3gp'].includes(getFileExtension(fileUrl))
|
||||
export function isVideo(fileName: string) {
|
||||
return [
|
||||
'mp4',
|
||||
'avi',
|
||||
'mov',
|
||||
'mkv',
|
||||
'flv',
|
||||
'wmv',
|
||||
'3gp',
|
||||
].includes(getFileExtension(fileName))
|
||||
}
|
||||
|
||||
/** =================== 下载入口 =================== */
|
||||
export function downloadFile(fileUrl: string, fileName?: string) {
|
||||
const type: 'image' | 'video' | 'file' = isImage(fileUrl)
|
||||
? 'image'
|
||||
: isVideo(fileUrl)
|
||||
? 'video'
|
||||
: 'file'
|
||||
|
||||
export function downloadFile(
|
||||
fileUrl: string,
|
||||
fileName?: string,
|
||||
) {
|
||||
const finalName = fileName ?? getFileName(fileUrl)
|
||||
|
||||
const type: 'image' | 'video' | 'file'
|
||||
= isImage(finalName)
|
||||
? 'image'
|
||||
: isVideo(finalName)
|
||||
? 'video'
|
||||
: 'file'
|
||||
|
||||
if (isH5) {
|
||||
downloadH5(fileUrl, finalName)
|
||||
}
|
||||
@@ -165,7 +193,6 @@ export function downloadFile(fileUrl: string, fileName?: string) {
|
||||
downloadApp(fileUrl, type, finalName)
|
||||
}
|
||||
}
|
||||
|
||||
/** =================== H5 下载 =================== */
|
||||
async function downloadH5(fileUrl: string, fileName: string) {
|
||||
try {
|
||||
|
||||
Reference in new issue
Block a user