refactor: use dexie.js instead of localforage

master
qier222 4 years ago
parent bf61fe6e28
commit fee97f7f3c
No known key found for this signature in database
GPG Key ID: 9C85007ED905F14D

@ -33,6 +33,7 @@
"core-js": "^3.6.5", "core-js": "^3.6.5",
"crypto-js": "^4.0.0", "crypto-js": "^4.0.0",
"dayjs": "^1.8.36", "dayjs": "^1.8.36",
"dexie": "^3.0.3",
"discord-rich-presence": "^0.0.8", "discord-rich-presence": "^0.0.8",
"electron": "^12.0.0", "electron": "^12.0.0",
"electron-builder": "^22.10.5", "electron-builder": "^22.10.5",
@ -50,7 +51,6 @@
"extract-zip": "^2.0.1", "extract-zip": "^2.0.1",
"howler": "^2.2.0", "howler": "^2.2.0",
"js-cookie": "^2.2.1", "js-cookie": "^2.2.1",
"localforage": "^1.9.0",
"lodash": "^4.17.20", "lodash": "^4.17.20",
"nprogress": "^0.2.0", "nprogress": "^0.2.0",
"pac-proxy-agent": "^4.1.0", "pac-proxy-agent": "^4.1.0",

@ -1,8 +1,7 @@
import { getTrackDetail, scrobble, getMP3 } from "@/api/track"; import { getTrackDetail, scrobble, getMP3 } from "@/api/track";
import { shuffle } from "lodash"; import { shuffle } from "lodash";
import { Howler, Howl } from "howler"; import { Howler, Howl } from "howler";
import localforage from "localforage"; import { cacheTrackSource, getTrackSource } from "@/utils/db";
import { cacheTrack } from "@/utils/db";
import { getAlbum } from "@/api/album"; import { getAlbum } from "@/api/album";
import { getPlaylistDetail } from "@/api/playlist"; import { getPlaylistDetail } from "@/api/playlist";
import { getArtist } from "@/api/artist"; import { getArtist } from "@/api/artist";
@ -201,10 +200,9 @@ export default class {
}); });
} }
_getAudioSourceFromCache(id) { _getAudioSourceFromCache(id) {
let tracks = localforage.createInstance({ name: "tracks" }); return getTrackSource(id).then((t) => {
return tracks.getItem(id).then((t) => { if (!t) return null;
if (t === null) return null; const source = URL.createObjectURL(new Blob([t.source]));
const source = URL.createObjectURL(new Blob([t.mp3]));
return source; return source;
}); });
} }
@ -216,7 +214,7 @@ export default class {
if (result.data[0].freeTrialInfo !== null) return null; // 跳过只能试听的歌曲 if (result.data[0].freeTrialInfo !== null) return null; // 跳过只能试听的歌曲
const source = result.data[0].url.replace(/^http:/, "https:"); const source = result.data[0].url.replace(/^http:/, "https:");
if (store.state.settings.automaticallyCacheSongs) { if (store.state.settings.automaticallyCacheSongs) {
cacheTrack(track.id, source); cacheTrackSource(track, source, result.data[0].br);
} }
return source; return source;
}); });
@ -230,7 +228,8 @@ export default class {
if (process.env.IS_ELECTRON !== true) return null; if (process.env.IS_ELECTRON !== true) return null;
const source = ipcRenderer.sendSync("unblock-music", track); const source = ipcRenderer.sendSync("unblock-music", track);
if (store.state.settings.automaticallyCacheSongs && source?.url) { if (store.state.settings.automaticallyCacheSongs && source?.url) {
cacheTrack(track.id, source.url); // TODO: 将unblockMusic字样换成真正的来源比如酷我咪咕等
cacheTrackSource(track, source.url, 128000, "unblockMusic");
} }
return source?.url; return source?.url;
} }

@ -1,54 +1,66 @@
import axios from "axios"; import axios from "axios";
import localforage from "localforage"; import Dexie from "dexie";
// import pkg from "../../package.json";
export function cacheTrack(id, url) { const db = new Dexie("yesplaymusic");
let tracks = localforage.createInstance({ db.version(1).stores({
name: "tracks", trackSources: "&id",
}); });
// TODO: limit cache songs number
// tracks.length().then(function (length) {
// if (length > 2) {
// tracks.keys().then(function (keys) {
// tracks.removeItem(keys[keys.length - 2]);
// });
// }
// });
// TODO: cache track details export function cacheTrackSource(trackInfo, url, bitRate, from = "netease") {
const name = trackInfo.name;
const artist = trackInfo.ar[0]?.name || trackInfo.artists[0]?.name;
return axios return axios
.get(url, { .get(url, {
responseType: "arraybuffer", responseType: "arraybuffer",
}) })
.then((response) => { .then((response) => {
tracks.setItem(`${id}`, { mp3: response.data }); db.trackSources.put({
return { mp3: response.data }; id: trackInfo.id,
source: response.data,
bitRate,
from,
name,
artist,
});
console.debug(`[debug][db.js] cached track 👉 ${name} by ${artist}`);
return { trackID: trackInfo.id, source: response.data, bitRate };
}); });
} }
export function countDBSize(dbName) { export function getTrackSource(id) {
let db = localforage.createInstance({ return db.trackSources.get(Number(id)).then((track) => {
name: dbName, if (!track) return null;
console.debug(
`[debug][db.js] get track from cache 👉 ${track.name} by ${track.artist}`
);
return track;
}); });
}
export function countDBSize() {
let trackSizes = []; let trackSizes = [];
return db return db.trackSources
.iterate((value) => { .each((track) => {
trackSizes.push(value.mp3.byteLength); trackSizes.push(track.source.byteLength);
}) })
.then(() => { .then(() => {
return { return {
bytes: trackSizes.reduce((s1, s2) => s1 + s2, 0), bytes: trackSizes.reduce((s1, s2) => s1 + s2, 0),
length: trackSizes.length, length: trackSizes.length,
}; };
})
.catch((err) => {
console.log(err);
}); });
} }
export function clearDB(dbName) { export function clearDB() {
let db = localforage.createInstance({ return new Promise((resolve) => {
name: dbName, db.tables.forEach(function (table) {
table.clear();
});
resolve();
}); });
return db.clear();
} }
window.cacheTrackSource = cacheTrackSource;
window.db = db;
window.countDBSize = countDBSize;

