ai-tube / src /app /server /actions /ai-tube-hf /downloadFileAsText.ts
jbilcke-hf's picture
jbilcke-hf HF staff
hide some features for the beta + improve player
f27679f
raw
history blame
1.13 kB
import { downloadFile } from "@/huggingface/hub/src"
import { getCredentials } from "./getCredentials"
export async function downloadFileAsText({
repo,
path,
apiKey,
renewCache = false,
neverThrow = false
}: {
repo: string
path: string
apiKey?: string
/**
* Force renewing the cache
*
* False by default
*/
renewCache?: boolean
/**
* If set to true, this function will never throw an exception
* this is useful in workflow where we don't care about what happened
*
* False by default
*/
neverThrow?: boolean
}): Promise<string> {
try {
const { credentials } = await getCredentials(apiKey)
const response = await downloadFile({
repo,
path,
credentials,
requestInit: renewCache
? { cache: "no-cache" }
: undefined
})
const text = await response?.text()
if (typeof text !== "string") {
throw new Error(`file has no text content`)
}
return text
} catch (err) {
if (neverThrow) {
console.error(`downloadFileAsText():`, err)
return ""
} else {
throw err
}
}
}