Skip to content

Commit 938f27c

Browse files
committed
fix: restore legacy download metadata during migration
1 parent bae8700 commit 938f27c

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

src/renderer/mainWindow/core/legacyMigration.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import { syncKV, asyncKV } from '@renderer/common/kvStore';
1717
import { showModal } from '@renderer/mainWindow/components/ui/Modal/modalManager';
1818
import type { IBackupData, IBackupSheet } from '@appTypes/infra/backup';
19+
import { DOWNLOADED_SHEET_ID } from '@infra/musicSheet/common/constant';
1920

2021
// ─── 常量 ───
2122

@@ -243,6 +244,20 @@ function sanitizeMusicItem(raw: any): IMusic.IMusicItem | null {
243244
return item as IMusic.IMusicItem;
244245
}
245246

247+
function getLegacyDownloadData(item: IMusic.IMusicItem): {
248+
path: string;
249+
quality: IMusic.IQualityKey;
250+
} | null {
251+
const downloadData = (item as any).$?.downloadData;
252+
if (!downloadData?.path || !downloadData?.quality) {
253+
return null;
254+
}
255+
return {
256+
path: downloadData.path,
257+
quality: downloadData.quality,
258+
};
259+
}
260+
246261
// ─── 执行迁移 ───
247262

248263
/**
@@ -257,6 +272,7 @@ function sanitizeMusicItem(raw: any): IMusic.IMusicItem | null {
257272
export async function executeMigration(): Promise<MigrationResult> {
258273
// 延迟导入避免循环依赖
259274
const { default: backup } = await import('@infra/backup/renderer');
275+
const { default: mediaMeta } = await import('@infra/mediaMeta/renderer');
260276
const { default: musicSheet, playQueueBridge } = await import('@infra/musicSheet/renderer');
261277
const { default: fsUtil } = await import('@infra/fsUtil/renderer');
262278

@@ -297,6 +313,13 @@ export async function executeMigration(): Promise<MigrationResult> {
297313

298314
// 组装 IBackupSheet[]
299315
const backupSheets: IBackupSheet[] = [];
316+
const downloadedItems = new Map<
317+
string,
318+
{
319+
item: IMusic.IMusicItem;
320+
downloadData: { path: string; quality: IMusic.IQualityKey };
321+
}
322+
>();
300323
let totalSongs = 0;
301324

302325
for (const sheet of sheets) {
@@ -306,6 +329,14 @@ export async function executeMigration(): Promise<MigrationResult> {
306329
const item = musicPool.get(`${ref.platform}@${ref.id}`);
307330
if (item) {
308331
musicList.push(item);
332+
333+
const downloadData = getLegacyDownloadData(item);
334+
if (downloadData) {
335+
downloadedItems.set(`${item.platform}@${item.id}`, {
336+
item,
337+
downloadData,
338+
});
339+
}
309340
}
310341
}
311342

@@ -335,6 +366,19 @@ export async function executeMigration(): Promise<MigrationResult> {
335366
encoding: 'utf-8',
336367
});
337368
await backup.restoreFromFile(tempPath, 'append');
369+
370+
if (downloadedItems.size > 0) {
371+
for (const [, { item, downloadData }] of downloadedItems) {
372+
await mediaMeta.setMeta(item.platform, String(item.id), {
373+
downloadData,
374+
});
375+
}
376+
377+
musicSheet.addMusicToSheet(
378+
Array.from(downloadedItems.values(), ({ item }) => item),
379+
DOWNLOADED_SHEET_ID,
380+
);
381+
}
338382
} finally {
339383
// 无论成功失败均删除临时文件
340384
try {

0 commit comments

Comments
 (0)