MCPcopy
hub / github.com/Anil-matcha/Open-Generative-AI / uploadFile

Function uploadFile

packages/studio/src/muapi.js:193–241  ·  view source on GitHub ↗
(apiKey, file, onProgress)

Source from the content-addressed store, hash-verified

191}
192
193export function uploadFile(apiKey, file, onProgress) {
194 return new Promise((resolve, reject) => {
195 const url = `${BASE_URL}/api/v1/upload_file`;
196 const formData = new FormData();
197 formData.append('file', file);
198
199 const xhr = new XMLHttpRequest();
200 xhr.open('POST', url);
201 xhr.setRequestHeader('x-api-key', apiKey);
202
203 if (onProgress) {
204 xhr.upload.onprogress = (event) => {
205 if (event.lengthComputable) {
206 const percentComplete = Math.round((event.loaded / event.total) * 100);
207 onProgress(percentComplete);
208 }
209 };
210 }
211
212 xhr.onload = () => {
213 if (xhr.status >= 200 && xhr.status < 300) {
214 try {
215 const data = JSON.parse(xhr.responseText);
216 const fileUrl = data.url || data.file_url || data.data?.url;
217 if (!fileUrl) {
218 reject(new Error('No URL returned from file upload'));
219 } else {
220 resolve(fileUrl);
221 }
222 } catch (e) {
223 reject(new Error('Failed to parse upload response'));
224 }
225 } else {
226 let detail = xhr.statusText;
227 try {
228 const errObj = JSON.parse(xhr.responseText);
229 detail = errObj.detail || detail;
230 } catch (e) {
231 // fallback to statusText
232 }
233 notifyAuthRequired(xhr.status, detail);
234 reject(new Error(`File upload failed: ${xhr.status} - ${detail}`));
235 }
236 };
237
238 xhr.onerror = () => reject(new Error('Network error during file upload'));
239 xhr.send(formData);
240 });
241}
242
243export async function getUserBalance(apiKey) {
244 const response = await fetch(`${BASE_URL}/api/v1/account/balance`, {

Callers 14

processDroppedImageFunction · 0.90
processDroppedVideoFunction · 0.90
handleImageFileChangeFunction · 0.90
handleEndImageFileChangeFunction · 0.90
handleVideoFileChangeFunction · 0.90
handleFileChangeFunction · 0.90
processDroppedImagesFunction · 0.90
LipSyncStudioFunction · 0.90
handleImageUploadFunction · 0.90
handleUploadFunction · 0.90
AudioStudioFunction · 0.90
ClippingStudioFunction · 0.90

Calls 1

notifyAuthRequiredFunction · 0.85

Tested by

no test coverage detected