photo.ts 805 B

123456789101112131415161718192021222324252627282930
  1. import { ref, getDownloadURL, listAll } from "firebase/storage";
  2. import { storage } from "../firebase";
  3. export async function getPhotoUrl(filePath: string): Promise<string> {
  4. const storageRef = ref(storage, filePath);
  5. try {
  6. const url = await getDownloadURL(storageRef);
  7. return url;
  8. } catch (error) {
  9. console.error("Error fetching photo URL:", error);
  10. throw error;
  11. }
  12. }
  13. export async function listPhotos(folderPath: string): Promise<string[]> {
  14. const folderRef = ref(storage, folderPath);
  15. try {
  16. const res = await listAll(folderRef);
  17. const urlPromises = res.items.map((itemRef) => getDownloadURL(itemRef));
  18. const urls = await Promise.all(urlPromises);
  19. return urls;
  20. } catch (error) {
  21. console.error("Error listing photos:", error);
  22. throw error;
  23. }
  24. }