Skip to content

Commit d160f52

Browse files
committed
feat: enhance UC.Files resolution with improved payload handling and URL construction
1 parent d44682e commit d160f52

1 file changed

Lines changed: 37 additions & 3 deletions

File tree

renderer/src/lib/downloads.ts

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ export const SUPPORTED_DOWNLOAD_HOSTS: PreferredDownloadHost[] = ["ucfiles", "pi
5757
const PREFERRED_HOSTS: PreferredDownloadHost[] = ["ucfiles", "pixeldrain"]
5858
const PIXELDRAIN_404_MESSAGE = "Pixeldrain returned 404. The link appears to be dead."
5959
const UCFILES_404_MESSAGE = "UC.Files returned 404. The link appears to be dead."
60+
const UCFILES_IDENTIFIER_RE = /^[A-Za-z0-9_-]{1,64}$/
61+
62+
type UCFilesResolvePayload = {
63+
fileId?: string
64+
downloadUrl?: string
65+
}
6066

6167
function normalizeUCFilesHostValue(value: string): string {
6268
return String(value || "")
@@ -300,6 +306,29 @@ export function isUCFilesUrl(url: string): boolean {
300306
}
301307
}
302308

309+
function buildUCFilesDownloadUrl(token: string): string {
310+
return `https://files.union-crax.xyz/download/${encodeURIComponent(token)}`
311+
}
312+
313+
function getUCFilesResolvePayload(value: string): UCFilesResolvePayload | null {
314+
const trimmed = String(value || "").trim()
315+
if (!trimmed) return null
316+
317+
if (isUCFilesUrl(trimmed)) {
318+
return { downloadUrl: trimmed }
319+
}
320+
321+
if (!UCFILES_IDENTIFIER_RE.test(trimmed)) {
322+
return null
323+
}
324+
325+
if (/^\d+$/.test(trimmed)) {
326+
return { fileId: trimmed }
327+
}
328+
329+
return { downloadUrl: buildUCFilesDownloadUrl(trimmed) }
330+
}
331+
303332
/**
304333
* Returns true if the UC.Files URL is already a signed /dl/ token URL
305334
* (i.e. it was already resolved and doesn't need re-resolution).
@@ -336,13 +365,18 @@ export async function resolveUCFilesDownload(url: string): Promise<ResolvedDownl
336365

337366
const fileId = extractUCFilesFileId(url)
338367
const shareDownloadUrl = isUCFilesShareDownloadUrl(url) ? url : null
339-
if (!fileId && !shareDownloadUrl) return { url, resolved: false }
368+
const payload = fileId
369+
? { fileId }
370+
: shareDownloadUrl
371+
? { downloadUrl: shareDownloadUrl }
372+
: getUCFilesResolvePayload(url)
373+
if (!payload) return { url, resolved: false }
340374

341375
try {
342376
const response = await apiFetch("/api/ucfiles/resolve", {
343377
method: "POST",
344378
headers: { "Content-Type": "application/json" },
345-
body: JSON.stringify(fileId ? { fileId } : { downloadUrl: shareDownloadUrl }),
379+
body: JSON.stringify(payload),
346380
})
347381

348382
if (response.status === 404) {
@@ -475,7 +509,7 @@ export async function resolveDownloadUrl(host: string, url: string): Promise<Res
475509
? String((url as any).url)
476510
: String(url ?? "")
477511

478-
if (host === "ucfiles" || isUCFilesUrl(normalizedUrl)) {
512+
if (host === "ucfiles" || isUCFilesUrl(normalizedUrl) || Boolean(getUCFilesResolvePayload(normalizedUrl))) {
479513
return resolveUCFilesDownload(normalizedUrl)
480514
}
481515
if (host === "pixeldrain" || isPixeldrainUrl(normalizedUrl)) {

0 commit comments

Comments
 (0)