testupload / server /upload.ts
Twan07's picture
Create server/upload.ts
d448990 verified
raw
history blame
819 Bytes
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}`;
}