task-7: buildDownloadUrl()/resolveDownloadUrl() 下载 URL 统一构造(user_id 追加与去重、fresh 优先)
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
export interface DownloadableItem {
|
||||
freshDownloadUrl?: string | null
|
||||
downloadUrl?: string | null
|
||||
}
|
||||
|
||||
export function buildDownloadUrl(path: string, userId?: number): string {
|
||||
if (userId === undefined || userId === null || Number.isNaN(userId)) {
|
||||
return path
|
||||
}
|
||||
if (/[?&]user_id=/.test(path)) {
|
||||
return path
|
||||
}
|
||||
const separator = path.includes('?') ? '&' : '?'
|
||||
return `${path}${separator}user_id=${userId}`
|
||||
}
|
||||
|
||||
export function resolveDownloadUrl(item: DownloadableItem | null | undefined): string {
|
||||
if (!item) {
|
||||
return ''
|
||||
}
|
||||
return item.freshDownloadUrl || item.downloadUrl || ''
|
||||
}
|
||||
Reference in New Issue
Block a user