-- Create storage bucket for book covers
-- This will store uploaded cover images for books
-- Create the book-covers storage bucket
INSERT INTO storage.buckets (id, name, public, file_size_limit, allowed_mime_types)
VALUES (
'book-covers',
'book-covers',
true,
5242880, -- 5MB limit
ARRAY['image/jpeg', 'image/png', 'image/webp', 'image/gif']
)
ON CONFLICT (id) DO NOTHING;
-- Enable RLS on the storage.objects table for our bucket
-- Allow users to see all public cover images
CREATE POLICY "Public book covers are viewable by everyone" ON storage.objects
FOR SELECT USING (bucket_id = 'book-covers');
-- Allow authenticated users to upload cover images
CREATE POLICY "Users can upload book covers" ON storage.objects
FOR INSERT WITH CHECK (
bucket_id = 'book-covers'
AND auth.role() = 'authenticated'
);
-- Allow users to update their own uploaded images
CREATE POLICY "Users can update own book covers" ON storage.objects
FOR UPDATE USING (
bucket_id = 'book-covers'
AND auth.uid()::text = (storage.foldername(name))[1]
) WITH CHECK (
bucket_id = 'book-covers'
AND auth.uid()::text = (storage.foldername(name))[1]
);
-- Allow users to delete their own uploaded images
CREATE POLICY "Users can delete own book covers" ON storage.objects
FOR DELETE USING (
bucket_id = 'book-covers'
AND auth.uid()::text = (storage.foldername(name))[1]
);