xref: /MusicFree/src/utils/mediaItem.ts (revision 268ffae051f9727ffd7aa44d5171e698a9dc4fd2)
1import {
2    internalSerializeKey,
3    localPluginPlatform,
4} from '@/constants/commonConst';
5import MediaMeta from '@/core/mediaMeta';
6import produce from 'immer';
7import objectPath from 'object-path';
8
9/** 获取mediakey */
10export function getMediaKey(mediaItem: ICommon.IMediaBase) {
11    return `${mediaItem.platform}@${mediaItem.id}`;
12}
13
14/** 解析mediakey */
15export function parseMediaKey(key: string): ICommon.IMediaBase {
16    try {
17        const str = JSON.parse(key.trim());
18        let platform, id;
19        if (typeof str === 'string') {
20            [platform, id] = str.split('@');
21        } else {
22            platform = str?.platform;
23            id = str?.id;
24        }
25        if (!platform || !id) {
26            throw new Error('mediakey不完整');
27        }
28        return {
29            platform,
30            id,
31        };
32    } catch (e: any) {
33        throw e;
34    }
35}
36
37/** 比较两media是否相同 */
38export function isSameMediaItem(
39    a: ICommon.IMediaBase | null | undefined,
40    b: ICommon.IMediaBase | null | undefined,
41) {
42    return a && b && a.id == b.id && a.platform === b.platform;
43}
44
45/** 查找是否存在 */
46export function includesMedia(
47    a: ICommon.IMediaBase[] | null | undefined,
48    b: ICommon.IMediaBase | null | undefined,
49) {
50    if (!a || !b) {
51        return false;
52    }
53    return a.findIndex(_ => isSameMediaItem(_, b)) !== -1;
54}
55
56/** 获取复位的mediaItem */
57export function resetMediaItem<T extends Partial<ICommon.IMediaBase>>(
58    mediaItem: T,
59    platform?: string,
60    newObj?: boolean,
61): T {
62    // 本地音乐不做处理
63    if (
64        mediaItem.platform === localPluginPlatform ||
65        platform === localPluginPlatform
66    ) {
67        return newObj ? {...mediaItem} : mediaItem;
68    }
69    if (!newObj) {
70        mediaItem.platform = platform ?? mediaItem.platform;
71        mediaItem[internalSerializeKey] = undefined;
72        return mediaItem;
73    } else {
74        return produce(mediaItem, _ => {
75            _.platform = platform ?? mediaItem.platform;
76            _[internalSerializeKey] = undefined;
77        });
78    }
79}
80
81export function mergeProps(
82    mediaItem: ICommon.IMediaBase,
83    props: Record<string, any> | undefined,
84    anotherProps?: Record<string, any> | undefined | null,
85) {
86    return props
87        ? {
88              ...mediaItem,
89              ...props,
90              ...(anotherProps ?? {}),
91              id: mediaItem.id,
92              platform: mediaItem.platform,
93          }
94        : mediaItem;
95}
96
97export enum InternalDataType {
98    LOCALPATH = 'localPath',
99}
100
101export function setInternalData<T extends ICommon.IMediaBase>(
102    mediaItem: T,
103    key: InternalDataType,
104    value: string | number | undefined | null,
105): T {
106    return produce(mediaItem, draft => {
107        objectPath.set(draft, `${internalSerializeKey}.${key}`, value);
108    });
109}
110
111export function getInternalData<T>(
112    mediaItem: ICommon.IMediaBase | null | undefined,
113    key: InternalDataType,
114): T | undefined {
115    if (!mediaItem) {
116        return undefined;
117    }
118    return objectPath.get(mediaItem, `${internalSerializeKey}.${key}`);
119}
120
121export function trimInternalData(
122    mediaItem: ICommon.IMediaBase | null | undefined,
123) {
124    if (!mediaItem) {
125        return undefined;
126    }
127    return {
128        ...mediaItem,
129        [internalSerializeKey]: undefined,
130    };
131}
132
133/** 关联歌词 */
134export async function associateLrc(
135    musicItem: ICommon.IMediaBase,
136    linkto: ICommon.IMediaBase,
137) {
138    if (!musicItem || !linkto) {
139        throw new Error('');
140    }
141    await MediaMeta.update(musicItem, {
142        associatedLrc: linkto,
143    });
144    await MediaMeta.update(linkto, [
145        ['lrc', linkto.lrc],
146        ['$.local.localLrc', linkto.$?.local?.localLrc],
147    ]);
148}
149