|
| 1 | +import { execFileSync } from "node:child_process"; |
| 2 | +import { readdirSync, readFileSync, statSync } from "node:fs"; |
| 3 | +import path from "node:path"; |
| 4 | +import { fileURLToPath } from "node:url"; |
| 5 | + |
| 6 | +import { createClient } from "@supabase/supabase-js"; |
| 7 | + |
| 8 | +const __filename = fileURLToPath(import.meta.url); |
| 9 | +const __dirname = path.dirname(__filename); |
| 10 | +const repoRoot = path.resolve(__dirname, ".."); |
| 11 | + |
| 12 | +const bucketConfigs = { |
| 13 | + avatars: { |
| 14 | + public: true, |
| 15 | + fileSizeLimit: "10MiB", |
| 16 | + allowedMimeTypes: ["image/png", "image/jpeg", "image/webp"], |
| 17 | + sourceDir: path.join(repoRoot, "supabase", "storage", "avatars"), |
| 18 | + }, |
| 19 | + listing_avatars: { |
| 20 | + public: true, |
| 21 | + fileSizeLimit: "10MiB", |
| 22 | + allowedMimeTypes: ["image/png", "image/jpeg", "image/webp"], |
| 23 | + sourceDir: path.join(repoRoot, "supabase", "storage", "listing_avatars"), |
| 24 | + }, |
| 25 | + listing_photos: { |
| 26 | + public: true, |
| 27 | + fileSizeLimit: "50MiB", |
| 28 | + allowedMimeTypes: ["image/png", "image/jpeg", "image/webp"], |
| 29 | + sourceDir: path.join(repoRoot, "supabase", "storage", "listing_photos"), |
| 30 | + }, |
| 31 | +}; |
| 32 | + |
| 33 | +function parseStatusEnv() { |
| 34 | + const output = execFileSync("supabase", ["status", "-o", "env"], { |
| 35 | + cwd: repoRoot, |
| 36 | + encoding: "utf8", |
| 37 | + }); |
| 38 | + |
| 39 | + return output |
| 40 | + .split("\n") |
| 41 | + .map((line) => line.trim()) |
| 42 | + .filter(Boolean) |
| 43 | + .reduce((env, line) => { |
| 44 | + const separatorIndex = line.indexOf("="); |
| 45 | + if (separatorIndex === -1) return env; |
| 46 | + |
| 47 | + const key = line.slice(0, separatorIndex); |
| 48 | + const rawValue = line.slice(separatorIndex + 1).trim(); |
| 49 | + const value = rawValue.replace(/^"(.*)"$/, "$1"); |
| 50 | + env[key] = value; |
| 51 | + return env; |
| 52 | + }, {}); |
| 53 | +} |
| 54 | + |
| 55 | +function assertLocalApiUrl(apiUrl) { |
| 56 | + if (!apiUrl) { |
| 57 | + throw new Error("Missing API_URL from `supabase status -o env`."); |
| 58 | + } |
| 59 | + |
| 60 | + const hostname = new URL(apiUrl).hostname; |
| 61 | + if (hostname !== "127.0.0.1" && hostname !== "localhost") { |
| 62 | + throw new Error( |
| 63 | + `Refusing to seed demo media into non-local Supabase API: ${apiUrl}` |
| 64 | + ); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +function walkFiles(sourceDir, currentDir = sourceDir) { |
| 69 | + return readdirSync(currentDir, { withFileTypes: true }).flatMap((entry) => { |
| 70 | + const absolutePath = path.join(currentDir, entry.name); |
| 71 | + |
| 72 | + if (entry.isDirectory()) { |
| 73 | + return walkFiles(sourceDir, absolutePath); |
| 74 | + } |
| 75 | + |
| 76 | + if (!entry.isFile()) { |
| 77 | + return []; |
| 78 | + } |
| 79 | + |
| 80 | + return { |
| 81 | + absolutePath, |
| 82 | + objectPath: path.relative(sourceDir, absolutePath).split(path.sep).join("/"), |
| 83 | + }; |
| 84 | + }); |
| 85 | +} |
| 86 | + |
| 87 | +function getContentType(filePath) { |
| 88 | + const extension = path.extname(filePath).toLowerCase(); |
| 89 | + |
| 90 | + if (extension === ".jpg" || extension === ".jpeg") return "image/jpeg"; |
| 91 | + if (extension === ".png") return "image/png"; |
| 92 | + if (extension === ".webp") return "image/webp"; |
| 93 | + |
| 94 | + return "application/octet-stream"; |
| 95 | +} |
| 96 | + |
| 97 | +function normalizeFileSizeLimit(fileSizeLimit) { |
| 98 | + return fileSizeLimit.replace("MiB", "MB").replace("KiB", "KB"); |
| 99 | +} |
| 100 | + |
| 101 | +async function ensureBucket(supabase, bucketName, bucketConfig) { |
| 102 | + const { error } = await supabase.storage.createBucket(bucketName, { |
| 103 | + public: bucketConfig.public, |
| 104 | + fileSizeLimit: normalizeFileSizeLimit(bucketConfig.fileSizeLimit), |
| 105 | + allowedMimeTypes: bucketConfig.allowedMimeTypes, |
| 106 | + }); |
| 107 | + |
| 108 | + if (!error) return; |
| 109 | + |
| 110 | + const message = error.message?.toLowerCase() ?? ""; |
| 111 | + const alreadyExists = |
| 112 | + message.includes("already exists") || message.includes("duplicate"); |
| 113 | + |
| 114 | + if (!alreadyExists) { |
| 115 | + throw error; |
| 116 | + } |
| 117 | + |
| 118 | + const { error: updateError } = await supabase.storage.updateBucket(bucketName, { |
| 119 | + public: bucketConfig.public, |
| 120 | + fileSizeLimit: normalizeFileSizeLimit(bucketConfig.fileSizeLimit), |
| 121 | + allowedMimeTypes: bucketConfig.allowedMimeTypes, |
| 122 | + }); |
| 123 | + |
| 124 | + if (updateError) { |
| 125 | + throw updateError; |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +async function uploadBucketObjects(supabase, bucketName, bucketConfig) { |
| 130 | + await ensureBucket(supabase, bucketName, bucketConfig); |
| 131 | + |
| 132 | + const files = walkFiles(bucketConfig.sourceDir); |
| 133 | + |
| 134 | + for (const file of files) { |
| 135 | + const body = readFileSync(file.absolutePath); |
| 136 | + const contentType = getContentType(file.absolutePath); |
| 137 | + |
| 138 | + const { error } = await supabase.storage.from(bucketName).upload( |
| 139 | + file.objectPath, |
| 140 | + body, |
| 141 | + { |
| 142 | + contentType, |
| 143 | + upsert: true, |
| 144 | + } |
| 145 | + ); |
| 146 | + |
| 147 | + if (error) { |
| 148 | + throw new Error( |
| 149 | + `Failed to upload ${bucketName}/${file.objectPath}: ${error.message}` |
| 150 | + ); |
| 151 | + } |
| 152 | + |
| 153 | + const size = Math.round(statSync(file.absolutePath).size / 1024); |
| 154 | + console.log(`Uploaded ${bucketName}/${file.objectPath} (${size} KB)`); |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +async function main() { |
| 159 | + const env = parseStatusEnv(); |
| 160 | + assertLocalApiUrl(env.API_URL); |
| 161 | + |
| 162 | + if (!env.SERVICE_ROLE_KEY) { |
| 163 | + throw new Error("Missing SERVICE_ROLE_KEY from `supabase status -o env`."); |
| 164 | + } |
| 165 | + |
| 166 | + const supabase = createClient(env.API_URL, env.SERVICE_ROLE_KEY, { |
| 167 | + auth: { |
| 168 | + autoRefreshToken: false, |
| 169 | + persistSession: false, |
| 170 | + }, |
| 171 | + }); |
| 172 | + |
| 173 | + for (const [bucketName, bucketConfig] of Object.entries(bucketConfigs)) { |
| 174 | + await uploadBucketObjects(supabase, bucketName, bucketConfig); |
| 175 | + } |
| 176 | + |
| 177 | + console.log("Local demo media seeding complete."); |
| 178 | +} |
| 179 | + |
| 180 | +main().catch((error) => { |
| 181 | + console.error(error.message || error); |
| 182 | + process.exit(1); |
| 183 | +}); |
0 commit comments