28 lines
1.2 KiB
JavaScript
28 lines
1.2 KiB
JavaScript
import { join } from 'node:path'
|
|
import { readFileSync, existsSync } from 'node:fs'
|
|
import { getDb } from './db.js'
|
|
import { sanitizeStreamUrl } from './feedUtils.js'
|
|
|
|
/**
|
|
* One-time migration: insert entries from server/data/feeds.json into devices (device_type = 'feed').
|
|
* No-op if devices table already has rows or feeds file is missing.
|
|
*/
|
|
export async function migrateFeedsToDevices() {
|
|
const db = await getDb()
|
|
const row = await db.get('SELECT COUNT(*) as n FROM devices')
|
|
if (row?.n > 0) return
|
|
const path = join(process.cwd(), 'server/data/feeds.json')
|
|
if (!existsSync(path)) return
|
|
const data = JSON.parse(readFileSync(path, 'utf8'))
|
|
const list = Array.isArray(data) ? data : []
|
|
for (const feed of list) {
|
|
if (!feed?.id || typeof feed.name !== 'string' || typeof feed.lat !== 'number' || typeof feed.lng !== 'number') continue
|
|
const streamUrl = sanitizeStreamUrl(feed.streamUrl) ?? ''
|
|
const sourceType = feed.sourceType === 'hls' ? 'hls' : 'mjpeg'
|
|
await db.run(
|
|
'INSERT OR IGNORE INTO devices (id, name, device_type, vendor, lat, lng, stream_url, source_type, config) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)',
|
|
[feed.id, feed.name, 'feed', null, feed.lat, feed.lng, streamUrl, sourceType, null],
|
|
)
|
|
}
|
|
}
|