mirror of
https://github.com/austinried/subtracks.git
synced 2026-02-10 06:52:43 +01:00
remove thumbnail cache
This commit is contained in:
@@ -2,16 +2,12 @@ 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, CacheImageSize, CacheItemType, CacheItemTypeKey, CacheRequest } from '@app/models/cache'
|
import { CacheFile, 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,9 +8,7 @@ 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),
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -32,7 +30,7 @@ export type CacheSlice = {
|
|||||||
cacheFiles: CacheFilesByServer
|
cacheFiles: CacheFilesByServer
|
||||||
cacheRequests: CacheRequestsByServer
|
cacheRequests: CacheRequestsByServer
|
||||||
|
|
||||||
fetchCoverArtFilePath: (coverArt: string, size?: CacheImageSize) => Promise<string | undefined>
|
fetchCoverArtFilePath: (coverArt: string) => Promise<string | undefined>
|
||||||
|
|
||||||
createCache: (serverId: string) => Promise<void>
|
createCache: (serverId: string) => Promise<void>
|
||||||
prepareCache: (serverId: string) => void
|
prepareCache: (serverId: string) => void
|
||||||
@@ -136,7 +134,7 @@ export const createCacheSlice = (set: SetState<Store>, get: GetState<Store>): Ca
|
|||||||
return await promise
|
return await promise
|
||||||
},
|
},
|
||||||
|
|
||||||
fetchCoverArtFilePath: async (coverArt, size = 'thumbnail') => {
|
fetchCoverArtFilePath: async coverArt => {
|
||||||
const client = get().client
|
const client = get().client
|
||||||
if (!client) {
|
if (!client) {
|
||||||
return
|
return
|
||||||
@@ -147,7 +145,7 @@ export const createCacheSlice = (set: SetState<Store>, get: GetState<Store>): Ca
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const key: CacheItemTypeKey = size === 'thumbnail' ? 'coverArtThumb' : 'coverArt'
|
const key: CacheItemTypeKey = '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]
|
||||||
@@ -158,12 +156,7 @@ export const createCacheSlice = (set: SetState<Store>, get: GetState<Store>): Ca
|
|||||||
return `file://${existing.path}`
|
return `file://${existing.path}`
|
||||||
}
|
}
|
||||||
|
|
||||||
await get().cacheItem(key, coverArt, () =>
|
await get().cacheItem(key, coverArt, () => client.getCoverArtUri({ id: 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}`
|
||||||
},
|
},
|
||||||
@@ -178,9 +171,7 @@ export const createCacheSlice = (set: SetState<Store>, get: GetState<Store>): Ca
|
|||||||
state.cacheFiles[serverId] = {
|
state.cacheFiles[serverId] = {
|
||||||
song: {},
|
song: {},
|
||||||
coverArt: {},
|
coverArt: {},
|
||||||
coverArtThumb: {},
|
|
||||||
artistArt: {},
|
artistArt: {},
|
||||||
artistArtThumb: {},
|
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
@@ -195,18 +186,14 @@ 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: {},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
@@ -273,9 +260,7 @@ 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 } from '@app/util/state'
|
import { reduceById, mergeById, mapId } 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,7 +547,3 @@ function mapSong(song: ChildElement): Song {
|
|||||||
starred: song.starred,
|
starred: song.starred,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function mapId(entities: { id: string }[]): string[] {
|
|
||||||
return entities.map(e => e.id)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,9 +1,26 @@
|
|||||||
const migrations: Array<(state: any) => any> = [
|
import { Store } from './store'
|
||||||
state => {
|
import RNFS from 'react-native-fs'
|
||||||
for (let server of state.settings.servers) {
|
import { rmdir } from '@app/util/fs'
|
||||||
|
|
||||||
|
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: (persistedState, version) => {
|
migrate: async (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 = migrations[i](persistedState)
|
persistedState = await migrations[i](persistedState)
|
||||||
}
|
}
|
||||||
|
|
||||||
return persistedState
|
return persistedState
|
||||||
|
|||||||
@@ -15,3 +15,7 @@ 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