Files
MajoorWaldi-ComfyUI-Majoor-…/js/utils/fastUpload.js
T
2026-01-08 02:21:14 +01:00

57 lines
2.0 KiB
JavaScript

/**
* Fast upload utilities for images and videos
*/
/**
* Fast upload image to ComfyUI input directory using native endpoint
* @param {File} file - The image file to upload
* @returns {Promise<Object>} - Response from the upload endpoint
*/
export async function fastUploadImageToInput(file) {
const fd = new FormData();
fd.append("image", file, file.name);
const res = await fetch("/upload/image", { method: "POST", body: fd });
if (!res.ok) throw new Error(`Upload failed: ${res.status}`);
return await res.json(); // { name: "file.png", ... }
}
/**
* Fast upload file to ComfyUI input directory using custom endpoint
* @param {File} file - The file to upload (for video, etc.)
* @param {string} purpose - Purpose of upload (e.g. "node_drop")
* @returns {Promise<Object>} - Response from the upload endpoint
*/
export async function fastUploadFileToInput(file, purpose = null) {
const fd = new FormData();
fd.append("file", file, file.name);
let url = "/mjr/am/upload_input";
if (purpose) {
url += `?purpose=${encodeURIComponent(purpose)}`;
}
const res = await fetch(url, { method: "POST", body: fd });
if (!res.ok) throw new Error(`Upload failed: ${res.status}`);
return await res.json(); // { name: "file.mp4", ... }
}
/**
* Upload a file with appropriate method based on type
* @param {File} file - The file to upload
* @param {string} purpose - Purpose of upload (e.g. "node_drop")
* @returns {Promise<Object>} - Response from the upload endpoint
*/
export async function smartUploadToInput(file, purpose = null) {
// Check if it's an image file
const imageTypes = ['image/jpeg', 'image/jpg', 'image/png', 'image/gif', 'image/webp', 'image/bmp', 'image/svg+xml'];
const isImage = imageTypes.includes(file.type.toLowerCase());
if (isImage) {
// Use native ComfyUI endpoint for images
return await fastUploadImageToInput(file);
} else {
// Use custom endpoint for other files (videos, etc.)
return await fastUploadFileToInput(file, purpose);
}
}