File size: 7,147 Bytes
b246ace da1ea85 b246ace da1ea85 b246ace da1ea85 b246ace da1ea85 b246ace da1ea85 b246ace da1ea85 b246ace da1ea85 b246ace da1ea85 b246ace da1ea85 b246ace | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | // 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;
}
|