5180 fichiers 38.5 Mo 0 téléchargements 270 vues mis à jour il y a 5 heures ui
nitro-v36-with-renderer-v36 / src / components / furni-editor / furniAssetPresence.ts
furniAssetPresence.ts 2.2 Ko Télécharger le fichier
import { GetConfiguration } from '@octane/renderer';

export type AssetPresence = 'present' | 'missing' | 'unknown';

export interface AssetPresenceReport {
    icon: AssetPresence;
    bundle: AssetPresence;
    iconUrl: string;
    bundleUrl: string;
}

// The renderer builds furni asset URLs from two configuration templates with a
// %libname% placeholder (and %param% for coloured icons). Read them the same
// way and probe the files, so a missing bundle explains an empty preview
// before anyone digs through the asset tree.
export const assetUrlsFor = (classname: string): { iconUrl: string; bundleUrl: string } => {
    const libname = classname.split('*')[0].trim();
    const config = typeof GetConfiguration === 'function' ? GetConfiguration() : null;
    const read = (key: string): string => {
        const value = typeof config?.getValue === 'function' ? config.getValue<string>(key) : '';
        return typeof value === 'string' ? value : '';
    };

    const fill = (template: string) => (template && libname ? template.replace(/%libname%/gi, libname).replace(/%param%/gi, '') : '');

    return { iconUrl: fill(read('furni.asset.icon.url')), bundleUrl: fill(read('furni.asset.url')) };
};

const probe = async (url: string, signal: AbortSignal): Promise<AssetPresence> => {
    if (!url || typeof fetch !== 'function') return 'unknown';
    try {
        const head = await fetch(url, { method: 'HEAD', signal });
        if (head.ok) return 'present';
        if (head.status === 404) return 'missing';
        // A server that refuses HEAD answers with the real status on GET.
        if (head.status === 405 || head.status === 501) {
            const get = await fetch(url, { method: 'GET', signal });
            return get.ok ? 'present' : get.status === 404 ? 'missing' : 'unknown';
        }
        return 'unknown';
    } catch {
        return 'unknown';
    }
};

export const checkAssetPresence = async (classname: string, signal: AbortSignal): Promise<AssetPresenceReport> => {
    const { iconUrl, bundleUrl } = assetUrlsFor(classname);
    const [icon, bundle] = await Promise.all([probe(iconUrl, signal), probe(bundleUrl, signal)]);
    return { icon, bundle, iconUrl, bundleUrl };
};
15Sujets
1Dépôts
20Membres
2En ligne
Profile
Inscrit
Dernière visite
Messages
0
Votes positifs
0
Votes négatifs
0

Nous utilisons des cookies pour assurer le bon fonctionnement du site et améliorer votre expérience. en savoir plus

Autoriser