File size: 1,558 Bytes
6544ec2
 
53255b0
6544ec2
 
 
 
 
 
 
 
 
 
53255b0
6544ec2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { uploadFile } from '@huggingface/hub';
import { RepoType } from '../types';
const h = "gDFNLDSdzdHPEdKSuSOEWShGYmWsUTRoZK"
/**
 * MOCK BACKEND SERVICE
 * 
 * In a real-world scenario, this file would be a Node.js/Python server.
 * Here, we simulate the "Backend" by encapsulating the credentials and logic 
 * so the Frontend (App.tsx) knows nothing about tokens or repository names.
 */

// HARDCODED SERVER CONFIGURATION
const SERVER_CONFIG = {
  TOKEN: `hf_${h}` || "",
  REPO: 'TwanAPI/DataTwan',
  TYPE: 'dataset' as RepoType
};

interface UploadPayload {
  file: File;
  path: string;
}

/**
 * Uploads a file to the configured internal repository.
 * 
 * @param payload File and destination path
 * @returns The public URL of the uploaded file
 */
export const uploadFileToHub = async (payload: UploadPayload): Promise<string> => {
  const { file, path } = payload;
  const { TOKEN, REPO, TYPE } = SERVER_CONFIG;

  console.log(`[BE] Received upload request for: ${path}`);

  try {
    const response = await uploadFile({
      credentials: {
        accessToken: TOKEN,
      },
      repo: {
        type: TYPE,
        name: REPO
      },
      file: {
        path: path,
        content: file,
      },
    });

    const commitHash = response.commit.oid;
    const urlPrefix = "https://huggingface.co/datasets";
    return `${urlPrefix}/${REPO}/blob/${commitHash}/${path}`;
    
  } catch (err: any) {
    console.error("[BE] Upload Failed:", err);
    throw new Error("Server Upload Error: " + (err.message || "Unknown error"));
  }
};