From 9913b929053c903f23eaa9fcbf8490a097059b56 Mon Sep 17 00:00:00 2001 From: Deluan Date: Tue, 6 Apr 2021 22:18:48 -0400 Subject: [PATCH] Get lossless format list from server --- consts/mime_types.go | 5 ++ server/app/serve_index.go | 1 + server/app/serve_index_test.go | 12 +++++ ui/src/common/QualityInfo.js | 24 ++++++--- ui/src/common/QualityInfo.test.js | 83 ++++++------------------------- ui/src/config.js | 3 +- ui/src/consts.js | 10 ---- 7 files changed, 51 insertions(+), 87 deletions(-) diff --git a/consts/mime_types.go b/consts/mime_types.go index 1386ecfd2..d97f21a3f 100644 --- a/consts/mime_types.go +++ b/consts/mime_types.go @@ -2,6 +2,10 @@ package consts import "mime" +var LosslessFormats = []string{ + "flac", "wav", "alac", "ape", "dsf", "wav", "shn", "wv", "wvp", +} + func init() { mt := map[string]string{ ".mp3": "audio/mpeg", @@ -9,6 +13,7 @@ func init() { ".oga": "audio/ogg", ".opus": "audio/ogg", ".aac": "audio/mp4", + ".alac": "audio/mp4", ".m4a": "audio/mp4", ".m4b": "audio/mp4", ".flac": "audio/flac", diff --git a/server/app/serve_index.go b/server/app/serve_index.go index 4c1c54610..b1a5ac47d 100644 --- a/server/app/serve_index.go +++ b/server/app/serve_index.go @@ -43,6 +43,7 @@ func serveIndex(ds model.DataStore, fs fs.FS) http.HandlerFunc { "gaTrackingId": conf.Server.GATrackingID, "enableDownloads": conf.Server.EnableDownloads, "enableFavourites": conf.Server.EnableFavourites, + "losslessFormats": strings.ToUpper(strings.Join(consts.LosslessFormats, ",")), "devActivityPanel": conf.Server.DevActivityPanel, "devFastAccessCoverArt": conf.Server.DevFastAccessCoverArt, } diff --git a/server/app/serve_index_test.go b/server/app/serve_index_test.go index a903b716d..90f47dab6 100644 --- a/server/app/serve_index_test.go +++ b/server/app/serve_index_test.go @@ -7,6 +7,7 @@ import ( "os" "regexp" "strconv" + "strings" "github.com/navidrome/navidrome/conf" "github.com/navidrome/navidrome/consts" @@ -155,6 +156,17 @@ var _ = Describe("serveIndex", func() { config := extractAppConfig(w.Body.String()) Expect(config).To(HaveKeyWithValue("version", consts.Version())) }) + + It("sets the losslessFormats", func() { + r := httptest.NewRequest("GET", "/index.html", nil) + w := httptest.NewRecorder() + + serveIndex(ds, fs)(w, r) + + config := extractAppConfig(w.Body.String()) + expected := strings.ToUpper(strings.Join(consts.LosslessFormats, ",")) + Expect(config).To(HaveKeyWithValue("losslessFormats", expected)) + }) }) var appConfigRegex = regexp.MustCompile(`(?m)window.__APP_CONFIG__="([^"]*)`) diff --git a/ui/src/common/QualityInfo.js b/ui/src/common/QualityInfo.js index 37b637dbf..a8376c67b 100644 --- a/ui/src/common/QualityInfo.js +++ b/ui/src/common/QualityInfo.js @@ -1,16 +1,24 @@ import React from 'react' import PropTypes from 'prop-types' import Chip from '@material-ui/core/Chip' -import { LOSSLESS_FORMATS } from '../consts' +import config from '../config' -export const QualityInfo = ({ record, size, ...rest }) => { - let { suffix = 'NO_SUFFIX', bitRate = 'NO_BITRATE' } = record - suffix = suffix.toUpperCase() - let info = suffix - if (!LOSSLESS_FORMATS.includes(suffix)) { - info += ' ' + bitRate +const llFormats = new Set(config.losslessFormats.split(',')) +const placeholder = 'N/A' + +export const QualityInfo = ({ record, ...rest }) => { + let { suffix, bitRate } = record + let info = placeholder + + if (suffix) { + suffix = suffix.toUpperCase() + info = suffix + if (!llFormats.has(suffix)) { + info += ' ' + bitRate + } } - return + + return } QualityInfo.propTypes = { diff --git a/ui/src/common/QualityInfo.test.js b/ui/src/common/QualityInfo.test.js index e2781c85a..aec3a3bc3 100644 --- a/ui/src/common/QualityInfo.test.js +++ b/ui/src/common/QualityInfo.test.js @@ -5,76 +5,23 @@ import { QualityInfo } from './QualityInfo' describe('', () => { afterEach(cleanup) - it('only render FLAC', () => { - const info = { suffix: 'FLAC', bitRate: 108 } - const { getByText } = render() - const format = getByText('FLAC') - expect(format.innerHTML).toEqual('FLAC') + it('only render suffix for lossless formats', () => { + const info = { suffix: 'FLAC', bitRate: 1008 } + const { queryByText } = render() + expect(queryByText('FLAC')).not.toBeNull() }) - it('only render WAV', () => { - const info = { suffix: 'WAV', bitRate: 108 } - const { getByText } = render() - const format = getByText('WAV') - expect(format.innerHTML).toEqual('WAV') + it('only render suffix and bitrate for lossy formats', () => { + const info = { suffix: 'MP3', bitRate: 320 } + const { queryByText } = render() + expect(queryByText('MP3 320')).not.toBeNull() }) - it('only render DSF', () => { - const info = { suffix: 'DSF', bitRate: 108 } - const { getByText } = render() - const format = getByText('DSF') - expect(format.innerHTML).toEqual('DSF') - }) - it('only render ALAC', () => { - const info = { suffix: 'ALAC', bitRate: 108 } - const { getByText } = render() - const format = getByText('ALAC') - expect(format.innerHTML).toEqual('ALAC') - }) - it('only render TTA', () => { - const info = { suffix: 'TTA', bitRate: 108 } - const { getByText } = render() - const format = getByText('TTA') - expect(format.innerHTML).toEqual('TTA') - }) - it('only render ATRAC', () => { - const info = { suffix: 'ATRAC', bitRate: 108 } - const { getByText } = render() - const format = getByText('ATRAC') - expect(format.innerHTML).toEqual('ATRAC') - }) - it('only render SHN', () => { - const info = { suffix: 'SHN', bitRate: 108 } - const { getByText } = render() - const format = getByText('SHN') - expect(format.innerHTML).toEqual('SHN') - }) - it('only render OCG 108', () => { - const info = { suffix: 'OCG', bitRate: 108 } - const { getByText } = render() - const format = getByText('OCG 108') - expect(format.innerHTML).toEqual('OCG 108') - }) - it('only render MP3 108', () => { - const info = { suffix: 'MP3', bitRate: 108 } - const { getByText } = render() - const format = getByText('MP3 108') - expect(format.innerHTML).toEqual('MP3 108') - }) - it('only render AAC 108', () => { - const info = { suffix: 'AAC', bitRate: 108 } - const { getByText } = render() - const format = getByText('AAC 108') - expect(format.innerHTML).toEqual('AAC 108') - }) - it('only render OPUS 108', () => { - const info = { suffix: 'OPUS', bitRate: 108 } - const { getByText } = render() - const format = getByText('OPUS 108') - expect(format.innerHTML).toEqual('OPUS 108') - }) - it('render nothing', () => { + it('renders placeholder if suffix is missing', () => { const info = {} - const { getByText } = render() - const format = getByText('NO_SUFFIX NO_BITRATE') - expect(format.innerHTML).toEqual('NO_SUFFIX NO_BITRATE') + const { queryByText } = render() + expect(queryByText('N/A')).not.toBeNull() + }) + it('does not break if record is null', () => { + const { queryByText } = render() + expect(queryByText('N/A')).not.toBeNull() }) }) diff --git a/ui/src/config.js b/ui/src/config.js index d0dd739df..c2f9449c3 100644 --- a/ui/src/config.js +++ b/ui/src/config.js @@ -9,11 +9,12 @@ const defaultConfig = { loginBackgroundURL: 'https://source.unsplash.com/collection/1065384/1600x900', enableTranscodingConfig: true, enableDownloads: true, + enableFavourites: true, + losslessFormats: 'FLAC,WAV,ALAC,DSF', welcomeMessage: '', gaTrackingId: '', devActivityPanel: true, devFastAccessCoverArt: false, - enableFavourites: true, } let config diff --git a/ui/src/consts.js b/ui/src/consts.js index 1e7578083..3dc6bcbf2 100644 --- a/ui/src/consts.js +++ b/ui/src/consts.js @@ -3,13 +3,3 @@ export const REST_URL = '/app/api' export const M3U_MIME_TYPE = 'audio/x-mpegurl' export const AUTO_THEME_ID = 'AUTO_THEME_ID' - -export const LOSSLESS_FORMATS = [ - 'FLAC', - 'WAV', - 'DSF', - 'ALAC', - 'TTA', - 'ATRAC', - 'SHN', -]