Spaces:
Sleeping
Sleeping
| 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); | |
| } | |