import { useMutation } from "@tanstack/react-query"; import { BlossomUploader } from '@nostrify/nostrify/uploaders'; import { useCurrentUser } from "./useCurrentUser"; /** * Two Blossom hosts. `BlossomUploader` signs ONE auth event and PUTs to every * server in parallel, resolving with the first success — so the same bytes * normally land on both from a single signature (no double Amber prompt). We then * record a mirror URL on the returned tags so photo redundancy is explicit in the * event; the read side also derives it from the sha256 (lib/imageFallback). */ const UPLOAD_SERVERS = ['https://blossom.primal.net/', 'https://blossom.band/']; function hostOf(url: string): string { try { return new URL(url).hostname; } catch { return ''; } } export function useUploadFile() { const { user } = useCurrentUser(); return useMutation({ mutationFn: async (file: File) => { if (!user) { throw new Error('Must be logged in to upload files'); } const uploader = new BlossomUploader({ servers: UPLOAD_SERVERS, signer: user.signer, }); const tags = await uploader.upload(file); // Remember the mirror copy of the same blob. Blossom is content-addressed, // so `/` is the same file. const primaryUrl = tags[0]?.[1]; const sha = tags.find(([name]) => name === 'x')?.[1]; if (primaryUrl && sha) { const primaryHost = hostOf(primaryUrl); const mirror = UPLOAD_SERVERS.find((s) => hostOf(s) !== primaryHost); if (mirror) tags.push(['fallback', `${mirror.replace(/\/+$/, '')}/${sha}`]); } return tags; }, }); }