@ -54,9 +54,15 @@ const updatePlayer = () => {
localStorage.setItem("player", JSON.stringify(data)); localStorage.setItem("player", JSON.stringify(data));
}; };
const removeOldStuff = () => {
// remove old indexedDB databases created by localforage
indexedDB.deleteDatabase("tracks");
};
export default function () { export default function () {
updateSetting(); updateSetting();
updateData(); updateData();
updatePlayer(); updatePlayer();
removeOldStuff();
localStorage.setItem("appVersion", JSON.stringify(pkg.version)); localStorage.setItem("appVersion", JSON.stringify(pkg.version));
} }

@ -122,7 +122,7 @@
> >
</div> </div>
<div class="right"> <div class="right">
<button @click="clearCache('tracks')"> <button @click="clearCache()">
{{ $t("settings.clearSongsCache") }} {{ $t("settings.clearSongsCache") }}
</button> </button>
</div> </div>
@ -365,7 +365,7 @@ export default {
set(value) { set(value) {
if (value === this.settings.musicQuality) return; if (value === this.settings.musicQuality) return;
this.$store.commit("changeMusicQuality", value); this.$store.commit("changeMusicQuality", value);
this.clearCache("tracks"); this.clearCache();
}, },
}, },
lyricFontSize: { lyricFontSize: {
@ -443,7 +443,7 @@ export default {
value, value,
}); });
if (value === false) { if (value === false) {
this.clearCache("tracks"); this.clearCache();
} }
}, },
}, },
@ -520,8 +520,8 @@ export default {
doLogout(); doLogout();
this.$router.push({ name: "home" }); this.$router.push({ name: "home" });
}, },
countDBSize(dbName) { countDBSize() {
countDBSize(dbName).then((data) => { countDBSize().then((data) => {
if (data === undefined) { if (data === undefined) {
this.tracksCache = { this.tracksCache = {
size: "0KB", size: "0KB",
@ -533,10 +533,9 @@ export default {
this.tracksCache.length = data.length; this.tracksCache.length = data.length;
}); });
}, },
clearCache(dbName) { clearCache() {
// TODO: toast clearDB().then(() => {
clearDB(dbName).then(() => { this.countDBSize();
this.countDBSize("tracks");
}); });
}, },
lastfmConnect() { lastfmConnect() {

@ -4258,6 +4258,11 @@ detect-node@^2.0.4:
resolved "https://registry.yarnpkg.com/detect-node/-/detect-node-2.0.4.tgz#014ee8f8f669c5c58023da64b8179c083a28c46c" resolved "https://registry.yarnpkg.com/detect-node/-/detect-node-2.0.4.tgz#014ee8f8f669c5c58023da64b8179c083a28c46c"
integrity sha512-ZIzRpLJrOj7jjP2miAtgqIfmzbxa4ZOr5jJc601zklsfEx9oTzmmj2nVpIPRpNlRTIh8lc1kyViIY7BWSGNmKw== integrity sha512-ZIzRpLJrOj7jjP2miAtgqIfmzbxa4ZOr5jJc601zklsfEx9oTzmmj2nVpIPRpNlRTIh8lc1kyViIY7BWSGNmKw==
dexie@^3.0.3:
version "3.0.3"
resolved "https://registry.yarnpkg.com/dexie/-/dexie-3.0.3.tgz#ede63849dfe5f07e13e99bb72a040e8ac1d29dab"
integrity sha512-BSFhGpngnCl1DOr+8YNwBDobRMH0ziJs2vts69VilwetHYOtEDcLqo7d/XiIphM0tJZ2rPPyAGd31lgH2Ln3nw==
dicer@0.3.0: dicer@0.3.0:
version "0.3.0" version "0.3.0"
resolved "https://registry.yarnpkg.com/dicer/-/dicer-0.3.0.tgz#eacd98b3bfbf92e8ab5c2fdb71aaac44bb06b872" resolved "https://registry.yarnpkg.com/dicer/-/dicer-0.3.0.tgz#eacd98b3bfbf92e8ab5c2fdb71aaac44bb06b872"
@ -7319,13 +7324,6 @@ localforage@1.8.1:
dependencies: dependencies:
lie "3.1.1" lie "3.1.1"
localforage@^1.9.0:
version "1.9.0"
resolved "https://registry.yarnpkg.com/localforage/-/localforage-1.9.0.tgz#f3e4d32a8300b362b4634cc4e066d9d00d2f09d1"
integrity sha512-rR1oyNrKulpe+VM9cYmcFn6tsHuokyVHFaCM3+osEmxaHTbEk8oQu6eGDfS6DQLWi/N67XRmB8ECG37OES368g==
dependencies:
lie "3.1.1"
locate-path@^3.0.0: locate-path@^3.0.0:
version "3.0.0" version "3.0.0"
resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-3.0.0.tgz#dbec3b3ab759758071b58fe59fc41871af21400e" resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-3.0.0.tgz#dbec3b3ab759758071b58fe59fc41871af21400e"

Loading…
Cancel
Save