Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions electron/main/ipc/ipc-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Comment thread
imsyy marked this conversation as resolved.
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" };
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down