Spaces:
Sleeping
Sleeping
Upload 6 files
Browse files- src/storage/hfClient.js +48 -67
src/storage/hfClient.js
CHANGED
|
@@ -1,45 +1,33 @@
|
|
| 1 |
-
import { downloadFile, uploadFiles, listFiles } from '@huggingface/hub'
|
| 2 |
import { retry } from '../utils/retry.js'
|
| 3 |
|
| 4 |
const HF_TOKEN = process.env.HF_TOKEN
|
| 5 |
-
const HF_BUCKET_NAME = process.env.HF_BUCKET_NAME
|
|
|
|
| 6 |
|
| 7 |
-
if (!HF_TOKEN)
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
if (!HF_BUCKET_NAME) {
|
| 11 |
-
throw new Error('Missing required environment variable: HF_BUCKET_NAME')
|
| 12 |
-
}
|
| 13 |
|
| 14 |
-
const
|
| 15 |
-
const
|
| 16 |
|
| 17 |
function isNotFoundError(err) {
|
| 18 |
const msg = err?.message?.toLowerCase() ?? ''
|
| 19 |
-
return (
|
| 20 |
-
err?.status === 404 ||
|
| 21 |
-
msg.includes('404') ||
|
| 22 |
-
msg.includes('not found') ||
|
| 23 |
-
msg.includes('entry not found')
|
| 24 |
-
)
|
| 25 |
}
|
| 26 |
|
| 27 |
export async function readJSON(remotePath) {
|
| 28 |
return retry(
|
| 29 |
async () => {
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
const buffer = await response.arrayBuffer()
|
| 41 |
-
const text = Buffer.from(buffer).toString('utf8')
|
| 42 |
-
return JSON.parse(text)
|
| 43 |
},
|
| 44 |
{ attempts: 3, label: `readJSON:${remotePath}` }
|
| 45 |
)
|
|
@@ -48,15 +36,15 @@ export async function readJSON(remotePath) {
|
|
| 48 |
export async function writeJSON(remotePath, data) {
|
| 49 |
return retry(
|
| 50 |
async () => {
|
| 51 |
-
const
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
repo,
|
| 56 |
-
credentials,
|
| 57 |
-
commitTitle: `update ${remotePath}`,
|
| 58 |
-
files: [{ path: remotePath, content: blob }],
|
| 59 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
},
|
| 61 |
{ attempts: 3, label: `writeJSON:${remotePath}` }
|
| 62 |
)
|
|
@@ -65,14 +53,15 @@ export async function writeJSON(remotePath, data) {
|
|
| 65 |
export async function uploadBinary(remotePath, buffer, mimeType) {
|
| 66 |
return retry(
|
| 67 |
async () => {
|
| 68 |
-
const
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
credentials,
|
| 73 |
-
commitTitle: `upload ${remotePath}`,
|
| 74 |
-
files: [{ path: remotePath, content: blob }],
|
| 75 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
},
|
| 77 |
{ attempts: 3, label: `uploadBinary:${remotePath}` }
|
| 78 |
)
|
|
@@ -81,18 +70,15 @@ export async function uploadBinary(remotePath, buffer, mimeType) {
|
|
| 81 |
export async function downloadBinary(remotePath) {
|
| 82 |
return retry(
|
| 83 |
async () => {
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
const buffer = await response.arrayBuffer()
|
| 95 |
-
return Buffer.from(buffer)
|
| 96 |
},
|
| 97 |
{ attempts: 3, label: `downloadBinary:${remotePath}` }
|
| 98 |
)
|
|
@@ -100,17 +86,12 @@ export async function downloadBinary(remotePath) {
|
|
| 100 |
|
| 101 |
export async function fileExists(remotePath) {
|
| 102 |
try {
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
// listFiles on a specific file path β if it yields anything, it exists
|
| 110 |
-
// Try a direct HEAD-like approach via downloadFile returning null
|
| 111 |
-
const response = await downloadFile({ repo, path: remotePath, credentials })
|
| 112 |
-
return response !== null
|
| 113 |
-
} catch (_err) {
|
| 114 |
return false
|
| 115 |
}
|
| 116 |
}
|
|
|
|
|
|
|
| 1 |
import { retry } from '../utils/retry.js'
|
| 2 |
|
| 3 |
const HF_TOKEN = process.env.HF_TOKEN
|
| 4 |
+
const HF_BUCKET_NAME = process.env.HF_BUCKET_NAME // e.g. "gateway-bot-storage"
|
| 5 |
+
const HF_ORG = process.env.HF_ORG // e.g. "Discord-Core-Bot"
|
| 6 |
|
| 7 |
+
if (!HF_TOKEN) throw new Error('Missing required environment variable: HF_TOKEN')
|
| 8 |
+
if (!HF_BUCKET_NAME) throw new Error('Missing required environment variable: HF_BUCKET_NAME')
|
| 9 |
+
if (!HF_ORG) throw new Error('Missing required environment variable: HF_ORG')
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
const BASE_URL = `https://huggingface.co/buckets/${HF_ORG}/${HF_BUCKET_NAME}`
|
| 12 |
+
const authHeader = () => ({ Authorization: `Bearer ${HF_TOKEN}` })
|
| 13 |
|
| 14 |
function isNotFoundError(err) {
|
| 15 |
const msg = err?.message?.toLowerCase() ?? ''
|
| 16 |
+
return err?.status === 404 || msg.includes('404') || msg.includes('not found')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
}
|
| 18 |
|
| 19 |
export async function readJSON(remotePath) {
|
| 20 |
return retry(
|
| 21 |
async () => {
|
| 22 |
+
const res = await fetch(`${BASE_URL}/${remotePath}`, {
|
| 23 |
+
headers: authHeader(),
|
| 24 |
+
})
|
| 25 |
+
if (res.status === 404) return null
|
| 26 |
+
if (!res.ok) throw Object.assign(
|
| 27 |
+
new Error(`GET ${remotePath} β ${res.status}: ${await res.text()}`),
|
| 28 |
+
{ status: res.status }
|
| 29 |
+
)
|
| 30 |
+
return res.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
},
|
| 32 |
{ attempts: 3, label: `readJSON:${remotePath}` }
|
| 33 |
)
|
|
|
|
| 36 |
export async function writeJSON(remotePath, data) {
|
| 37 |
return retry(
|
| 38 |
async () => {
|
| 39 |
+
const res = await fetch(`${BASE_URL}/${remotePath}`, {
|
| 40 |
+
method: 'PUT',
|
| 41 |
+
headers: { ...authHeader(), 'Content-Type': 'application/json' },
|
| 42 |
+
body: JSON.stringify(data, null, 2),
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
})
|
| 44 |
+
if (!res.ok) throw Object.assign(
|
| 45 |
+
new Error(`PUT ${remotePath} β ${res.status}: ${await res.text()}`),
|
| 46 |
+
{ status: res.status }
|
| 47 |
+
)
|
| 48 |
},
|
| 49 |
{ attempts: 3, label: `writeJSON:${remotePath}` }
|
| 50 |
)
|
|
|
|
| 53 |
export async function uploadBinary(remotePath, buffer, mimeType) {
|
| 54 |
return retry(
|
| 55 |
async () => {
|
| 56 |
+
const res = await fetch(`${BASE_URL}/${remotePath}`, {
|
| 57 |
+
method: 'PUT',
|
| 58 |
+
headers: { ...authHeader(), 'Content-Type': mimeType },
|
| 59 |
+
body: buffer,
|
|
|
|
|
|
|
|
|
|
| 60 |
})
|
| 61 |
+
if (!res.ok) throw Object.assign(
|
| 62 |
+
new Error(`PUT ${remotePath} β ${res.status}: ${await res.text()}`),
|
| 63 |
+
{ status: res.status }
|
| 64 |
+
)
|
| 65 |
},
|
| 66 |
{ attempts: 3, label: `uploadBinary:${remotePath}` }
|
| 67 |
)
|
|
|
|
| 70 |
export async function downloadBinary(remotePath) {
|
| 71 |
return retry(
|
| 72 |
async () => {
|
| 73 |
+
const res = await fetch(`${BASE_URL}/${remotePath}`, {
|
| 74 |
+
headers: authHeader(),
|
| 75 |
+
})
|
| 76 |
+
if (res.status === 404) return null
|
| 77 |
+
if (!res.ok) throw Object.assign(
|
| 78 |
+
new Error(`GET ${remotePath} β ${res.status}`),
|
| 79 |
+
{ status: res.status }
|
| 80 |
+
)
|
| 81 |
+
return Buffer.from(await res.arrayBuffer())
|
|
|
|
|
|
|
|
|
|
| 82 |
},
|
| 83 |
{ attempts: 3, label: `downloadBinary:${remotePath}` }
|
| 84 |
)
|
|
|
|
| 86 |
|
| 87 |
export async function fileExists(remotePath) {
|
| 88 |
try {
|
| 89 |
+
const res = await fetch(`${BASE_URL}/${remotePath}`, {
|
| 90 |
+
method: 'HEAD',
|
| 91 |
+
headers: authHeader(),
|
| 92 |
+
})
|
| 93 |
+
return res.ok
|
| 94 |
+
} catch {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
return false
|
| 96 |
}
|
| 97 |
}
|