xref: /MusicFree/src/core/localMusicSheet.ts (revision 213b6edb3f145274a506fc18582f5b66286568e2)
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    console.log('GGGG', folderPaths);
130    const _importToken = nanoid();
131    importToken = _importToken;
132    const musicList: string[] = [];
133    let peek: string | undefined;
134    let dirFiles: ReadDirItem[] = [];
135    while (folderPaths.length !== 0) {
136        if (importToken !== _importToken) {
137            throw new Error('Import Broken');
138        }
139        peek = folderPaths.shift() as string;
140        try {
141            dirFiles = await readDir(peek);
142        } catch {
143            dirFiles = [];
144        }
145
146        dirFiles.forEach(item => {
147            if (item.isDirectory() && !folderPaths.includes(item.path)) {
148                folderPaths.push(item.path);
149            } else if (localMediaFilter(item.path)) {
150                musicList.push(item.path);
151            }
152        });
153    }
154
155    return {musicList, token: _importToken};
156}
157
158function cancelImportLocal() {
159    importToken = null;
160}
161
162// 导入本地音乐
163const groupNum = 25;
164async function importLocal(_folderPaths: string[]) {
165    const folderPaths = [..._folderPaths.map(it => addFileScheme(it))];
166    const {musicList, token} = await getMusicStats(folderPaths);
167    if (token !== importToken) {
168        throw new Error('Import Broken');
169    }
170    // 分组请求,不然序列化可能出问题
171    let metas: IBasicMeta[] = [];
172    const groups = Math.ceil(musicList.length / groupNum);
173    for (let i = 0; i < groups; ++i) {
174        metas = metas.concat(
175            await mp3Util.getMediaMeta(
176                musicList.slice(i * groupNum, (i + 1) * groupNum),
177            ),
178        );
179    }
180    if (token !== importToken) {
181        throw new Error('Import Broken');
182    }
183    const musicItems: IMusic.IMusicItem[] = await Promise.all(
184        musicList.map(async (musicPath, index) => {
185            let {platform, id, title, artist} =
186                parseFilename(getFileName(musicPath, true)) ?? {};
187            const meta = metas[index];
188            if (!platform || !id) {
189                platform = '本地';
190                id = CryptoJs.MD5(musicPath).toString(CryptoJs.enc.Hex);
191            }
192            return {
193                id,
194                platform,
195                title: title ?? meta?.title ?? getFileName(musicPath),
196                artist: artist ?? meta?.artist ?? '未知歌手',
197                duration: parseInt(meta?.duration ?? '0', 10) / 1000,
198                album: meta?.album ?? '未知专辑',
199                artwork: '',
200                [internalSerializeKey]: {
201                    localPath: musicPath,
202                },
203            } as IMusic.IMusicItem;
204        }),
205    );
206    if (token !== importToken) {
207        throw new Error('Import Broken');
208    }
209    addMusic(musicItems);
210}
211
212/** 是否为本地音乐 */
213function isLocalMusic(
214    musicItem: ICommon.IMediaBase | null,
215): IMusic.IMusicItem | undefined {
216    return musicItem
217        ? localSheet.find(_ => isSameMediaItem(_, musicItem))
218        : undefined;
219}
220
221/** 状态-是否为本地音乐 */
222function useIsLocal(musicItem: IMusic.IMusicItem | null) {
223    const localMusicState = localSheetStateMapper.useMappedState();
224    const [isLocal, setIsLocal] = useState<boolean>(!!isLocalMusic(musicItem));
225    useEffect(() => {
226        if (!musicItem) {
227            setIsLocal(false);
228        } else {
229            setIsLocal(!!isLocalMusic(musicItem));
230        }
231    }, [localMusicState, musicItem]);
232    return isLocal;
233}
234
235function getMusicList() {
236    return localSheet;
237}
238
239async function updateMusicList(newSheet: IMusic.IMusicItem[]) {
240    const _localSheet = [...newSheet];
241    try {
242        await setStorage(StorageKeys.LocalMusicSheet, _localSheet);
243        localSheet = _localSheet;
244        localSheetStateMapper.notify();
245    } catch {}
246}
247
248const LocalMusicSheet = {
249    setup,
250    addMusic,
251    removeMusic,
252    addMusicDraft,
253    saveLocalSheet,
254    importLocal,
255    cancelImportLocal,
256    isLocalMusic,
257    useIsLocal,
258    getMusicList,
259    useMusicList: localSheetStateMapper.useMappedState,
260    updateMusicList,
261};
262
263export default LocalMusicSheet;
264