Spaces:
Sleeping
Sleeping
File size: 7,747 Bytes
4bea261 | 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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | import fs from 'node:fs/promises';
import path from 'node:path';
import { txasupabase } from './txasupabase.js';
const DATA_DIR = path.join(process.cwd(), 'src/data');
const MOVIES_FILE = path.join(DATA_DIR, 'movies.json');
const DELETED_FILE = path.join(DATA_DIR, 'deleted_movies.json');
const GENRES_FILE = path.join(DATA_DIR, 'genres.json');
const COUNTRIES_FILE = path.join(DATA_DIR, 'countries.json');
const ACTORS_FILE = path.join(DATA_DIR, 'actors.json');
const SERVERS_FILE = path.join(DATA_DIR, 'servers.json');
const USERS_FILE = path.join(DATA_DIR, 'users.json');
const RATINGS_FILE = path.join(DATA_DIR, 'ratings.json');
// --- AUTO MIGRATION FROM LOCAL JSON TO SUPABASE ---
let isMigrated = false;
async function checkAndMigrate() {
if (isMigrated) return;
try {
// 1. Kiểm tra xem trên Supabase đã có phim nào chưa
const movies = await txasupabase.getLocalMovies();
if (Object.keys(movies).length === 0) {
console.log('=== [SUPABASE AUTO-MIGRATION START] ===');
// A. Di trú Server
try {
const localServers = JSON.parse(await fs.readFile(SERVERS_FILE, 'utf-8'));
if (Array.isArray(localServers) && localServers.length > 0) {
console.log(`Migrating ${localServers.length} servers to Supabase...`);
for (const s of localServers) {
await txasupabase.saveServer(s.name);
}
}
} catch (e) {}
// B. Di trú Thể loại
try {
const localGenres = JSON.parse(await fs.readFile(GENRES_FILE, 'utf-8'));
if (Array.isArray(localGenres) && localGenres.length > 0) {
console.log(`Migrating ${localGenres.length} genres to Supabase...`);
for (const g of localGenres) {
await txasupabase.saveGenre(g.slug, g.name);
}
}
} catch (e) {}
// C. Di trú Quốc gia
try {
const localCountries = JSON.parse(await fs.readFile(COUNTRIES_FILE, 'utf-8'));
if (Array.isArray(localCountries) && localCountries.length > 0) {
console.log(`Migrating ${localCountries.length} countries to Supabase...`);
for (const c of localCountries) {
await txasupabase.saveCountry(c.slug, c.name);
}
}
} catch (e) {}
// D. Di trú Diễn viên
try {
const localActors = JSON.parse(await fs.readFile(ACTORS_FILE, 'utf-8'));
if (Array.isArray(localActors) && localActors.length > 0) {
console.log(`Migrating ${localActors.length} actors to Supabase...`);
for (const a of localActors) {
await txasupabase.saveActor(a.name);
}
}
} catch (e) {}
// E. Di trú Deleted/Hidden Movies list
try {
const localDeleted = JSON.parse(await fs.readFile(DELETED_FILE, 'utf-8'));
if (Array.isArray(localDeleted) && localDeleted.length > 0) {
console.log(`Migrating ${localDeleted.length} hidden movie slugs to Supabase...`);
for (const slug of localDeleted) {
await txasupabase.deleteLocalMovie(slug);
}
}
} catch (e) {}
// F. Di trú Phim cục bộ
try {
const localMovies = JSON.parse(await fs.readFile(MOVIES_FILE, 'utf-8'));
const entries = Object.entries(localMovies);
if (entries.length > 0) {
console.log(`Migrating ${entries.length} movies with episodes to Supabase...`);
for (const [slug, mData] of entries) {
await txasupabase.saveLocalMovie(slug, mData);
}
}
} catch (e) {}
// G. Di trú Users
try {
const localUsers = JSON.parse(await fs.readFile(USERS_FILE, 'utf-8'));
if (Array.isArray(localUsers) && localUsers.length > 0) {
console.log(`Migrating ${localUsers.length} user accounts to Supabase...`);
for (const user of localUsers) {
await txasupabase.saveUser(user);
}
}
} catch (e) {}
// H. Di trú Ratings
try {
const localRatings = JSON.parse(await fs.readFile(RATINGS_FILE, 'utf-8'));
const entries = Object.entries(localRatings);
if (entries.length > 0) {
console.log(`Migrating ${entries.length} movie ratings to Supabase...`);
for (const [slug, rData] of entries) {
await txasupabase.saveRating(slug, rData.count, rData.totalScore, rData.userRatings || {}, rData.ipRatings || {});
}
}
} catch (e) {}
console.log('=== [SUPABASE AUTO-MIGRATION COMPLETED SUCCESS] ===');
}
isMigrated = true;
} catch (err) {
console.error('Error during Supabase auto-migration:', err);
}
}
// --- MOVIES ---
export async function getLocalMovies() {
await checkAndMigrate();
return await txasupabase.getLocalMovies();
}
export async function saveLocalMovie(slug, movieData) {
await checkAndMigrate();
return await txasupabase.saveLocalMovie(slug, movieData);
}
export async function resetLocalMovie(slug) {
await checkAndMigrate();
return await txasupabase.resetLocalMovie(slug);
}
// --- DELETED MOVIES ---
export async function getDeletedMovies() {
await checkAndMigrate();
return await txasupabase.getDeletedMovies();
}
export async function deleteLocalMovie(slug) {
await checkAndMigrate();
return await txasupabase.deleteLocalMovie(slug);
}
export async function restoreLocalMovie(slug) {
await checkAndMigrate();
return await txasupabase.restoreLocalMovie(slug);
}
// --- GENRES ---
export async function getGenres() {
await checkAndMigrate();
return await txasupabase.getGenres();
}
export async function saveGenre(slug, name) {
await checkAndMigrate();
return await txasupabase.saveGenre(slug, name);
}
export async function deleteGenre(slug) {
await checkAndMigrate();
return await txasupabase.deleteGenre(slug);
}
export async function bulkDeleteGenres(slugs) {
await checkAndMigrate();
return await txasupabase.bulkDeleteGenres(slugs);
}
// --- COUNTRIES ---
export async function getCountries() {
await checkAndMigrate();
return await txasupabase.getCountries();
}
export async function saveCountry(slug, name) {
await checkAndMigrate();
return await txasupabase.saveCountry(slug, name);
}
export async function deleteCountry(slug) {
await checkAndMigrate();
return await txasupabase.deleteCountry(slug);
}
export async function bulkDeleteCountries(slugs) {
await checkAndMigrate();
return await txasupabase.bulkDeleteCountries(slugs);
}
// --- ACTORS ---
export async function getActors() {
await checkAndMigrate();
return await txasupabase.getActors();
}
export async function saveActor(name, originalName = null) {
await checkAndMigrate();
return await txasupabase.saveActor(name, originalName);
}
export async function deleteActor(name) {
await checkAndMigrate();
return await txasupabase.deleteActor(name);
}
export async function bulkDeleteActors(names) {
await checkAndMigrate();
return await txasupabase.bulkDeleteActors(names);
}
// --- SERVERS ---
export async function getServers() {
await checkAndMigrate();
return await txasupabase.getServers();
}
export async function saveServer(name, originalName = null) {
await checkAndMigrate();
return await txasupabase.saveServer(name, originalName);
}
export async function deleteServer(name) {
await checkAndMigrate();
return await txasupabase.deleteServer(name);
}
export async function bulkDeleteServers(names) {
await checkAndMigrate();
return await txasupabase.bulkDeleteServers(names);
}
// --- AUTOMATIC SYNC FROM MOVIES DATA ---
export async function syncFromMovies(type) {
await checkAndMigrate();
return await txasupabase.syncFromMovies(type);
}
|