mirror of
https://github.com/ikun0014/lx-music-mobile.git
synced 2025-05-23 22:37:41 +08:00
添加本地歌曲封面缓存清理
This commit is contained in:
parent
adb9c7c582
commit
f1b660ff5a
@ -4,7 +4,9 @@ package cn.toside.music.mobile.utils;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.os.storage.StorageManager;
|
import android.os.storage.StorageManager;
|
||||||
|
|
||||||
|
import com.facebook.react.bridge.Promise;
|
||||||
import com.facebook.react.bridge.ReactApplicationContext;
|
import com.facebook.react.bridge.ReactApplicationContext;
|
||||||
|
import com.facebook.react.bridge.ReactMethod;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@ -16,6 +18,7 @@ import java.io.InputStreamReader;
|
|||||||
import java.lang.reflect.Array;
|
import java.lang.reflect.Array;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.concurrent.Callable;
|
import java.util.concurrent.Callable;
|
||||||
|
|
||||||
public class Utils {
|
public class Utils {
|
||||||
@ -118,4 +121,30 @@ public class Utils {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void deleteRecursive(File fileOrDirectory) {
|
||||||
|
if (fileOrDirectory.isDirectory()) {
|
||||||
|
for (File child : Objects.requireNonNull(fileOrDirectory.listFiles())) {
|
||||||
|
deleteRecursive(child);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fileOrDirectory.delete();
|
||||||
|
}
|
||||||
|
public static void unlink(String filepath) {
|
||||||
|
deleteRecursive(new File(filepath));
|
||||||
|
}
|
||||||
|
static class Unlink implements Callable<Object> {
|
||||||
|
private final String filePath;
|
||||||
|
|
||||||
|
public Unlink(String filePath) {
|
||||||
|
this.filePath = filePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object call() {
|
||||||
|
unlink(filePath);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -315,6 +315,11 @@ public class UtilsModule extends ReactContextBaseJavaModule {
|
|||||||
AsyncTask.runTask(new Utils.WriteStringToFile(filePath, dataStr), promise);
|
AsyncTask.runTask(new Utils.WriteStringToFile(filePath, dataStr), promise);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ReactMethod
|
||||||
|
public void unlink(String filePath, Promise promise) {
|
||||||
|
AsyncTask.runTask(new Utils.Unlink(filePath), promise);
|
||||||
|
}
|
||||||
|
|
||||||
// https://stackoverflow.com/questions/73463341/in-per-app-language-how-to-get-app-locale-in-api-33-if-system-locale-is-diffe
|
// https://stackoverflow.com/questions/73463341/in-per-app-language-how-to-get-app-locale-in-api-33-if-system-locale-is-diffe
|
||||||
@ReactMethod
|
@ReactMethod
|
||||||
public void getSystemLocales(Promise promise) {
|
public void getSystemLocales(Promise promise) {
|
||||||
|
@ -5,7 +5,7 @@ export const externalDirectoryPath = RNFS.ExternalDirectoryPath
|
|||||||
export const temporaryDirectoryPath = RNFS.TemporaryDirectoryPath
|
export const temporaryDirectoryPath = RNFS.TemporaryDirectoryPath
|
||||||
export const externalStorageDirectoryPath = RNFS.ExternalStorageDirectoryPath
|
export const externalStorageDirectoryPath = RNFS.ExternalStorageDirectoryPath
|
||||||
|
|
||||||
export const unlink = async(path: string) => RNFS.unlink(path)
|
// export const unlink = async(path: string) => RNFS.unlink(path)
|
||||||
|
|
||||||
export const readDir = async(path: string) => RNFS.readDir(path)
|
export const readDir = async(path: string) => RNFS.readDir(path)
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import { temporaryDirectoryPath } from '@/utils/fs'
|
import { temporaryDirectoryPath } from '@/utils/fs'
|
||||||
import { scanFiles, readPic as _readPic } from 'react-native-local-media-metadata'
|
import { scanFiles, readPic as _readPic } from 'react-native-local-media-metadata'
|
||||||
|
import { unlink } from './nativeModules/utils'
|
||||||
export {
|
export {
|
||||||
type MusicMetadata,
|
type MusicMetadata,
|
||||||
type MusicMetadataFull,
|
type MusicMetadataFull,
|
||||||
@ -10,12 +11,21 @@ export {
|
|||||||
writeLyric,
|
writeLyric,
|
||||||
} from 'react-native-local-media-metadata'
|
} from 'react-native-local-media-metadata'
|
||||||
|
|
||||||
|
let cleared = false
|
||||||
|
const picCachePath = temporaryDirectoryPath + '/local-media-metadata'
|
||||||
|
|
||||||
export const scanAudioFiles = async(dirPath: string): Promise<string[]> => {
|
export const scanAudioFiles = async(dirPath: string): Promise<string[]> => {
|
||||||
return scanFiles(dirPath, ['mp3', 'flac', 'ogg', 'wav'])
|
return scanFiles(dirPath, ['mp3', 'flac', 'ogg', 'wav'])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const clearPicCache = async() => {
|
||||||
|
await unlink(picCachePath)
|
||||||
|
cleared = true
|
||||||
|
}
|
||||||
|
|
||||||
export const readPic = async(dirPath: string): Promise<string> => {
|
export const readPic = async(dirPath: string): Promise<string> => {
|
||||||
return _readPic(dirPath, temporaryDirectoryPath + '/local-media-metadata')
|
if (!cleared) await clearPicCache()
|
||||||
|
return _readPic(dirPath, picCachePath)
|
||||||
}
|
}
|
||||||
|
|
||||||
// export interface MusicMetadata {
|
// export interface MusicMetadata {
|
||||||
|
@ -54,6 +54,9 @@ export const writeFile = async(filePath: string, data: string): Promise<void> =>
|
|||||||
export const readFile = async(filePath: string): Promise<string> => {
|
export const readFile = async(filePath: string): Promise<string> => {
|
||||||
return UtilsModule.getStringFromFile(filePath)
|
return UtilsModule.getStringFromFile(filePath)
|
||||||
}
|
}
|
||||||
|
export const unlink = async(filePath: string): Promise<void> => {
|
||||||
|
return UtilsModule.unlink(filePath)
|
||||||
|
}
|
||||||
export const getSystemLocales = async(): Promise<string> => {
|
export const getSystemLocales = async(): Promise<string> => {
|
||||||
return UtilsModule.getSystemLocales()
|
return UtilsModule.getSystemLocales()
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user