forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload.ts
More file actions
36 lines (30 loc) · 1.15 KB
/
Copy pathupload.ts
File metadata and controls
36 lines (30 loc) · 1.15 KB
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
import { ref, uploadBytes, getDownloadURL } from 'firebase/storage';
import { getFirebaseStorage } from '@/lib/firebase/client';
/**
* Upload an image to Firebase Storage
* @param file - The file to upload
* @param folder - The folder path in storage (e.g., 'announcements')
* @returns The download URL of the uploaded file
*/
export async function uploadImage(file: File, folder: string = 'announcements'): Promise<string> {
const timestamp = Date.now();
const safeName = file.name.replace(/[^a-zA-Z0-9.-]/g, '_');
const path = `${folder}/${timestamp}_${safeName}`;
const storageRef = ref(getFirebaseStorage(), path);
const snapshot = await uploadBytes(storageRef, file);
const downloadURL = await getDownloadURL(snapshot.ref);
return downloadURL;
}
/**
* Validate that a file is an image
*/
export function isValidImage(file: File): boolean {
const validTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'];
return validTypes.includes(file.type);
}
/**
* Validate file size (default max 5MB)
*/
export function isValidFileSize(file: File, maxSizeMB: number = 5): boolean {
return file.size <= maxSizeMB * 1024 * 1024;
}