-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathcache.native.js
More file actions
137 lines (120 loc) · 4.21 KB
/
Copy pathcache.native.js
File metadata and controls
137 lines (120 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import AsyncStorage from '@react-native-async-storage/async-storage'
import * as FileSystem from 'expo-file-system'
import logger from '~/utils/logger'
// API Cache
export const getCache = async (_cacheName, _key) => {
return null
}
export const getJsonCache = async (_cacheName, key) => {
const json = await AsyncStorage.getItem(key)
return json ? JSON.parse(json) : null
}
export const setJsonCache = async (_cacheName, key, json) => {
if (!json) return
await AsyncStorage.setItem(key, JSON.stringify(json))
}
// Song Cache
export const isSongCached = async (_config, songId, streamFormat, _maxBitrate) => {
return global.listCacheSong?.includes(`${songId}.${streamFormat}`) ? true : false
}
export const getSongCachedInfo = async (_config, songId, streamFormat, _maxBitrate) => {
const pathSong = getPathSong(songId, streamFormat)
return await FileSystem.getInfoAsync(pathSong)
.then(info => {
return [
{ title: 'File', value: `${songId}.${streamFormat}` },
{ title: 'Is cached', value: info.exists ? 'Yes' : 'No' },
{ title: 'Size', value: `${(info.size / (1024 * 1024)).toFixed(2)} MB` },
{ title: 'Modified', value: new Date(info.modificationTime).toLocaleString() },
]
})
.catch(() => null)
}
export const deleteSongCache = async (_config, songId, streamFormat, _maxBitrate) => {
const pathSong = getPathSong(songId, streamFormat)
return await FileSystem.deleteAsync(pathSong)
.then(() => {
global.listCacheSong = global.listCacheSong.filter(file => file !== `${songId}.${streamFormat}`)
})
.catch(() => { })
}
export const getListCacheSong = async () => {
return await FileSystem.readDirectoryAsync(getPathDir())
.catch(() => [])
}
export const getPathSong = (songId, streamFormat) => {
return `${getPathDir()}${songId}.${streamFormat}`
}
// Total size in bytes on disk of the given song ids that are cached.
export const getSongsCacheSize = async (songIds, streamFormat) => {
let total = 0
for (const id of songIds) {
const info = await FileSystem.getInfoAsync(getPathSong(id, streamFormat)).catch(() => null)
if (info?.exists) total += info.size || 0
}
return total
}
const getPathDir = () => {
return `${FileSystem.documentDirectory}/cache/${global.config.folderCache}/songs/`
}
export const initCacheSong = async () => {
// An error was in the past where folderCache was undefined
// So we need to rename it to avoid losing all cached songs
// This can be removed in the future
const info = await FileSystem.getInfoAsync(`${FileSystem.documentDirectory}/cache/undefined/`)
if (info.exists) {
await FileSystem.moveAsync({
from: `${FileSystem.documentDirectory}/cache/undefined`,
to: `${FileSystem.documentDirectory}/cache/${global.config.folderCache}`
})
.catch(error => {
logger.error('initCacheSong', error)
})
.then(() => logger.info('initCacheSong', 'Renamed undefined cache folder to the correct one'))
}
await FileSystem.makeDirectoryAsync(getPathDir(), { intermediates: true })
.catch(error => {
logger.error('initCacheSong', error)
})
global.listCacheSong = await getListCacheSong() || []
}
// Cache Settings
export const clearCache = async () => {
await AsyncStorage.multiRemove(
await AsyncStorage.getAllKeys()
.then(keys => keys.filter(key => key.startsWith('http')))
.catch(() => [])
)
}
export const clearSongCache = async () => {
const pathDir = getPathDir()
await FileSystem.readDirectoryAsync(pathDir)
.then(files => {
const deletePromises = files.map(file => FileSystem.deleteAsync(`${pathDir}${file}`))
return Promise.all(deletePromises)
})
.catch(() => [])
await initCacheSong()
}
export const getStatCache = async () => {
return [
{
name: 'Cache Api',
count: await AsyncStorage.getAllKeys()
.then(keys => keys.filter(key => key.startsWith('http')).length)
.catch(() => 0)
},
{
name: 'Cache Songs',
count: await getListCacheSong()
.then(files => files.length)
.catch(() => 0)
},
{
name: 'Cache Songs Size',
count: await FileSystem.getInfoAsync(getPathDir())
.then(info => `${(info.size / (1024 * 1024)) | 0 || 0} MB`)
.catch(() => '0.00')
},
]
}