File size: 819 Bytes
d448990
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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}`;
}