Spaces:
Sleeping
Sleeping
| import fs from "fs"; | |
| import { uploadFile } from "@huggingface/hub"; | |
| import type { RepoType } from "@huggingface/hub"; | |
| const HF_TOKEN = process.env.HF_TOKEN!; | |
| const REPO = "TwanAPI/DataTwan"; | |
| const TYPE: RepoType = "dataset"; | |
| export async function uploadToHF( | |
| localPath: string, | |
| originalName: string | |
| ) { | |
| // ✅ PATH AN TOÀN | |
| if (originalName.includes("..") || originalName.includes("/")) { | |
| throw new Error("Invalid filename"); | |
| } | |
| const hfPath = `uploads/${Date.now()}-${originalName}`; | |
| const res = await uploadFile({ | |
| credentials: { accessToken: HF_TOKEN }, | |
| repo: { type: TYPE, name: REPO }, | |
| file: { | |
| path: hfPath, | |
| content: fs.createReadStream(localPath), | |
| }, | |
| }); | |
| // ✅ URL RAW | |
| return `https://huggingface.co/datasets/${REPO}/resolve/${res.commit.oid}/${hfPath}`; | |
| } | |