mirror of
https://github.com/navidrome/navidrome.git
synced 2026-11-23 18:39:46 +01:00
167 lines
4.4 KiB
JavaScript
167 lines
4.4 KiB
JavaScript
import { baseUrl } from '../utils'
|
|
import {
|
|
httpClient,
|
|
clientUniqueId,
|
|
clientUniqueIdHeader,
|
|
} from '../dataProvider'
|
|
|
|
const url = (command, id, options) => {
|
|
const username = localStorage.getItem('username')
|
|
const token = localStorage.getItem('subsonic-token')
|
|
const salt = localStorage.getItem('subsonic-salt')
|
|
if (!username || !token || !salt) {
|
|
return ''
|
|
}
|
|
|
|
const params = new URLSearchParams()
|
|
params.append('u', username)
|
|
params.append('t', token)
|
|
params.append('s', salt)
|
|
params.append('f', 'json')
|
|
params.append('v', '1.8.0')
|
|
params.append('c', 'NavidromeUI')
|
|
id && params.append('id', id)
|
|
if (options) {
|
|
if (options.ts) {
|
|
options['_'] = new Date().getTime()
|
|
delete options.ts
|
|
}
|
|
Object.keys(options).forEach((k) => {
|
|
const value = options[k]
|
|
// Handle array parameters by appending each value separately
|
|
if (Array.isArray(value)) {
|
|
value.forEach((v) => params.append(k, v))
|
|
} else {
|
|
params.append(k, value)
|
|
}
|
|
})
|
|
}
|
|
return `/rest/${command}?${params.toString()}`
|
|
}
|
|
|
|
const ping = () => httpClient(url('ping'))
|
|
|
|
const reportPlaybackUrl = (mediaId, positionMs, state) =>
|
|
url('reportPlayback', null, { mediaId, mediaType: 'song', positionMs, state })
|
|
|
|
const reportPlayback = (mediaId, positionMs, state) =>
|
|
httpClient(reportPlaybackUrl(mediaId, positionMs, state))
|
|
|
|
const reportPlaybackKeepalive = (mediaId, positionMs, state) => {
|
|
const u = reportPlaybackUrl(mediaId, positionMs, state)
|
|
if (u) {
|
|
fetch(baseUrl(u), {
|
|
keepalive: true,
|
|
headers: { [clientUniqueIdHeader]: clientUniqueId },
|
|
})
|
|
}
|
|
}
|
|
|
|
const star = (id) => httpClient(url('star', id))
|
|
|
|
const unstar = (id) => httpClient(url('unstar', id))
|
|
|
|
const setRating = (id, rating) => httpClient(url('setRating', id, { rating }))
|
|
|
|
const download = (id, format = 'raw', bitrate = '0') =>
|
|
(window.location.href = baseUrl(url('download', id, { format, bitrate })))
|
|
|
|
const startScan = (options) => httpClient(url('startScan', null, options))
|
|
|
|
const getScanStatus = () => httpClient(url('getScanStatus'))
|
|
|
|
const getNowPlaying = () => httpClient(url('getNowPlaying'))
|
|
|
|
const getAvatarUrl = (username, size) =>
|
|
baseUrl(
|
|
url('getAvatar', null, {
|
|
username,
|
|
...(size && { size }),
|
|
}),
|
|
)
|
|
|
|
const getCoverArtUrl = (record, size, square) => {
|
|
// Known-absent art would only ever return a placeholder; skip the round trip entirely.
|
|
if (record.imageAbsent) {
|
|
return ''
|
|
}
|
|
const suffix = record.imageHash ? '_' + record.imageHash : ''
|
|
const options = {
|
|
// A hash-suffixed url is already pixel-versioned; the buster would defeat immutable caching.
|
|
...(!record.imageHash && record.updatedAt && { _: record.updatedAt }),
|
|
...(size && { size }),
|
|
...(square && { square }),
|
|
}
|
|
|
|
// TODO Move this logic to server
|
|
if (record.album) {
|
|
return baseUrl(url('getCoverArt', 'mf-' + record.id + suffix, options))
|
|
} else if (record.albumArtist) {
|
|
return baseUrl(url('getCoverArt', 'al-' + record.id + suffix, options))
|
|
} else if (record.sync !== undefined) {
|
|
// This is a playlist
|
|
return baseUrl(url('getCoverArt', 'pl-' + record.id + suffix, options))
|
|
} else if (record.streamUrl !== undefined) {
|
|
// This is a radio station
|
|
return baseUrl(url('getCoverArt', 'ra-' + record.id + suffix, options))
|
|
} else {
|
|
return baseUrl(url('getCoverArt', 'ar-' + record.id + suffix, options))
|
|
}
|
|
}
|
|
|
|
const getDiscCoverArtUrl = (albumId, discNumber, updatedAt, size) => {
|
|
const options = {
|
|
...(updatedAt && { _: updatedAt }),
|
|
...(size && { size }),
|
|
}
|
|
return baseUrl(
|
|
url('getCoverArt', 'dc-' + albumId + ':' + discNumber, options),
|
|
)
|
|
}
|
|
|
|
const getArtistInfo = (id) => {
|
|
return httpClient(url('getArtistInfo', id))
|
|
}
|
|
|
|
const getAlbumInfo = (id) => {
|
|
return httpClient(url('getAlbumInfo', id))
|
|
}
|
|
|
|
const getSimilarSongs2 = (id, count = 100) => {
|
|
return httpClient(url('getSimilarSongs2', id, { count }))
|
|
}
|
|
|
|
const getTopSongs = (artist, count = 50) => {
|
|
return httpClient(url('getTopSongs', null, { artist, count }))
|
|
}
|
|
|
|
const streamUrl = (id, options) => {
|
|
return baseUrl(
|
|
url('stream', id, {
|
|
ts: true,
|
|
...options,
|
|
}),
|
|
)
|
|
}
|
|
|
|
export default {
|
|
url,
|
|
ping,
|
|
reportPlayback,
|
|
reportPlaybackKeepalive,
|
|
download,
|
|
star,
|
|
unstar,
|
|
setRating,
|
|
startScan,
|
|
getScanStatus,
|
|
getNowPlaying,
|
|
getCoverArtUrl,
|
|
getDiscCoverArtUrl,
|
|
getAvatarUrl,
|
|
streamUrl,
|
|
getAlbumInfo,
|
|
getArtistInfo,
|
|
getTopSongs,
|
|
getSimilarSongs2,
|
|
}
|