1import { 2 internalSerializeKey, 3 StorageKeys, 4 supportLocalMediaType, 5} from '@/constants/commonConst'; 6import mp3Util, {IBasicMeta} from '@/native/mp3Util'; 7import { 8 getInternalData, 9 InternalDataType, 10 isSameMediaItem, 11} from '@/utils/mediaItem'; 12import StateMapper from '@/utils/stateMapper'; 13import {getStorage, setStorage} from '@/utils/storage'; 14import {nanoid} from 'nanoid'; 15import {useEffect, useState} from 'react'; 16import {exists, readDir, ReadDirItem, unlink} from 'react-native-fs'; 17import {addFileScheme, getFileName} from '@/utils/fileUtils.ts'; 18import CryptoJs from 'crypto-js'; 19 20let localSheet: IMusic.IMusicItem[] = []; 21const localSheetStateMapper = new StateMapper(() => localSheet); 22 23export async function setup() { 24 const sheet = await getStorage(StorageKeys.LocalMusicSheet); 25 if (sheet) { 26 let validSheet: IMusic.IMusicItem[] = []; 27 for (let musicItem of sheet) { 28 const localPath = getInternalData<string>( 29 musicItem, 30 InternalDataType.LOCALPATH, 31 ); 32 if (localPath && (await exists(localPath))) { 33 validSheet.push(musicItem); 34 } 35 } 36 if (validSheet.length !== sheet.length) { 37 await setStorage(StorageKeys.LocalMusicSheet, validSheet); 38 } 39 localSheet = validSheet; 40 } else { 41 await setStorage(StorageKeys.LocalMusicSheet, []); 42 } 43 localSheetStateMapper.notify(); 44} 45 46export async function addMusic( 47 musicItem: IMusic.IMusicItem | IMusic.IMusicItem[], 48) { 49 if (!Array.isArray(musicItem)) { 50 musicItem = [musicItem]; 51 } 52 let newSheet = [...localSheet]; 53 musicItem.forEach(mi => { 54 if (localSheet.findIndex(_ => isSameMediaItem(mi, _)) === -1) { 55 newSheet.push(mi); 56 } 57 }); 58 await setStorage(StorageKeys.LocalMusicSheet, newSheet); 59 localSheet = newSheet; 60 localSheetStateMapper.notify(); 61} 62 63function addMusicDraft(musicItem: IMusic.IMusicItem | IMusic.IMusicItem[]) { 64 if (!Array.isArray(musicItem)) { 65 musicItem = [musicItem]; 66 } 67 let newSheet = [...localSheet]; 68 musicItem.forEach(mi => { 69 if (localSheet.findIndex(_ => isSameMediaItem(mi, _)) === -1) { 70 newSheet.push(mi); 71 } 72 }); 73 localSheet = newSheet; 74 localSheetStateMapper.notify(); 75} 76 77async function saveLocalSheet() { 78 await setStorage(StorageKeys.LocalMusicSheet, localSheet); 79} 80 81export async function removeMusic( 82 musicItem: IMusic.IMusicItem, 83 deleteOriginalFile = false, 84) { 85 const idx = localSheet.findIndex(_ => isSameMediaItem(_, musicItem)); 86 let newSheet = [...localSheet]; 87 if (idx !== -1) { 88 const localMusicItem = localSheet[idx]; 89 newSheet.splice(idx, 1); 90 const localPath = 91 musicItem[internalSerializeKey]?.localPath ?? 92 localMusicItem[internalSerializeKey]?.localPath; 93 if (deleteOriginalFile && localPath) { 94 try { 95 await unlink(localPath); 96 } catch (e: any) { 97 if (e.message !== 'File does not exist') { 98 throw e; 99 } 100 } 101 } 102 } 103 localSheet = newSheet; 104 localSheetStateMapper.notify(); 105 saveLocalSheet(); 106} 107 108function parseFilename(fn: string): Partial<IMusic.IMusicItem> | null { 109 const data = fn.slice(0, fn.lastIndexOf('.')).split('@'); 110 const [platform, id, title, artist] = data; 111 if (!platform || !id) { 112 return null; 113 } 114 return { 115 id, 116 platform: platform, 117 title: title ?? '', 118 artist: artist ?? '', 119 }; 120} 121 122function localMediaFilter(filename: string) { 123 return supportLocalMediaType.some(ext => filename.endsWith(ext)); 124} 125 126let importToken: string | null = null; 127// 获取本地的文件列表 128async function getMusicStats(folderPaths: string[]) { 129 const _importToken = nanoid(); 130 importToken = _importToken; 131 const musicList: string[] = []; 132 let peek: string | undefined; 133 let dirFiles: ReadDirItem[] = []; 134 while (folderPaths.length !== 0) { 135 if (importToken !== _importToken) { 136 throw new Error('Import Broken'); 137 } 138 peek = folderPaths.shift() as string; 139 try { 140 dirFiles = await readDir(peek); 141 } catch { 142 dirFiles = []; 143 } 144 145 dirFiles.forEach(item => { 146 if (item.isDirectory() && !folderPaths.includes(item.path)) { 147 folderPaths.push(item.path); 148 } else if (localMediaFilter(item.path)) { 149 musicList.push(item.path); 150 } 151 }); 152 } 153 154 return {musicList, token: _importToken}; 155} 156 157function cancelImportLocal() { 158 importToken = null; 159} 160 161// 导入本地音乐 162const groupNum = 25; 163async function importLocal(_folderPaths: string[]) { 164 const folderPaths = [..._folderPaths.map(it => addFileScheme(it))]; 165 const {musicList, token} = await getMusicStats(folderPaths); 166 if (token !== importToken) { 167 throw new Error('Import Broken'); 168 } 169 // 分组请求,不然序列化可能出问题 170 let metas: IBasicMeta[] = []; 171 const groups = Math.ceil(musicList.length / groupNum); 172 for (let i = 0; i < groups; ++i) { 173 metas = metas.concat( 174 await mp3Util.getMediaMeta( 175 musicList.slice(i * groupNum, (i + 1) * groupNum), 176 ), 177 ); 178 } 179 if (token !== importToken) { 180 throw new Error('Import Broken'); 181 } 182 const musicItems: IMusic.IMusicItem[] = await Promise.all( 183 musicList.map(async (musicPath, index) => { 184 let {platform, id, title, artist} = 185 parseFilename(getFileName(musicPath, true)) ?? {}; 186 const meta = metas[index]; 187 if (!platform || !id) { 188 platform = '本地'; 189 id = CryptoJs.MD5(musicPath).toString(CryptoJs.enc.Hex); 190 } 191 return { 192 id, 193 platform, 194 title: title ?? meta?.title ?? getFileName(musicPath), 195 artist: artist ?? meta?.artist ?? '未知歌手', 196 duration: parseInt(meta?.duration ?? '0', 10) / 1000, 197 album: meta?.album ?? '未知专辑', 198 artwork: '', 199 [internalSerializeKey]: { 200 localPath: musicPath, 201 }, 202 } as IMusic.IMusicItem; 203 }), 204 ); 205 if (token !== importToken) { 206 throw new Error('Import Broken'); 207 } 208 addMusic(musicItems); 209} 210 211/** 是否为本地音乐 */ 212function isLocalMusic( 213 musicItem: ICommon.IMediaBase | null, 214): IMusic.IMusicItem | undefined { 215 return musicItem 216 ? localSheet.find(_ => isSameMediaItem(_, musicItem)) 217 : undefined; 218} 219 220/** 状态-是否为本地音乐 */ 221function useIsLocal(musicItem: IMusic.IMusicItem | null) { 222 const localMusicState = localSheetStateMapper.useMappedState(); 223 const [isLocal, setIsLocal] = useState<boolean>(!!isLocalMusic(musicItem)); 224 useEffect(() => { 225 if (!musicItem) { 226 setIsLocal(false); 227 } else { 228 setIsLocal(!!isLocalMusic(musicItem)); 229 } 230 }, [localMusicState, musicItem]); 231 return isLocal; 232} 233 234function getMusicList() { 235 return localSheet; 236} 237 238async function updateMusicList(newSheet: IMusic.IMusicItem[]) { 239 const _localSheet = [...newSheet]; 240 try { 241 await setStorage(StorageKeys.LocalMusicSheet, _localSheet); 242 localSheet = _localSheet; 243 localSheetStateMapper.notify(); 244 } catch {} 245} 246 247const LocalMusicSheet = { 248 setup, 249 addMusic, 250 removeMusic, 251 addMusicDraft, 252 saveLocalSheet, 253 importLocal, 254 cancelImportLocal, 255 isLocalMusic, 256 useIsLocal, 257 getMusicList, 258 useMusicList: localSheetStateMapper.useMappedState, 259 updateMusicList, 260}; 261 262export default LocalMusicSheet; 263