|
| 1 | +import { readable } from "svelte/store"; |
| 2 | +import { getDownloadURL, list, ref } from "firebase/storage"; |
| 3 | + |
| 4 | +import type { |
| 5 | + StorageReference, |
| 6 | + FirebaseStorage, |
| 7 | + ListResult, |
| 8 | +} from "firebase/storage"; |
| 9 | + |
| 10 | +const defaultListResult: ListResult = { |
| 11 | + prefixes: [], |
| 12 | + items: [], |
| 13 | +}; |
| 14 | + |
| 15 | +interface StorageListStore { |
| 16 | + subscribe: (cb: (value: ListResult) => void) => void | (() => void); |
| 17 | + reference: StorageReference | null; |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * @param {FirebaseStorage} storage firebase storage instance |
| 22 | + * @param {string|StorageReference} reference file or storage item path or reference |
| 23 | + * @param {{prefixes:[], items:[]}} startWith optional default data |
| 24 | + * @returns a store with the list result |
| 25 | + */ |
| 26 | +export function storageListStore( |
| 27 | + storage: FirebaseStorage, |
| 28 | + reference: string | StorageReference, |
| 29 | + startWith: ListResult = defaultListResult |
| 30 | +): StorageListStore { |
| 31 | + |
| 32 | + // Fallback for SSR |
| 33 | + if (!globalThis.window) { |
| 34 | + const { subscribe } = readable(startWith); |
| 35 | + return { |
| 36 | + subscribe, |
| 37 | + reference: null, |
| 38 | + }; |
| 39 | + } |
| 40 | + |
| 41 | + // Fallback for missing SDK |
| 42 | + if (!storage) { |
| 43 | + console.warn( |
| 44 | + "Cloud Storage is not initialized. Are you missing FirebaseApp as a parent component?" |
| 45 | + ); |
| 46 | + const { subscribe } = readable(defaultListResult); |
| 47 | + return { |
| 48 | + subscribe, |
| 49 | + reference: null, |
| 50 | + }; |
| 51 | + } |
| 52 | + |
| 53 | + const storageRef = typeof reference === "string" ? ref(storage, reference) : reference; |
| 54 | + |
| 55 | + const { subscribe } = readable(startWith, (set) => { |
| 56 | + list(storageRef).then((snapshot) => { |
| 57 | + set(snapshot); |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + return { |
| 62 | + subscribe, |
| 63 | + reference: storageRef, |
| 64 | + }; |
| 65 | +} |
| 66 | + |
| 67 | +interface DownloadUrlStore { |
| 68 | + subscribe: (cb: (value: string | null) => void) => void | (() => void); |
| 69 | + reference: StorageReference | null; |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * @param {FirebaseStorage} storage firebase storage instance |
| 74 | + * @param {string|StorageReference} reference file or storage item path or reference |
| 75 | + * @param {null} startWith optional default data |
| 76 | + * @returns a store with the list result |
| 77 | + */ |
| 78 | +export function downloadUrlStore( |
| 79 | + storage: FirebaseStorage, |
| 80 | + reference: string | StorageReference, |
| 81 | + startWith: string | null = null |
| 82 | +): DownloadUrlStore { |
| 83 | + |
| 84 | + // Fallback for SSR |
| 85 | + if (!globalThis.window) { |
| 86 | + const { subscribe } = readable(startWith); |
| 87 | + return { |
| 88 | + subscribe, |
| 89 | + reference: null, |
| 90 | + }; |
| 91 | + } |
| 92 | + |
| 93 | + // Fallback for missing SDK |
| 94 | + if (!storage) { |
| 95 | + console.warn( |
| 96 | + "Cloud Storage is not initialized. Are you missing FirebaseApp as a parent component?" |
| 97 | + ); |
| 98 | + const { subscribe } = readable(null); |
| 99 | + return { |
| 100 | + subscribe, |
| 101 | + reference: null, |
| 102 | + }; |
| 103 | + } |
| 104 | + |
| 105 | + const storageRef = typeof reference === "string" ? ref(storage, reference) : reference; |
| 106 | + |
| 107 | + const { subscribe } = readable(startWith, (set) => { |
| 108 | + getDownloadURL(storageRef).then((snapshot) => { |
| 109 | + set(snapshot); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + return { |
| 114 | + subscribe, |
| 115 | + reference: storageRef, |
| 116 | + }; |
| 117 | +} |
| 118 | + |
0 commit comments