From 0bb5c4d864c08f9db07b4aff4b0e56e4b2fcce7e Mon Sep 17 00:00:00 2001 From: Jixun Wu Date: Fri, 7 Jul 2023 12:45:31 +0100 Subject: [PATCH] strict type check for ncm metadata #48 --- package-lock.json | 4 ++-- package.json | 2 +- src/decrypt/ncm.ts | 25 ++++++++++++++++++------- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/package-lock.json b/package-lock.json index efcbfea..7457ca0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "unlock-music", - "version": "1.10.4", + "version": "1.10.5", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "unlock-music", - "version": "1.10.4", + "version": "1.10.5", "hasInstallScript": true, "license": "MIT", "dependencies": { diff --git a/package.json b/package.json index fc70d97..1d4549b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "unlock-music", - "version": "1.10.4", + "version": "1.10.5", "ext_build": 0, "updateInfo": "完善音乐标签编辑功能,支持编辑更多标签", "license": "MIT", diff --git a/src/decrypt/ncm.ts b/src/decrypt/ncm.ts index c2f9851..e075cf1 100644 --- a/src/decrypt/ncm.ts +++ b/src/decrypt/ncm.ts @@ -139,7 +139,7 @@ class NcmDecrypt { } else { result = JSON.parse(plainText.slice(labelIndex + 1)); } - if (!!result.albumPic) { + if (result.albumPic) { result.albumPic = result.albumPic.replace('http://', 'https://') + '?param=500y500'; } return result; @@ -160,11 +160,20 @@ class NcmDecrypt { // build artists let artists: string[] = []; - if (!!this.oriMeta.artist) { - this.oriMeta.artist.forEach((arr) => artists.push(arr[0])); + if (typeof this.oriMeta.artist === 'string') { + // v3.0: artist 现在可能是字符串了? + artists.push(this.oriMeta.artist); + } else if (Array.isArray(this.oriMeta.artist)) { + this.oriMeta.artist.forEach((artist) => { + if (typeof artist === 'string') { + artists.push(artist); + } else if (Array.isArray(artist) && artist[0] && typeof artist[0] === 'string') { + artists.push(artist[0]); + } + }); } - if (artists.length === 0 && !!info.artist) { + if (artists.length === 0 && info.artist) { artists = info.artist .split(',') .map((val) => val.trim()) @@ -180,7 +189,7 @@ class NcmDecrypt { this.image.buffer = await img.getBufferAsync('image/jpeg'); } } catch (e) { - console.log('get cover image failed', e); + console.log('fetch cover image failed', e); } this.newMeta = { title: info.title, artists, album: this.oriMeta.album, picture: this.image?.buffer }; @@ -226,12 +235,14 @@ class NcmDecrypt { this.audio = this._getAudio(keyBox); this.format = this.oriMeta.format || SniffAudioExt(this.audio); this.mime = AudioMimeType[this.format]; - await this._buildMeta(); + try { + await this._buildMeta(); await this._writeMeta(); } catch (e) { - console.warn('write meta data failed', e); + console.warn('build/write meta failed, skip.', e); } + return this.gatherResult(); } }