Bin
2025-12-16 9e0b2ba2c317b1a86212f24cbae3195ad1f3dbfa
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
import { API } from "apps/labelstudio/src/providers/ApiProvider";
 
export const importFiles = async ({
  files,
  body,
  project,
  onUploadStart,
  onUploadFinish,
  onFinish,
  onError,
  dontCommitToProject,
}: {
  files: { name: string }[];
  body: Record<string, any> | FormData;
  project: APIProject;
  onUploadStart?: (files: { name: string }[]) => void;
  onUploadFinish?: (files: { name: string }[]) => void;
  onFinish?: (response: any) => void;
  onError?: (response: any) => void;
  dontCommitToProject?: boolean;
}) => {
  onUploadStart?.(files);
 
  const query = dontCommitToProject ? { commit_to_project: "false" } : {};
 
  const contentType =
    body instanceof FormData
      ? "multipart/form-data" // usual multipart for usual files
      : "application/x-www-form-urlencoded"; // chad urlencoded for URL uploads
  const res = await API.invoke(
    "importFiles",
    { pk: project.id, ...query },
    { headers: { "Content-Type": contentType }, body },
  );
 
  if (res && !res.error) {
    await onFinish?.(res);
  } else {
    onError?.(res?.response);
  }
 
  onUploadFinish?.(files);
};