mirror of
https://github.com/imsyy/SPlayer.git
synced 2025-11-24 19:13:00 +08:00
Merge branch 'dev' of https://github.com/imsyy/SPlayer into dev
This commit is contained in:
@@ -6,12 +6,22 @@ import { getFileID, getFileMD5, metaDataLyricsArrayToLrc } from "../utils/helper
|
||||
import { File, Picture, Id3v2Settings } from "node-taglib-sharp";
|
||||
import { ipcLog } from "../logger";
|
||||
import { download } from "electron-dl";
|
||||
import { Options as GlobOptions } from "fast-glob/out/settings";
|
||||
import FastGlob from "fast-glob";
|
||||
|
||||
/**
|
||||
* 文件相关 IPC
|
||||
*/
|
||||
const initFileIpc = (): void => {
|
||||
/**
|
||||
* 获取全局搜索配置
|
||||
* @param cwd 当前工作目录
|
||||
*/
|
||||
const globOpt = (cwd?: string): GlobOptions => ({
|
||||
cwd,
|
||||
caseSensitiveMatch: false,
|
||||
});
|
||||
|
||||
// 默认文件夹
|
||||
ipcMain.handle(
|
||||
"get-default-dir",
|
||||
@@ -27,7 +37,7 @@ const initFileIpc = (): void => {
|
||||
const filePath = resolve(dirPath).replace(/\\/g, "/");
|
||||
console.info(`📂 Fetching music files from: ${filePath}`);
|
||||
// 查找指定目录下的所有音乐文件
|
||||
const musicFiles = await FastGlob("**/*.{mp3,wav,flac,aac,webm}", { cwd: filePath });
|
||||
const musicFiles = await FastGlob("**/*.{mp3,wav,flac,aac,webm}", globOpt(filePath));
|
||||
// 解析元信息
|
||||
const metadataPromises = musicFiles.map(async (file) => {
|
||||
const filePath = join(dirPath, file);
|
||||
@@ -127,23 +137,25 @@ const initFileIpc = (): void => {
|
||||
}> => {
|
||||
try {
|
||||
const filePath = resolve(path).replace(/\\/g, "/");
|
||||
const { common } = await parseFile(filePath);
|
||||
|
||||
// 尝试获取同名的歌词文件
|
||||
const filePathWithoutExt = filePath.replace(/\.[^.]+$/, "");
|
||||
for (const ext of ["ttml", "lrc"] as const) {
|
||||
const lyricPath = `${filePathWithoutExt}.${ext}`;
|
||||
ipcLog.info("lyricPath", lyricPath);
|
||||
try {
|
||||
await access(lyricPath);
|
||||
const lyric = await readFile(lyricPath, "utf-8");
|
||||
if (lyric && lyric != "") return { lyric, format: ext };
|
||||
} catch {
|
||||
/* empty */
|
||||
const matches = await FastGlob(lyricPath, globOpt());
|
||||
ipcLog.info("lyric matches", matches);
|
||||
if (matches.length > 0) {
|
||||
try {
|
||||
const lyric = await readFile(matches[0], "utf-8");
|
||||
if (lyric && lyric !== "") return { lyric, format: ext };
|
||||
} catch {
|
||||
/* empty */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 尝试获取元数据
|
||||
const { common } = await parseFile(filePath);
|
||||
const lyric = common?.lyrics?.[0]?.syncText;
|
||||
if (lyric && lyric.length > 0) {
|
||||
return { lyric: metaDataLyricsArrayToLrc(lyric), format: "lrc" };
|
||||
@@ -207,7 +219,7 @@ const initFileIpc = (): void => {
|
||||
try {
|
||||
// 查找 ttml
|
||||
if (!result.ttml) {
|
||||
const ttmlFiles = await FastGlob(patterns.ttml, { cwd: dir });
|
||||
const ttmlFiles = await FastGlob(patterns.ttml, globOpt(dir));
|
||||
if (ttmlFiles.length > 0) {
|
||||
const filePath = join(dir, ttmlFiles[0]);
|
||||
await access(filePath);
|
||||
@@ -217,7 +229,7 @@ const initFileIpc = (): void => {
|
||||
|
||||
// 查找 lrc
|
||||
if (!result.lrc) {
|
||||
const lrcFiles = await FastGlob(patterns.lrc, { cwd: dir });
|
||||
const lrcFiles = await FastGlob(patterns.lrc, globOpt(dir));
|
||||
if (lrcFiles.length > 0) {
|
||||
const filePath = join(dir, lrcFiles[0]);
|
||||
await access(filePath);
|
||||
|
||||
Reference in New Issue
Block a user