MCPcopy Create free account
hub / github.com/Hello-Application-XH/HelloGML / uploadFile

Function uploadFile

src/chat.ts:719–752  ·  view source on GitHub ↗
(fileUrl: string, refreshToken: string, isVideoImage = false)

Source from the content-addressed store, hash-verified

717}
718
719async function uploadFile(fileUrl: string, refreshToken: string, isVideoImage = false) {
720 await checkFileUrl(fileUrl);
721 let filename: string, fileData: ArrayBuffer, mimeType: string | null = null;
722 if (isBASE64Data(fileUrl)) {
723 mimeType = extractBASE64DataFormat(fileUrl);
724 const ext = mimeType ? getExtension(mimeType) : "bin";
725 filename = `${uuid()}.${ext || "bin"}`;
726 const base64Data = removeBASE64DataHeader(fileUrl);
727 fileData = Uint8Array.from(atob(base64Data), (c) => c.charCodeAt(0)).buffer;
728 } else {
729 filename = basename(fileUrl);
730 const response = await fetch(fileUrl);
731 fileData = await response.arrayBuffer();
732 }
733 mimeType = mimeType || getMimeType(filename);
734 // 注意:CF Worker 不支持 sharp,跳过图片 resize
735 const formData = new FormData();
736 formData.append("file", new Blob([fileData], { type: mimeType }), filename);
737 const token = await acquireToken(refreshToken);
738 const uploadUrl = isVideoImage
739 ? "https://chatglm.cn/chatglm/video-api/v1/static/upload"
740 : "https://chatglm.cn/chatglm/backend-api/assistant/file_upload";
741 const response = await fetch(uploadUrl, {
742 method: "POST",
743 headers: {
744 Authorization: `Bearer ${token}`,
745 Referer: isVideoImage ? "https://chatglm.cn/video" : "https://chatglm.cn/",
746 ...getHeaders(),
747 },
748 body: formData,
749 });
750 const uploadResult = await checkResult(response, refreshToken);
751 return uploadResult.result;
752}
753
754async function receiveStream(model: string, readableStream: ReadableStream, tools?: any[]): Promise<any> {
755 return new Promise((resolve, reject) => {

Callers 3

createCompletionFunction · 0.85
createCompletionStreamFunction · 0.85
generateVideosFunction · 0.85

Calls 12

isBASE64DataFunction · 0.90
extractBASE64DataFormatFunction · 0.90
getExtensionFunction · 0.90
uuidFunction · 0.90
removeBASE64DataHeaderFunction · 0.90
basenameFunction · 0.90
getMimeTypeFunction · 0.90
checkFileUrlFunction · 0.85
fetchFunction · 0.85
acquireTokenFunction · 0.85
getHeadersFunction · 0.85
checkResultFunction · 0.85

Tested by

no test coverage detected