-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate-photos-to-supabase.mjs
More file actions
190 lines (156 loc) · 5.88 KB
/
Copy pathmigrate-photos-to-supabase.mjs
File metadata and controls
190 lines (156 loc) · 5.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env node
import { createClient } from '@supabase/supabase-js';
import { readFileSync, readdirSync, statSync } from 'fs';
import { join, basename, extname } from 'path';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
import dotenv from 'dotenv';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// Load environment variables
dotenv.config();
const supabaseUrl = process.env.VITE_SUPABASE_URL;
// Use service role key if available, otherwise fall back to publishable key
const supabaseKey = process.env.SUPABASE_SERVICE_ROLE_KEY || process.env.VITE_SUPABASE_PUBLISHABLE_KEY;
// Note: Storage uploads typically require service role key to bypass RLS
if (!supabaseUrl || !supabaseKey) {
console.error('Missing Supabase credentials in .env file');
console.error('Required: VITE_SUPABASE_URL and either:');
console.error(' - SUPABASE_SERVICE_ROLE_KEY (recommended for uploads), or');
console.error(' - VITE_SUPABASE_PUBLISHABLE_KEY');
process.exit(1);
}
if (!process.env.SUPABASE_SERVICE_ROLE_KEY) {
console.warn('⚠️ Warning: Using publishable key. If uploads fail, add SUPABASE_SERVICE_ROLE_KEY to .env\n');
}
const supabase = createClient(supabaseUrl, supabaseKey);
// MIME type mapping for common image formats
const MIME_TYPES = {
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.png': 'image/png',
'.webp': 'image/webp',
'.gif': 'image/gif',
};
// Category mapping based on filenames
const categoryMapping = {
'selected': ['selected-'],
'commissioned': ['commissioned-', 'marie-'],
'editorial': ['editorial-'],
'personal': ['personal-', 'country-road', 'morning-fog', 'farmhouse', 'winter-landscape',
'lake-reflection', 'stone-wall', 'mountain-vista', 'wheat-field', 'barn-detail',
'forest-path', 'autumn-trees', 'prairie-sunset']
};
function determineCategory(filename) {
for (const [category, patterns] of Object.entries(categoryMapping)) {
if (patterns.some(pattern => filename.toLowerCase().includes(pattern.toLowerCase()))) {
return category;
}
}
return 'personal'; // default category
}
async function uploadPhoto(filePath, category) {
try {
const fileBuffer = readFileSync(filePath);
const fileName = basename(filePath);
const fileExt = extname(fileName);
const timestamp = Date.now();
// Note: For WebP conversion, use sharp or canvas in production
// For now, uploading original format
const uploadPath = `${category}/${timestamp}-${fileName}`;
console.log(`Uploading ${fileName} to ${uploadPath}...`);
const mimeType = MIME_TYPES[fileExt.toLowerCase()] || `image/${fileExt.replace('.', '')}`;
const { data: uploadData, error: uploadError } = await supabase.storage
.from('photos')
.upload(uploadPath, fileBuffer, {
contentType: mimeType,
cacheControl: '31536000',
upsert: false
});
if (uploadError) {
// Check if file already exists
if (uploadError.message.includes('already exists')) {
console.log(` ⚠️ File already exists, skipping: ${fileName}`);
return null;
}
throw uploadError;
}
const { data: { publicUrl } } = supabase.storage
.from('photos')
.getPublicUrl(uploadPath);
// Get current max display order and z_index for the category
const { data: maxOrderData } = await supabase
.from('photos')
.select('display_order, z_index')
.eq('category', category)
.order('display_order', { ascending: false })
.limit(1)
.maybeSingle();
const nextOrder = (maxOrderData?.display_order ?? -1) + 1;
const nextZIndex = (maxOrderData?.z_index ?? -1) + 1;
// Calculate initial position for new photo
const photosPerRow = 3;
const photoWidth = 300;
const photoHeight = 400;
const gap = 20;
const row = Math.floor(nextOrder / photosPerRow);
const col = nextOrder % photosPerRow;
const initialX = col * (photoWidth + gap);
const initialY = row * (photoHeight + gap);
// Insert into photos table
const { error: insertError } = await supabase
.from('photos')
.insert({
category,
image_url: publicUrl,
display_order: nextOrder,
title: fileName.replace(fileExt, '').replace(/-/g, ' '),
position_x: initialX,
position_y: initialY,
width: photoWidth,
height: photoHeight,
scale: 1.0,
rotation: 0,
z_index: nextZIndex,
is_draft: false
});
if (insertError) throw insertError;
console.log(` ✓ Successfully uploaded and saved: ${fileName}`);
return publicUrl;
} catch (error) {
console.error(` ✗ Error uploading ${basename(filePath)}:`, error.message);
return null;
}
}
async function migratePhotos() {
const galleryPath = join(__dirname, 'src', 'assets', 'gallery');
console.log('Starting photo migration to Supabase...\n');
if (!statSync(galleryPath).isDirectory()) {
console.error('Gallery directory not found');
process.exit(1);
}
const files = readdirSync(galleryPath);
const imageFiles = files.filter(f => /\.(jpg|jpeg|png|webp)$/i.test(f));
console.log(`Found ${imageFiles.length} images to migrate\n`);
let successCount = 0;
let skipCount = 0;
let errorCount = 0;
for (const file of imageFiles) {
const filePath = join(galleryPath, file);
const category = determineCategory(file);
const result = await uploadPhoto(filePath, category);
if (result === null) {
skipCount++;
} else if (result) {
successCount++;
} else {
errorCount++;
}
}
console.log('\n=== Migration Summary ===');
console.log(`Total files: ${imageFiles.length}`);
console.log(`✓ Successfully uploaded: ${successCount}`);
console.log(`⚠️ Skipped (already exists): ${skipCount}`);
console.log(`✗ Failed: ${errorCount}`);
}
migratePhotos().catch(console.error);