| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- import { type ClassValue, clsx } from 'clsx';
- export function cn(...inputs: ClassValue[]) {
- return clsx(inputs);
- }
- export function formatFileSize(bytes: number): string {
- if (bytes === 0) return '0 Bytes';
- const k = 1024;
- const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
- const i = Math.floor(Math.log(bytes) / Math.log(k));
- return Math.round(bytes / Math.pow(k, i) * 100) / 100 + ' ' + sizes[i];
- }
- export function downloadImage(url: string, filename: string = 'generated-image.png') {
- const link = document.createElement('a');
- link.href = url;
- link.download = filename;
- document.body.appendChild(link);
- link.click();
- document.body.removeChild(link);
- }
- // Download image with authentication support
- export async function downloadAuthenticatedImage(
- imageUrl: string,
- filename: string = 'generated-image.png',
- authToken?: string,
- unixUser?: string
- ): Promise<void> {
- try {
- const headers: Record<string, string> = {};
- // Add authentication headers if provided
- if (unixUser) {
- headers['X-Unix-User'] = unixUser;
- } else if (authToken) {
- headers['Authorization'] = `Bearer ${authToken}`;
- }
- const response = await fetch(imageUrl, { headers });
- if (!response.ok) {
- throw new Error(`Failed to download image: ${response.statusText}`);
- }
- const blob = await response.blob();
- const url = window.URL.createObjectURL(blob);
- downloadImage(url, filename);
- // Clean up the object URL
- window.URL.revokeObjectURL(url);
- } catch (error) {
- console.error('Error downloading authenticated image:', error);
- throw error;
- }
- }
- export function fileToBase64(file: File): Promise<string> {
- return new Promise((resolve, reject) => {
- const reader = new FileReader();
- reader.readAsDataURL(file);
- reader.onload = () => resolve(reader.result as string);
- reader.onerror = error => reject(error);
- });
- }
|