feat: last-played stamping via sync and played endpoint, stream/album proxy routes
This commit is contained in:
@@ -14,6 +14,7 @@ import { registerSettingsRoutes } from './routes/settingsRoutes.js'
|
||||
import { registerLookupRoutes } from './routes/lookupRoutes.js'
|
||||
import { registerCollectionRoutes } from './routes/collectionRoutes.js'
|
||||
import { registerLibraryRoutes } from './routes/libraryRoutes.js'
|
||||
import { registerStreamRoutes } from './routes/streamRoutes.js'
|
||||
|
||||
declare module 'fastify' {
|
||||
interface FastifyInstance {
|
||||
@@ -52,6 +53,7 @@ export async function buildApp(opts: AppOptions): Promise<FastifyInstance> {
|
||||
await registerLookupRoutes(app)
|
||||
await registerCollectionRoutes(app)
|
||||
await registerLibraryRoutes(app)
|
||||
await registerStreamRoutes(app)
|
||||
|
||||
// artwork cache (always available)
|
||||
await app.register(fastifyStatic, {
|
||||
|
||||
65
server/src/routes/streamRoutes.ts
Normal file
65
server/src/routes/streamRoutes.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import { FastifyInstance } from 'fastify'
|
||||
import { SubsonicClient, SubsonicError } from '../subsonic.js'
|
||||
import { requireAuth } from './authRoutes.js'
|
||||
import { getSettings, subsonicConfigComplete } from './settingsRoutes.js'
|
||||
|
||||
function clientFor(request: any): SubsonicClient | null {
|
||||
const s = getSettings(request.server.db, request.user.id)
|
||||
if (!subsonicConfigComplete(s)) return null
|
||||
return new SubsonicClient({
|
||||
url: s.subsonic_url as string,
|
||||
username: s.subsonic_username as string,
|
||||
password: s.subsonic_password as string,
|
||||
fetchImpl: request.server.fetchImpl,
|
||||
})
|
||||
}
|
||||
|
||||
export async function registerStreamRoutes(app: FastifyInstance): Promise<void> {
|
||||
app.get('/api/stream/:songId', { preHandler: [requireAuth] }, async (request, reply) => {
|
||||
const client = clientFor(request)
|
||||
if (!client) return reply.code(409).send({ error: 'no_subsonic_config' })
|
||||
const songId = (request.params as { songId: string }).songId
|
||||
const range = request.headers.range
|
||||
let upstream: Response
|
||||
try {
|
||||
upstream = await request.server.fetchImpl(client.url('stream', { id: songId }), {
|
||||
headers: range ? { Range: range } : {},
|
||||
})
|
||||
} catch {
|
||||
return reply.code(502).send({ error: 'stream_unavailable' })
|
||||
}
|
||||
if (!upstream.ok && upstream.status !== 206) {
|
||||
return reply.code(502).send({ error: 'stream_unavailable' })
|
||||
}
|
||||
const headers: Record<string, string> = {}
|
||||
for (const h of ['content-type', 'content-length', 'content-range', 'accept-ranges']) {
|
||||
const v = upstream.headers.get(h)
|
||||
if (v) headers[h] = v
|
||||
}
|
||||
if (!upstream.headers.get('accept-ranges')) headers['accept-ranges'] = 'bytes'
|
||||
return reply.code(upstream.status).headers(headers).send(upstream.body)
|
||||
})
|
||||
|
||||
app.get('/api/album/:subsonicId/tracks', { preHandler: [requireAuth] }, async (request, reply) => {
|
||||
const client = clientFor(request)
|
||||
if (!client) return reply.code(409).send({ error: 'no_subsonic_config' })
|
||||
try {
|
||||
const album = await client.getAlbum((request.params as { subsonicId: string }).subsonicId)
|
||||
return album
|
||||
} catch (err) {
|
||||
if (err instanceof SubsonicError) return reply.code(502).send({ error: 'subsonic_error', detail: err.message })
|
||||
return reply.code(502).send({ error: 'subsonic_unreachable' })
|
||||
}
|
||||
})
|
||||
|
||||
app.post('/api/album/:subsonicId/played', { preHandler: [requireAuth] }, async (request, reply) => {
|
||||
const subsonicId = (request.params as { subsonicId: string }).subsonicId
|
||||
const info = request.server.db
|
||||
.prepare(
|
||||
'UPDATE digital_albums SET last_played_at = ? WHERE user_id = ? AND subsonic_id = ?'
|
||||
)
|
||||
.run(new Date().toISOString(), request.user!.id, subsonicId)
|
||||
if (info.changes === 0) return reply.code(404).send({ error: 'not_found' })
|
||||
return { ok: true }
|
||||
})
|
||||
}
|
||||
@@ -54,6 +54,18 @@ export class SyncManager {
|
||||
}
|
||||
})
|
||||
apply()
|
||||
// best-effort: stamp played timestamps reported by the server
|
||||
try {
|
||||
const recent = await client.getRecentAlbums(500)
|
||||
const stamp = this.db.prepare(
|
||||
'UPDATE digital_albums SET last_played_at = ? WHERE user_id = ? AND subsonic_id = ?'
|
||||
)
|
||||
for (const r of recent) {
|
||||
if (r.playedAt) stamp.run(r.playedAt, userId, r.id)
|
||||
}
|
||||
} catch {
|
||||
// recency stamping is optional
|
||||
}
|
||||
this.states.set(userId, {
|
||||
status: 'done',
|
||||
error: null,
|
||||
|
||||
Reference in New Issue
Block a user