Download src/utils/credentials.ts from oga3oga/Tele-v: direct link, hf CLI and curl.
- Browser
- Download file 7.15 kB
-
https://huggingface.co/spaces/oga3oga/Tele-v/resolve/main/src/utils/credentials.ts
- Command line
-
hf download hf://spaces/oga3oga/Tele-v/src/utils/credentials.ts
-
curl -L -o credentials.ts https://huggingface.co/spaces/oga3oga/Tele-v/resolve/main/src/utils/credentials.ts
7.15 kB
| // Credentials Export/Import Utilities | |
| import { saveAs } from 'file-saver'; | |
| /** | |
| * Extract storage credentials from app settings | |
| * Now supports per-remote URL credentials | |
| */ | |
| export function extractStorageCredentials(settings: any) { | |
| // Extract per-remote credentials (new format) | |
| const remoteCredentials: Record<string, { hfToken: string; hfS3AccessKeyId: string; hfS3SecretAccessKey: string }> = {}; | |
| if (settings.remoteCredentials && typeof settings.remoteCredentials === 'object') { | |
| for (const [url, creds] of Object.entries(settings.remoteCredentials)) { | |
| if (creds && typeof creds === 'object') { | |
| remoteCredentials[url] = { | |
| hfToken: (creds as any).hfToken || '', | |
| hfS3AccessKeyId: (creds as any).hfS3AccessKeyId || '', | |
| hfS3SecretAccessKey: (creds as any).hfS3SecretAccessKey || '', | |
| }; | |
| } | |
| } | |
| } | |
| const credentials = { | |
| // B2/Backblaze credentials | |
| b2KeyId: settings.b2KeyId || '', | |
| b2AppKey: settings.b2AppKey || '', | |
| b2BucketName: settings.b2BucketName || '', | |
| b2Host: settings.b2Host || '', | |
| b2EndpointUrl: settings.b2EndpointUrl || '', | |
| b2Region: settings.b2Region || '', | |
| b2SavedConnections: settings.b2SavedConnections || [], | |
| // Remote storage credentials - per-URL format | |
| remoteUrl: settings.remoteUrl || '', | |
| remoteCredentials: remoteCredentials, | |
| remoteUrlHistory: settings.remoteUrlHistory || [], | |
| // Legacy single remote credentials (for backward compatibility) | |
| remoteHfToken: settings.remoteHfToken || '', | |
| remoteHfS3AccessKeyId: settings.remoteHfS3AccessKeyId || '', | |
| remoteHfS3SecretAccessKey: settings.remoteHfS3SecretAccessKey || '', | |
| // Other credentials | |
| teleboxToken: settings.teleboxToken || '', | |
| teleboxWebToken: settings.teleboxWebToken || '', | |
| // Settings | |
| encodeAutoS3: settings.encodeAutoS3 || false, | |
| uploadDirectToRemote: settings.uploadDirectToRemote || false, | |
| // Metadata | |
| exportedAt: new Date().toISOString(), | |
| appVersion: 'vd-encoder-Tele-v' | |
| }; | |
| return credentials; | |
| } | |
| /** | |
| * Get credentials for a specific remote URL | |
| */ | |
| export function getCredentialsForUrl(remoteUrl: string, settings: any): { hfToken: string; hfS3AccessKeyId: string; hfS3SecretAccessKey: string } { | |
| const url = normalizeRemoteUrl(remoteUrl); | |
| // Check per-URL credentials first | |
| if (settings.remoteCredentials && typeof settings.remoteCredentials === 'object') { | |
| const stored = (settings.remoteCredentials as Record<string, any>)[url]; | |
| if (stored) { | |
| return { | |
| hfToken: stored.hfToken || '', | |
| hfS3AccessKeyId: stored.hfS3AccessKeyId || '', | |
| hfS3SecretAccessKey: stored.hfS3SecretAccessKey || '', | |
| }; | |
| } | |
| } | |
| // Fallback to legacy global credentials | |
| return { | |
| hfToken: settings.remoteHfToken || '', | |
| hfS3AccessKeyId: settings.remoteHfS3AccessKeyId || '', | |
| hfS3SecretAccessKey: settings.remoteHfS3SecretAccessKey || '', | |
| }; | |
| } | |
| /** | |
| * Normalize remote URL for consistent key usage | |
| */ | |
| export function normalizeRemoteUrl(url: string): string { | |
| if (!url) return ''; | |
| try { | |
| // Remove trailing slashes and query parameters for consistent storage | |
| const parsed = new URL(url); | |
| // For HF buckets, normalize to the base bucket URL | |
| if (parsed.hostname.includes('huggingface.co')) { | |
| const pathParts = parsed.pathname.split('/').filter(Boolean); | |
| if (pathParts.length >= 2 && pathParts[0] === 'buckets') { | |
| // Return: https://huggingface.co/buckets/org/bucket | |
| return `https://${parsed.hostname}/buckets/${pathParts[1]}/${pathParts[2]}`; | |
| } | |
| } | |
| // For other URLs, just remove trailing slash | |
| return url.replace(/\/+$/, ''); | |
| } catch { | |
| return url.replace(/\/+$/, ''); | |
| } | |
| } | |
| /** | |
| * Export credentials to a downloadable file | |
| */ | |
| export function exportCredentialsToFile(credentials: any, filename: string = 'storage_credentials.json') { | |
| const blob = new Blob([JSON.stringify(credentials, null, 2)], { | |
| type: 'application/json;charset=utf-8' | |
| }); | |
| saveAs(blob, filename); | |
| } | |
| /** | |
| * Import credentials from a file | |
| */ | |
| export async function importCredentialsFromFile(file: File): Promise<any> { | |
| return new Promise((resolve, reject) => { | |
| const reader = new FileReader(); | |
| reader.onload = (event) => { | |
| try { | |
| const content = event.target?.result as string; | |
| const credentials = JSON.parse(content); | |
| resolve(credentials); | |
| } catch (error) { | |
| reject(new Error('Invalid credentials file format')); | |
| } | |
| }; | |
| reader.onerror = () => { | |
| reject(new Error('Failed to read credentials file')); | |
| }; | |
| reader.readAsText(file); | |
| }); | |
| } | |
| /** | |
| * Merge imported credentials into current settings | |
| * Handles both legacy format and new per-remote format | |
| */ | |
| export function mergeCredentialsIntoSettings(currentSettings: any, importedCredentials: any) { | |
| // Start with current settings | |
| const merged = { ...currentSettings }; | |
| // B2/Backblaze credentials | |
| merged.b2KeyId = importedCredentials.b2KeyId || currentSettings.b2KeyId; | |
| merged.b2AppKey = importedCredentials.b2AppKey || currentSettings.b2AppKey; | |
| merged.b2BucketName = importedCredentials.b2BucketName || currentSettings.b2BucketName; | |
| merged.b2Host = importedCredentials.b2Host || currentSettings.b2Host; | |
| merged.b2EndpointUrl = importedCredentials.b2EndpointUrl || currentSettings.b2EndpointUrl; | |
| merged.b2Region = importedCredentials.b2Region || currentSettings.b2Region; | |
| merged.b2SavedConnections = importedCredentials.b2SavedConnections || currentSettings.b2SavedConnections; | |
| // Remote storage credentials | |
| merged.remoteUrl = importedCredentials.remoteUrl || currentSettings.remoteUrl; | |
| merged.remoteUrlHistory = importedCredentials.remoteUrlHistory || currentSettings.remoteUrlHistory; | |
| // Handle per-remote credentials (new format) | |
| if (importedCredentials.remoteCredentials && typeof importedCredentials.remoteCredentials === 'object') { | |
| merged.remoteCredentials = { | |
| ...(currentSettings.remoteCredentials || {}), | |
| ...importedCredentials.remoteCredentials, | |
| }; | |
| } | |
| // Legacy single remote credentials (for backward compatibility) | |
| // These will be used as fallback if no per-URL credentials exist | |
| merged.remoteHfToken = importedCredentials.remoteHfToken || currentSettings.remoteHfToken; | |
| merged.remoteHfS3AccessKeyId = importedCredentials.remoteHfS3AccessKeyId || currentSettings.remoteHfS3AccessKeyId; | |
| merged.remoteHfS3SecretAccessKey = importedCredentials.remoteHfS3SecretAccessKey || currentSettings.remoteHfS3SecretAccessKey; | |
| // Other credentials | |
| merged.teleboxToken = importedCredentials.teleboxToken || currentSettings.teleboxToken; | |
| merged.teleboxWebToken = importedCredentials.teleboxWebToken || currentSettings.teleboxWebToken; | |
| // Settings | |
| merged.encodeAutoS3 = importedCredentials.encodeAutoS3 !== undefined | |
| ? importedCredentials.encodeAutoS3 | |
| : currentSettings.encodeAutoS3; | |
| merged.uploadDirectToRemote = importedCredentials.uploadDirectToRemote !== undefined | |
| ? importedCredentials.uploadDirectToRemote | |
| : currentSettings.uploadDirectToRemote; | |
| return merged; | |
| } | |