utils.ts 974 B

12345678910111213141516171819202122232425262728293031
  1. import { type ClassValue, clsx } from 'clsx';
  2. export function cn(...inputs: ClassValue[]) {
  3. return clsx(inputs);
  4. }
  5. export function formatFileSize(bytes: number): string {
  6. if (bytes === 0) return '0 Bytes';
  7. const k = 1024;
  8. const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
  9. const i = Math.floor(Math.log(bytes) / Math.log(k));
  10. return Math.round(bytes / Math.pow(k, i) * 100) / 100 + ' ' + sizes[i];
  11. }
  12. export function downloadImage(dataUrl: string, filename: string = 'generated-image.png') {
  13. const link = document.createElement('a');
  14. link.href = dataUrl;
  15. link.download = filename;
  16. document.body.appendChild(link);
  17. link.click();
  18. document.body.removeChild(link);
  19. }
  20. export function fileToBase64(file: File): Promise<string> {
  21. return new Promise((resolve, reject) => {
  22. const reader = new FileReader();
  23. reader.readAsDataURL(file);
  24. reader.onload = () => resolve(reader.result as string);
  25. reader.onerror = error => reject(error);
  26. });
  27. }