Spaces:
Sleeping

fx-craft / supabase_storage_buckets.sql
felix-fx's picture
Initial setup for fx-craft website
9694b21 verified
Raw History Blame Contribute Delete
5.3 kB
-- =====================================================
-- إنشاء Storage Buckets في Supabase لرفع ملفات الإضافات
-- قم بتشغيل هذا SQL في Supabase Dashboard > SQL Editor
-- =====================================================
-- 1. إنشاء Bucket للإضافات (mods)
INSERT INTO storage.buckets (id, name, public, file_size_limit, allowed_mime_types)
VALUES (
'mods',
'mods',
true,
104857600, -- 100 MB
ARRAY[
'application/zip',
'application/java-archive',
'application/octet-stream',
'application/x-zip-compressed'
]
)
ON CONFLICT (id) DO UPDATE SET
public = true,
file_size_limit = 104857600;
-- 2. إنشاء Bucket للصور المصغرة (thumbnails)
INSERT INTO storage.buckets (id, name, public, file_size_limit, allowed_mime_types)
VALUES (
'thumbnails',
'thumbnails',
true,
5242880, -- 5 MB
ARRAY[
'image/jpeg',
'image/png',
'image/webp',
'image/gif'
]
)
ON CONFLICT (id) DO UPDATE SET
public = true,
file_size_limit = 5242880;
-- 3. إنشاء Bucket للقطات الشاشة (screenshots)
INSERT INTO storage.buckets (id, name, public, file_size_limit, allowed_mime_types)
VALUES (
'screenshots',
'screenshots',
true,
10485760, -- 10 MB
ARRAY[
'image/jpeg',
'image/png',
'image/webp'
]
)
ON CONFLICT (id) DO UPDATE SET
public = true,
file_size_limit = 10485760;
-- =====================================================
-- إعداد سياسات RLS للـ Storage Buckets
-- =====================================================
-- سياسات Bucket: mods
DROP POLICY IF EXISTS "Public can read mods" ON storage.objects;
CREATE POLICY "Public can read mods"
ON storage.objects FOR SELECT
USING (bucket_id = 'mods');
DROP POLICY IF EXISTS "Authenticated users can upload mods" ON storage.objects;
CREATE POLICY "Authenticated users can upload mods"
ON storage.objects FOR INSERT
WITH CHECK (bucket_id = 'mods' AND auth.role() = 'authenticated');
DROP POLICY IF EXISTS "Users can delete own mods" ON storage.objects;
CREATE POLICY "Users can delete own mods"
ON storage.objects FOR DELETE
USING (bucket_id = 'mods' AND auth.uid()::text = (storage.foldername(name))[1]);
-- سياسات Bucket: thumbnails
DROP POLICY IF EXISTS "Public can read thumbnails" ON storage.objects;
CREATE POLICY "Public can read thumbnails"
ON storage.objects FOR SELECT
USING (bucket_id = 'thumbnails');
DROP POLICY IF EXISTS "Authenticated users can upload thumbnails" ON storage.objects;
CREATE POLICY "Authenticated users can upload thumbnails"
ON storage.objects FOR INSERT
WITH CHECK (bucket_id = 'thumbnails' AND auth.role() = 'authenticated');
-- سياسات Bucket: screenshots
DROP POLICY IF EXISTS "Public can read screenshots" ON storage.objects;
CREATE POLICY "Public can read screenshots"
ON storage.objects FOR SELECT
USING (bucket_id = 'screenshots');
DROP POLICY IF EXISTS "Authenticated users can upload screenshots" ON storage.objects;
CREATE POLICY "Authenticated users can upload screenshots"
ON storage.objects FOR INSERT
WITH CHECK (bucket_id = 'screenshots' AND auth.role() = 'authenticated');
-- =====================================================
-- التأكد من أن جدول mods يستخدم user_id (وليس developer_id)
-- =====================================================
ALTER TABLE IF EXISTS public.mods
ADD COLUMN IF NOT EXISTS user_id UUID REFERENCES auth.users(id) ON DELETE SET NULL;
-- إصلاح سياسات RLS لجدول mods (تستخدم user_id)
ALTER TABLE public.mods ENABLE ROW LEVEL SECURITY;
DROP POLICY IF EXISTS "Mods are viewable by everyone" ON public.mods;
CREATE POLICY "Mods are viewable by everyone"
ON public.mods FOR SELECT USING (true);
DROP POLICY IF EXISTS "Users can insert their own mods" ON public.mods;
CREATE POLICY "Users can insert their own mods"
ON public.mods FOR INSERT
WITH CHECK (auth.uid()::text = user_id::text);
DROP POLICY IF EXISTS "Users can update their own mods" ON public.mods;
CREATE POLICY "Users can update their own mods"
ON public.mods FOR UPDATE
USING (auth.uid()::text = user_id::text);
DROP POLICY IF EXISTS "Users can delete their own mods" ON public.mods;
CREATE POLICY "Users can delete their own mods"
ON public.mods FOR DELETE
USING (auth.uid()::text = user_id::text);
-- =====================================================
-- جدول سجل التحميلات (إذا لم يكن موجوداً)
-- =====================================================
CREATE TABLE IF NOT EXISTS public.download_history (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
mod_id UUID REFERENCES public.mods(id) ON DELETE CASCADE,
user_id UUID REFERENCES auth.users(id) ON DELETE SET NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
ALTER TABLE public.download_history ENABLE ROW LEVEL SECURITY;
DROP POLICY IF EXISTS "Anyone can insert download history" ON public.download_history;
CREATE POLICY "Anyone can insert download history"
ON public.download_history FOR INSERT WITH CHECK (true);
DROP POLICY IF EXISTS "Users can view own download history" ON public.download_history;
CREATE POLICY "Users can view own download history"
ON public.download_history FOR SELECT
USING (auth.uid() = user_id OR auth.role() = 'service_role');