refactor shared source URL detection

This commit is contained in:
Dariusz L
2026-08-07 04:12:57 +02:00
parent c168157727
commit 8a8e2bc3a1
5 changed files with 104 additions and 55 deletions
+10
View File
@@ -28,6 +28,16 @@ export function normalizeSourceKey(value, { trim = true } = {}) {
return (trim ? text.trim() : text).toLowerCase().replace(/-/g, '_');
}
export function getSourceKeyFromText(value = '') {
const text = String(value || '').toLowerCase();
if (!text) return '';
if (text.includes('huggingface') || text.includes('hugging face')) return 'huggingface';
if (text.includes('civarchive') || text.includes('civ archive')) return 'civarchive';
if (text.includes('civitai') || text.includes('civitai.red')) return 'civitai';
if (text.includes('lora_manager_archive') || text.includes('lora archive')) return 'lora_manager_archive';
return '';
}
export function normalizeSourceList(sources = []) {
return new Set(
(Array.isArray(sources) ? sources : [sources])
+26
View File
@@ -2,6 +2,32 @@
* URL Utilities for Model Resolver
*/
import { getSourceKeyFromText } from './source_labels.js';
/**
* Resolve a supported provider key from a URL host.
* @param {string} value
* @param {{fallbackToText?: boolean}} options
* @returns {string}
*/
export function getSourceKeyFromUrl(value, { fallbackToText = false } = {}) {
const url = String(value || '').trim();
if (!url) return '';
try {
const baseUrl = globalThis.location?.origin || 'http://localhost';
const parsed = new URL(url, baseUrl);
const host = parsed.hostname.toLowerCase().replace(/^www\./, '');
if (host === 'huggingface.co' || host.endsWith('.huggingface.co')) return 'huggingface';
if (host === 'civarchive.com' || host.endsWith('.civarchive.com')) return 'civarchive';
if (host === 'civitai.com' || host.endsWith('.civitai.com') || host === 'civitai.red' || host.endsWith('.civitai.red')) return 'civitai';
} catch {
// Preserve the caller's optional text fallback for malformed URLs.
}
return fallbackToText ? getSourceKeyFromText(url) : '';
}
/**
* Parse a Hugging Face blob/resolve file URL into repository coordinates.
* @param {string} value