mirror of
https://github.com/austinried/subtracks.git
synced 2026-02-10 06:52:43 +01:00
@@ -2,12 +2,16 @@ import { Album, Playlist, Artist, Song } from './library'
|
|||||||
|
|
||||||
export enum CacheItemType {
|
export enum CacheItemType {
|
||||||
coverArt = 'coverArt',
|
coverArt = 'coverArt',
|
||||||
|
coverArtThumb = 'coverArtThumb',
|
||||||
artistArt = 'artistArt',
|
artistArt = 'artistArt',
|
||||||
|
artistArtThumb = 'artistArtThumb',
|
||||||
song = 'song',
|
song = 'song',
|
||||||
}
|
}
|
||||||
|
|
||||||
export type CacheItemTypeKey = keyof typeof CacheItemType
|
export type CacheItemTypeKey = keyof typeof CacheItemType
|
||||||
|
|
||||||
|
export type CacheImageSize = 'thumbnail' | 'original'
|
||||||
|
|
||||||
export type CacheFile = {
|
export type CacheFile = {
|
||||||
path: string
|
path: string
|
||||||
date: number
|
date: number
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { CacheFile, CacheItemType, CacheItemTypeKey, CacheRequest } from '@app/models/cache'
|
import { CacheFile, CacheImageSize, CacheItemType, CacheItemTypeKey, CacheRequest } from '@app/models/cache'
|
||||||
import { mkdir, rmdir } from '@app/util/fs'
|
import { mkdir, rmdir } from '@app/util/fs'
|
||||||
import PromiseQueue from '@app/util/PromiseQueue'
|
import PromiseQueue from '@app/util/PromiseQueue'
|
||||||
import produce from 'immer'
|
import produce from 'immer'
|
||||||
@@ -8,7 +8,9 @@ import { Store } from './store'
|
|||||||
|
|
||||||
const queues: Record<CacheItemTypeKey, PromiseQueue> = {
|
const queues: Record<CacheItemTypeKey, PromiseQueue> = {
|
||||||
coverArt: new PromiseQueue(5),
|
coverArt: new PromiseQueue(5),
|
||||||
|
coverArtThumb: new PromiseQueue(50),
|
||||||
artistArt: new PromiseQueue(5),
|
artistArt: new PromiseQueue(5),
|
||||||
|
artistArtThumb: new PromiseQueue(50),
|
||||||
song: new PromiseQueue(1),
|
song: new PromiseQueue(1),
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -30,7 +32,7 @@ export type CacheSlice = {
|
|||||||
cacheFiles: CacheFilesByServer
|
cacheFiles: CacheFilesByServer
|
||||||
cacheRequests: CacheRequestsByServer
|
cacheRequests: CacheRequestsByServer
|
||||||
|
|
||||||
fetchCoverArtFilePath: (coverArt: string) => Promise<string | undefined>
|
fetchCoverArtFilePath: (coverArt: string, size?: CacheImageSize) => Promise<string | undefined>
|
||||||
|
|
||||||
createCache: (serverId: string) => Promise<void>
|
createCache: (serverId: string) => Promise<void>
|
||||||
prepareCache: (serverId: string) => void
|
prepareCache: (serverId: string) => void
|
||||||
@@ -134,7 +136,7 @@ export const createCacheSlice = (set: SetState<Store>, get: GetState<Store>): Ca
|
|||||||
return await promise
|
return await promise
|
||||||
},
|
},
|
||||||
|
|
||||||
fetchCoverArtFilePath: async coverArt => {
|
fetchCoverArtFilePath: async (coverArt, size = 'thumbnail') => {
|
||||||
const client = get().client
|
const client = get().client
|
||||||
if (!client) {
|
if (!client) {
|
||||||
return
|
return
|
||||||
@@ -145,7 +147,7 @@ export const createCacheSlice = (set: SetState<Store>, get: GetState<Store>): Ca
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const key: CacheItemTypeKey = 'coverArt'
|
const key: CacheItemTypeKey = size === 'thumbnail' ? 'coverArtThumb' : 'coverArt'
|
||||||
|
|
||||||
const existing = get().cacheFiles[activeServerId][key][coverArt]
|
const existing = get().cacheFiles[activeServerId][key][coverArt]
|
||||||
const inProgress = get().cacheRequests[activeServerId][key][coverArt]
|
const inProgress = get().cacheRequests[activeServerId][key][coverArt]
|
||||||
@@ -156,7 +158,12 @@ export const createCacheSlice = (set: SetState<Store>, get: GetState<Store>): Ca
|
|||||||
return `file://${existing.path}`
|
return `file://${existing.path}`
|
||||||
}
|
}
|
||||||
|
|
||||||
await get().cacheItem(key, coverArt, () => client.getCoverArtUri({ id: coverArt }))
|
await get().cacheItem(key, coverArt, () =>
|
||||||
|
client.getCoverArtUri({
|
||||||
|
id: coverArt,
|
||||||
|
size: size === 'thumbnail' ? '256' : undefined,
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
return `file://${get().cacheFiles[activeServerId][key][coverArt].path}`
|
return `file://${get().cacheFiles[activeServerId][key][coverArt].path}`
|
||||||
},
|
},
|
||||||
@@ -171,7 +178,9 @@ export const createCacheSlice = (set: SetState<Store>, get: GetState<Store>): Ca
|
|||||||
state.cacheFiles[serverId] = {
|
state.cacheFiles[serverId] = {
|
||||||
song: {},
|
song: {},
|
||||||
coverArt: {},
|
coverArt: {},
|
||||||
|
coverArtThumb: {},
|
||||||
artistArt: {},
|
artistArt: {},
|
||||||
|
artistArtThumb: {},
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
@@ -186,14 +195,18 @@ export const createCacheSlice = (set: SetState<Store>, get: GetState<Store>): Ca
|
|||||||
state.cacheDirs[serverId] = {
|
state.cacheDirs[serverId] = {
|
||||||
song: `${RNFS.DocumentDirectoryPath}/servers/${serverId}/song`,
|
song: `${RNFS.DocumentDirectoryPath}/servers/${serverId}/song`,
|
||||||
coverArt: `${RNFS.DocumentDirectoryPath}/servers/${serverId}/coverArt`,
|
coverArt: `${RNFS.DocumentDirectoryPath}/servers/${serverId}/coverArt`,
|
||||||
|
coverArtThumb: `${RNFS.DocumentDirectoryPath}/servers/${serverId}/coverArtThumb`,
|
||||||
artistArt: `${RNFS.DocumentDirectoryPath}/servers/${serverId}/artistArt`,
|
artistArt: `${RNFS.DocumentDirectoryPath}/servers/${serverId}/artistArt`,
|
||||||
|
artistArtThumb: `${RNFS.DocumentDirectoryPath}/servers/${serverId}/artistArtThumb`,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!state.cacheRequests[serverId]) {
|
if (!state.cacheRequests[serverId]) {
|
||||||
state.cacheRequests[serverId] = {
|
state.cacheRequests[serverId] = {
|
||||||
song: {},
|
song: {},
|
||||||
coverArt: {},
|
coverArt: {},
|
||||||
|
coverArtThumb: {},
|
||||||
artistArt: {},
|
artistArt: {},
|
||||||
|
artistArtThumb: {},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
@@ -260,7 +273,9 @@ export const createCacheSlice = (set: SetState<Store>, get: GetState<Store>): Ca
|
|||||||
set(
|
set(
|
||||||
produce<CacheSlice>(state => {
|
produce<CacheSlice>(state => {
|
||||||
state.cacheFiles[serverId].coverArt = {}
|
state.cacheFiles[serverId].coverArt = {}
|
||||||
|
state.cacheFiles[serverId].coverArtThumb = {}
|
||||||
state.cacheFiles[serverId].artistArt = {}
|
state.cacheFiles[serverId].artistArt = {}
|
||||||
|
state.cacheFiles[serverId].artistArtThumb = {}
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ import {
|
|||||||
SubsonicResponse,
|
SubsonicResponse,
|
||||||
} from '@app/subsonic/responses'
|
} from '@app/subsonic/responses'
|
||||||
import PromiseQueue from '@app/util/PromiseQueue'
|
import PromiseQueue from '@app/util/PromiseQueue'
|
||||||
import { reduceById, mergeById, mapId } from '@app/util/state'
|
import { reduceById, mergeById } from '@app/util/state'
|
||||||
import produce from 'immer'
|
import produce from 'immer'
|
||||||
import { WritableDraft } from 'immer/dist/types/types-external'
|
import { WritableDraft } from 'immer/dist/types/types-external'
|
||||||
import pick from 'lodash.pick'
|
import pick from 'lodash.pick'
|
||||||
@@ -547,3 +547,7 @@ function mapSong(song: ChildElement): Song {
|
|||||||
starred: song.starred,
|
starred: song.starred,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function mapId(entities: { id: string }[]): string[] {
|
||||||
|
return entities.map(e => e.id)
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,26 +1,9 @@
|
|||||||
import { Store } from './store'
|
const migrations: Array<(state: any) => any> = [
|
||||||
import RNFS from 'react-native-fs'
|
state => {
|
||||||
import { rmdir } from '@app/util/fs'
|
for (let server of state.settings.servers) {
|
||||||
|
|
||||||
const migrations: Array<(state: any) => Promise<any>> = [
|
|
||||||
async state => {
|
|
||||||
for (const server of state.settings.servers) {
|
|
||||||
server.usePlainPassword = false
|
server.usePlainPassword = false
|
||||||
}
|
}
|
||||||
|
|
||||||
return state
|
|
||||||
},
|
|
||||||
async state => {
|
|
||||||
const store = state as Store
|
|
||||||
const keysToDelete = ['coverArtThumb', 'artistArtThumb']
|
|
||||||
|
|
||||||
for (const serverId in store.cacheDirs) {
|
|
||||||
for (const key in keysToDelete) {
|
|
||||||
await rmdir(`${RNFS.DocumentDirectoryPath}/servers/${serverId}/${key}`)
|
|
||||||
delete state.cacheFiles[serverId][key]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return state
|
return state
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -50,13 +50,13 @@ export const useStore = create<
|
|||||||
postState?.setHydrated(true)
|
postState?.setHydrated(true)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
migrate: async (persistedState, version) => {
|
migrate: (persistedState, version) => {
|
||||||
if (version > DB_VERSION) {
|
if (version > DB_VERSION) {
|
||||||
throw new Error('cannot migrate db on a downgrade, delete all data first')
|
throw new Error('cannot migrate db on a downgrade, delete all data first')
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let i = version; i < DB_VERSION; i++) {
|
for (let i = version; i < DB_VERSION; i++) {
|
||||||
persistedState = await migrations[i](persistedState)
|
persistedState = migrations[i](persistedState)
|
||||||
}
|
}
|
||||||
|
|
||||||
return persistedState
|
return persistedState
|
||||||
|
|||||||
@@ -15,7 +15,3 @@ export function mergeById<T extends { [id: string]: unknown }>(object: T, source
|
|||||||
export function mapById<T>(object: ById<T>, ids: string[]): T[] {
|
export function mapById<T>(object: ById<T>, ids: string[]): T[] {
|
||||||
return ids.map(id => object[id]).filter(a => a !== undefined)
|
return ids.map(id => object[id]).filter(a => a !== undefined)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function mapId(entities: { id: string }[]): string[] {
|
|
||||||
return entities.map(e => e.id)
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user