From d8720876857c61e030c8c07d3835d4a1e3f9aab9 Mon Sep 17 00:00:00 2001 From: vapormusic Date: Thu, 30 Jun 2022 22:34:00 +0700 Subject: [PATCH] some test --- package.json | 2 ++ src/main/base/browserwindow.ts | 2 ++ src/main/providers/local/db/index.ts | 10 +++++++--- src/main/providers/local/index.ts | 25 +++++++++++++++++++------ 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index e3c81145..64b8ec00 100644 --- a/package.json +++ b/package.json @@ -68,11 +68,13 @@ "node-ssdp": "^4.0.1", "pouchdb-adapter-leveldb": "^7.3.0", "pouchdb-node": "^7.3.0", + "pouchdb-upsert": "^2.2.0", "qrcode": "^1.5.0", "react": "^18.0.0", "react-dom": "^18.0.0", "run-script-os": "^1.1.6", "source-map-support": "^0.5.21", + "ts-md5": "^1.2.11", "v8-compile-cache": "^2.3.0", "wallpaper": "5.0.1", "ws": "^8.5.0", diff --git a/src/main/base/browserwindow.ts b/src/main/base/browserwindow.ts index b3822530..e4a53270 100644 --- a/src/main/base/browserwindow.ts +++ b/src/main/base/browserwindow.ts @@ -1185,6 +1185,8 @@ export class BrowserWindow { ipcMain.handle("scanLibrary", async (event, folders) => { + const oldmetadatalist = await LocalFiles.sendOldLibrary() + BrowserWindow.win.webContents.send('getUpdatedLocalList', oldmetadatalist); const metadatalist = await LocalFiles.scanLibrary() BrowserWindow.win.webContents.send('getUpdatedLocalList', metadatalist); }) diff --git a/src/main/providers/local/db/index.ts b/src/main/providers/local/db/index.ts index 0add4b93..89063d20 100644 --- a/src/main/providers/local/db/index.ts +++ b/src/main/providers/local/db/index.ts @@ -1,8 +1,12 @@ import * as PouchDB from 'pouchdb-node'; - +import {join} from 'path'; +import {app} from "electron"; +PouchDB.plugin(require('pouchdb-upsert')); export class ProviderDB { public static db: any = null - static async init() { - // ProviderDB.db = new PouchDB('tracks') + static init() { + if (ProviderDB.db == null){ + ProviderDB.db = new PouchDB(join(app.getPath('userData'), 'tracksdb')) + } } } \ No newline at end of file diff --git a/src/main/providers/local/index.ts b/src/main/providers/local/index.ts index fe12f368..6d0db3b5 100644 --- a/src/main/providers/local/index.ts +++ b/src/main/providers/local/index.ts @@ -1,16 +1,26 @@ -// import { ProviderDB } from "./db"; +import { ProviderDB } from "./db"; import * as path from 'path'; const { readdir } = require('fs').promises; import { utils } from '../../base/utils'; import * as mm from 'music-metadata'; +import {Md5} from 'ts-md5/dist/md5'; export class LocalFiles { static localSongs: any = []; static localSongsArts: any = []; - // public static DB = ProviderDB.db; - + public static DB = ProviderDB.db; + static async sendOldLibrary() { + ProviderDB.init() + let u = (await ProviderDB.db.allDocs({include_docs: true, + attachments: true})).rows.map((item: any)=>{return item.doc}) + this.localSongs = u; + console.log('sdadad', u.length) + return u; + } + static async scanLibrary() { + ProviderDB.init() let folders = utils.getStoreValue("libraryPrefs.localPaths") if (folders == null || folders.length == null || folders.length == 0) folders = ["D:\\Music"] console.log('folders', folders) @@ -30,16 +40,18 @@ export class LocalFiles { for (var audio of audiofiles) { try { const metadata = await mm.parseFile(audio); + let lochash = Md5.hashStr(audio) ?? numid; if (metadata != null) { let form = { - "id": "ciderlocal" + numid, + "id": "ciderlocal" + lochash, + "_id": "ciderlocal" + lochash, "type": "podcast-episodes", "href": audio, "attributes": { "artwork": { "width": 3000, "height": 3000, - "url": "/ciderlocalart/" + "ciderlocal" + numid, + "url": "/ciderlocalart/" + "ciderlocal" + lochash, }, "topics": [], "url": "", @@ -74,10 +86,11 @@ export class LocalFiles { } }; metadatalistart.push({ - id: "ciderlocal" + numid, + id: "ciderlocal" + lochash, url: metadata.common.picture != undefined ? metadata.common.picture[0].data.toString('base64') : "", }) numid += 1; + ProviderDB.db.putIfNotExists(form) metadatalist.push(form) } } catch (e) { }