1- import { readFile } from 'node:fs/promises'
1+ import { readFile , access } from 'node:fs/promises'
2+ import { execFile as execFileCallback } from 'node:child_process'
3+ import { promisify } from 'node:util'
24import { dirname , resolve } from 'node:path'
35import { fileURLToPath } from 'node:url'
46import { sdk } from '@radio4000/sdk'
57import fuzzysort from 'fuzzysort'
68import * as config from './config.js'
79import { channelSchema , trackSchema } from './schema.js'
810
11+ const execFile = promisify ( execFileCallback )
912const __dirname = dirname ( fileURLToPath ( import . meta. url ) )
1013
1114// V1 data paths
12- const V1_CHANNELS_PATH = resolve ( __dirname , '../../data/channels_v1.json' )
13- const V1_TRACKS_PATH = resolve ( __dirname , '../../data/tracks_v1.json' )
15+ const dataDir = resolve ( __dirname , '../../data' )
16+ const tarballPath = resolve ( dataDir , 'data_v1.tar.gz' )
17+ const channelsPath = resolve ( dataDir , 'channels_v1.json' )
18+ const tracksPath = resolve ( dataDir , 'tracks_v1.json' )
1419
1520// Cache for v1 data
1621let v1ChannelsCache = null
@@ -90,9 +95,19 @@ const rejectV1Mutation = (type, id) => {
9095
9196// ===== V1 DATA LOADERS =====
9297
98+ async function ensureV1DataExtracted ( ) {
99+ try {
100+ await access ( channelsPath )
101+ await access ( tracksPath )
102+ } catch {
103+ await execFile ( 'tar' , [ 'xzf' , tarballPath , '-C' , dataDir ] )
104+ }
105+ }
106+
93107export async function loadV1Channels ( ) {
94108 if ( v1ChannelsCache ) return v1ChannelsCache
95- const content = await readFile ( V1_CHANNELS_PATH , 'utf-8' )
109+ await ensureV1DataExtracted ( )
110+ const content = await readFile ( channelsPath , 'utf-8' )
96111 v1ChannelsCache = JSON . parse ( content ) . map ( ( item ) =>
97112 channelSchema . parse ( { ...item , source : 'v1' } )
98113 )
@@ -101,7 +116,8 @@ export async function loadV1Channels() {
101116
102117export async function loadV1Tracks ( ) {
103118 if ( v1TracksCache ) return v1TracksCache
104- const content = await readFile ( V1_TRACKS_PATH , 'utf-8' )
119+ await ensureV1DataExtracted ( )
120+ const content = await readFile ( tracksPath , 'utf-8' )
105121 v1TracksCache = JSON . parse ( content )
106122 . map ( ( item ) => {
107123 try {
0 commit comments