File size: 1,374 Bytes
9ada4bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type { Client } from "..";
import { BROKEN_CONNECTION_MSG, UPLOAD_URL } from "../constants";
import type { UploadResponse } from "../types";

export async function upload_files(
	this: Client,
	root_url: string,
	files: (Blob | File)[],
	upload_id?: string
): Promise<UploadResponse> {
	const headers: {
		Authorization?: string;
	} = {};
	if (this?.options?.hf_token) {
		headers.Authorization = `Bearer ${this.options.hf_token}`;
	}

	const chunkSize = 1000;
	const uploadResponses = [];
	let response: Response;

	for (let i = 0; i < files.length; i += chunkSize) {
		const chunk = files.slice(i, i + chunkSize);
		const formData = new FormData();
		chunk.forEach((file) => {
			formData.append("files", file);
		});
		try {
			const upload_url = upload_id
				? `${root_url}/${UPLOAD_URL}?upload_id=${upload_id}`
				: `${root_url}/${UPLOAD_URL}`;

			response = await this.fetch(upload_url, {
				method: "POST",
				body: formData,
				headers,
				credentials: "include"
			});
		} catch (e) {
			throw new Error(BROKEN_CONNECTION_MSG + (e as Error).message);
		}
		if (!response.ok) {
			const error_text = await response.text();
			return { error: `HTTP ${response.status}: ${error_text}` };
		}
		const output: UploadResponse["files"] = await response.json();
		if (output) {
			uploadResponses.push(...output);
		}
	}
	return { files: uploadResponses };
}