Browse Source

解决bug 封面为空的音乐 不能一键扫描请求在线封面

onecold 1 năm trước cách đây
mục cha
commit
cb233a0c83

+ 3 - 2
entry/src/main/ets/common/util/MediaTable.ets

@@ -20,9 +20,10 @@ export default class MediaTable {
     this.accountTable.getRdbStore(context,callback);
   }
 
-  insert(item: VideoItem, callback: Function) {
+
+  insert(item: VideoItem, callback: Function,cover_api?:string) {
     const valueBucket: relationalStore.ValuesBucket = generateBucket(item);
-    this.accountTable.insertData(valueBucket, callback);
+    this.accountTable.insertData(valueBucket, callback,cover_api);
   }
 
   deleteData(item: VideoItem, callback: Function) {

+ 9 - 5
entry/src/main/ets/common/util/NetAxiosUtil.ets

@@ -61,14 +61,18 @@ class NetAxiosUtil{
 
 
 
-  async getLyricCover(title: string, artist: string): Promise<string> {
+  async getLyricCover(title: string, artist: string,cover_api?:string): Promise<string> {
 
     try {
-      let baseUrl =  PreferencesUtil.getStringSync('COVER_API','')
-      if ( baseUrl=='') {
-        LogUtil.debug("Heanup 未设置API");
-        return ''
+      let baseUrl = cover_api
+      if(cover_api===undefined&&StrUtil.isEmpty(cover_api)){
+        baseUrl =  PreferencesUtil.getStringSync('COVER_API','')
+        if ( baseUrl=='') {
+          LogUtil.debug("Heanup 未设置API");
+          return ''
+        }
       }
+
       const cover_url = baseUrl
         + '?title=' + encodeURIComponent(title.trim()) + '&artist=' + encodeURIComponent(artist.trim());
       const httpRequest = http.createHttp();

+ 4 - 4
entry/src/main/ets/common/util/RdbUtils.ets

@@ -92,7 +92,7 @@ export default class RdbUtils {
 
 
   //检查是否存在相同id的记录,存在则不插入,进一步判断pixelMapPath字段,如果旧值为空而新值不为空,则更新该字段。
-  async insertData(data: relationalStore.ValuesBucket, callback: Function = () => {}) {
+  async insertData(data: relationalStore.ValuesBucket, callback: Function = () => {},cover_api?:string) {
     if (!callback || typeof callback !== 'function') {
       Logger.info(RdbUtils.RDB_TAG, 'insertData() has no valid callback!');
       return;
@@ -125,8 +125,8 @@ export default class RdbUtils {
             const existingPixelMapPath = resultSet.getString(resultSet.getColumnIndex('pixelMapPath'));
             const name = resultSet.getString(resultSet.getColumnIndex('name'));
             const artist = resultSet.getString(resultSet.getColumnIndex('artist'));
-            if ((!existingPixelMapPath || existingPixelMapPath.trim() === '') ) {
-              let newPixelMapPath =  await NetAxiosUtil.getLyricCover(name,artist)
+            if ((!existingPixelMapPath || existingPixelMapPath.trim() === '')&& cover_api!==undefined) {
+              let newPixelMapPath =  await NetAxiosUtil.getLyricCover(name,artist,cover_api)
               console.info('onecold getLyricCover success. name= '+name);
               console.info('onecold getLyricCover success. newPixelMapPath= '+newPixelMapPath);
               if(StrUtil.isNotEmpty(newPixelMapPath)){
@@ -160,7 +160,7 @@ export default class RdbUtils {
 
         //不存在相同记录 ,判断要不要去请求getLyricCover
         if(pixelMapPathV==undefined||pixelMapPathV.toString()==='' ){
-          valueBucket.pixelMapPath = await NetAxiosUtil.getLyricCover(titleNameV+'',artistV+'')
+          valueBucket.pixelMapPath = await NetAxiosUtil.getLyricCover(titleNameV+'',artistV+'',cover_api)
 
         }
         console.info('onecold getLyricCover pixelMapPathV1= '+pixelMapPathV);

+ 4 - 5
entry/src/main/ets/pages/ScanFilePage.ets

@@ -62,7 +62,6 @@ export struct ScanFilePage{
     this.lockPath =  this.rootPath +'/'+ LocalMusic.STR_LOCK_VIDEO
     LogUtil.info('onecold 文件扫描 aboutToAppear')
 
-
   }
 
   endScan(){
@@ -112,7 +111,8 @@ export struct ScanFilePage{
   doOptimize(){
 
     this.initState()
-    const task = new taskpool.Task(scanDirectoryTask, getContext(this), this.rootPath,  this.lockPath);
+    const task = new taskpool.Task(scanDirectoryTask, getContext(this), this.rootPath,
+      this.lockPath,PreferencesUtil.getStringSync('COVER_API',''));
     taskpool.execute(task, taskpool.Priority.HIGH).then(()=>{
       this.endScan()
     }).catch((e:object)=>{
@@ -289,13 +289,12 @@ export struct ScanFilePage{
 // }
 
 @Concurrent
-async function  scanDirectoryTask(context: Context, dirPath: string, lockPath: string) {
+async function  scanDirectoryTask(context: Context, dirPath: string, lockPath: string,cover_api:string) {
   const stack: string[] = [dirPath];
   const table: MediaTable = new MediaTable(context);
   while (stack.length  > 0) {
     const currentPath = stack.pop()!;
     const files = FileUtil.listFileSync(currentPath);
-
     await Promise.all(files.map(async  (file) => {
       const fPath = `${currentPath}/${file}`;
       if (fPath === lockPath) return;
@@ -311,7 +310,7 @@ async function  scanDirectoryTask(context: Context, dirPath: string, lockPath: s
           );
           table.insert(mediaItem,  (id: number) => {
 
-          });
+          },cover_api);
         }
       }
     }));

+ 5 - 3
entry/src/main/ets/view/LocalMusic.ets

@@ -449,7 +449,8 @@ export struct LocalMusic {
           code: 1,
           data1: this.context,
           data2: this.rootPath,
-          data3: this.lockPath
+          data3: this.lockPath,
+          data4: PreferencesUtil.getStringSync('COVER_API',''),
         });
         PreferencesUtil.putSync('isFirstApp', false)
       } else {
@@ -966,7 +967,8 @@ export struct LocalMusic {
             code: 1,
             data1: this.context,
             data2: this.rootPath,
-            data3: this.lockPath
+            data3: this.lockPath,
+            data4: PreferencesUtil.getStringSync('COVER_API',''),
           });
           break
       }
@@ -1519,7 +1521,7 @@ export struct LocalMusic {
         if (!newPath.includes(this.lockPath)) { //如果移动到私密,就不插入数据库
           this.table.insert(newItem, (id: number) => {
             //加入数据库
-            // workerInstance.postMessage({ code: 1, data1: this.context,data2: newPath,data3: this.lockPath });
+
           });
         }
 

+ 4 - 4
entry/src/main/ets/workers/Worker.ets

@@ -19,7 +19,7 @@ workerPort.onmessage = async (e: MessageEvents) => {
 
   switch (e.data.code){
     case 1://第一次扫描文件夹入库
-      await scanDirectory(e.data.data1,e.data.data2,e.data.data3).then(()=>{
+      await scanDirectory(e.data.data1,e.data.data2,e.data.data3,e.data.data4).then(()=>{
         Logger.info('onecold scanDirectory 22 全部扫描完成后执行  ')
         // 全部扫描完成后执行
         workerPort.postMessage({  code: 101, data: {} });
@@ -47,7 +47,7 @@ workerPort.onmessage = async (e: MessageEvents) => {
 
 
 
-async function scanDirectory(context: Context, curPath: string, lockPath: string): Promise<VideoItem[]> {
+async function scanDirectory(context: Context, curPath: string, lockPath: string,cover_api:string): Promise<VideoItem[]> {
   const table: MediaTable = new MediaTable(context);
   let mediaItems: VideoItem[] = [];
 
@@ -70,7 +70,7 @@ async function scanDirectory(context: Context, curPath: string, lockPath: string
           if (FileUtil.isDirectory(fPath))  {
             // 用立即执行函数处理异步递归
             pendingTasks.push((async  () => {
-              const subItems = await scanDirectory(context, fPath, lockPath);
+              const subItems = await scanDirectory(context, fPath, lockPath,cover_api);
               mediaItems = mediaItems.concat(subItems);
             })());
           } else {
@@ -84,7 +84,7 @@ async function scanDirectory(context: Context, curPath: string, lockPath: string
                 await new Promise<void>((resolve: (value: void) => void) => {
                   table.insert(mediaItem,  (id: number) => {
                     resolve(); // 明确调用 resolve 且无返回值
-                  });
+                  },cover_api);
                 });
               })());
             }