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

audio-store.ts

31 lines · 1.4 KB · TypeScript
import { kody, packageStorage } from 'kody:runtime'
import { OWNER_KODY_ID } from './config.ts'
import { store } from './store.ts'

/** Storage keys left by an older version of this package; removed by `{ action: 'forget-legacy' }`. */
const LEGACY_KEYS = ['source:auth', 'radio:sync', 'radio:catalog']

/**
 * Manage the MEGA Radio MP3s and covers kept in this package's storage (see src/audio-core.ts).
 * Owner only. Called by scripts/upload-radio.ts through `kody execute --invoke kody:@noah/mega-radio/audio-store`.
 *
 * @param params - `{ action: 'begin' | 'put' | 'status' | 'verify' | 'remove' | 'store', code, ... }`, `{ action: 'list', prefix }`
 *   or `{ action: 'forget-legacy' }` (deletes old storage keys this version no longer uses)
 * @returns a small JSON summary, never the file itself
 */
export default async function audioStore(params: Record<string, unknown> = {}) {
	const me = (await kody.metaGetCurrentUser({})) as { user_id?: string }
	if (me?.user_id !== OWNER_KODY_ID) throw new Error('Only the package owner can manage the radio files.')
	if (params.action === 'forget-legacy') {
		const removed: string[] = []
		for (const k of LEGACY_KEYS) {
			if ((await packageStorage().get(k)) == null) continue
			await packageStorage().delete(k)
			removed.push(k)
		}
		return { ok: true, removed }
	}
	return store.manage(params)
}

export const serveFile = store.serve