Skip to content

Black Friday

Annual Pro is $90 for the first year, normally $120. Or start monthly at $6 for two months, then $12.

Ends Dec 4, 11:59 PM PT.

Create an account
← Public packages

@noah/mega-radio

MEGA Radio (the WAKE album) for members of the MEGA.dev Discord: sign in with Kody, verify with Discord, listen with repeat.

FollowForkOpen app

Files

upload-radio.ts

36 lines · 1.7 KB · TypeScript
/**
 * Upload the released MEGA Radio tracks and covers (each track's localFile in src/tracks.ts, cover =
 * `<name> (cover).png` beside it) into @noah/mega-radio's package storage. Run on Noah's machine, where
 * the files live under ~/mega-dev/radio/, after the package is published:
 *
 *   npm run upload-radio
 *
 * Files already stored with the same sha256 are skipped, so rerunning resumes.
 */
import { createHash } from 'node:crypto'
import { readFileSync } from 'node:fs'
import { homedir } from 'node:os'
import { uploadAudio } from '../src/audio-core.ts'
import { radioFiles } from '../src/tracks.ts'
import { callStore } from './kody-store.ts'

const sha = (bytes: Uint8Array) => createHash('sha256').update(bytes).digest('hex')
const { files: rows } = await callStore({ action: 'list', prefix: 'radio/' })
const have = new Map((rows as { code: string; sha256: string; complete: boolean }[]).filter((r) => r.complete).map((r) => [r.code, r.sha256]))
const results: Record<string, boolean> = {}
for (const f of radioFiles(homedir())) {
	const bytes = readFileSync(f.path)
	const digest = sha(bytes)
	if (have.get(f.key) === digest) {
		console.log(`${f.key}: already stored`)
		results[f.key] = true
		continue
	}
	const result = f.key.endsWith('.mp3')
		? await uploadAudio(f.key, bytes, digest, callStore, (line) => console.log(line))
		: await callStore({ action: 'store', code: f.key, sha256: digest, data: bytes.toString('base64') })
	results[f.key] = f.key.endsWith('.mp3') ? !!result?.ok : result?.sha256 === digest
	console.log(`${f.key}: ${results[f.key] ? 'ok' : 'FAILED'}`)
}
console.log(JSON.stringify(results))
if (Object.values(results).some((ok) => !ok)) process.exit(1)