Spaces:
Sleeping
Sleeping
Create server/upload.ts
Browse files- server/upload.ts +31 -0
server/upload.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import fs from "fs";
|
| 2 |
+
import { uploadFile } from "@huggingface/hub";
|
| 3 |
+
import type { RepoType } from "@huggingface/hub";
|
| 4 |
+
|
| 5 |
+
const HF_TOKEN = process.env.HF_TOKEN!;
|
| 6 |
+
const REPO = "TwanAPI/DataTwan";
|
| 7 |
+
const TYPE: RepoType = "dataset";
|
| 8 |
+
|
| 9 |
+
export async function uploadToHF(
|
| 10 |
+
localPath: string,
|
| 11 |
+
originalName: string
|
| 12 |
+
) {
|
| 13 |
+
// ✅ PATH AN TOÀN
|
| 14 |
+
if (originalName.includes("..") || originalName.includes("/")) {
|
| 15 |
+
throw new Error("Invalid filename");
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
const hfPath = `uploads/${Date.now()}-${originalName}`;
|
| 19 |
+
|
| 20 |
+
const res = await uploadFile({
|
| 21 |
+
credentials: { accessToken: HF_TOKEN },
|
| 22 |
+
repo: { type: TYPE, name: REPO },
|
| 23 |
+
file: {
|
| 24 |
+
path: hfPath,
|
| 25 |
+
content: fs.createReadStream(localPath),
|
| 26 |
+
},
|
| 27 |
+
});
|
| 28 |
+
|
| 29 |
+
// ✅ URL RAW
|
| 30 |
+
return `https://huggingface.co/datasets/${REPO}/resolve/${res.commit.oid}/${hfPath}`;
|
| 31 |
+
}
|