Skip to content

Commit

Permalink
🐞 fix: 修复本地目录无法读取 #315
Browse files Browse the repository at this point in the history
  • Loading branch information
imsyy committed Dec 23, 2024
1 parent 02befcd commit 96a0495
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 12 deletions.
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ jobs:
shell: bash
env:
GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }}
SNAPCRAFT_STORE_CREDENTIALS: ${{ secrets.SNAPCRAFT_TOKEN }}
# 上传 Snap 包到 Snapcraft 商店
- name: Publish Snap to Snap Store
run: snapcraft upload dist/*.snap --release stable
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# SPlayer

> 一个简约的音乐播放器
> A simple music player
![Stars](https://img.shields.io/github/stars/imsyy/SPlayer?style=flat)
![Version](https://img.shields.io/github/v/release/imsyy/SPlayer)
[![Build Release](https://github.com/imsyy/SPlayer/actions/workflows/release.yml/badge.svg)](https://github.com/imsyy/SPlayer/actions/workflows/release.yml)
![License](https://img.shields.io/github/license/imsyy/SPlayer)
![Issues](https://img.shields.io/github/issues/imsyy/SPlayer)

![main](/screenshots/SPlayer.jpg)

Expand Down
16 changes: 14 additions & 2 deletions electron/main/ipcMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { Thumbar } from "./thumbar";
import { StoreType } from "./store";
import { isDev, getFileID, getFileMD5 } from "./utils";
import { isShortcutRegistered, registerShortcut, unregisterShortcuts } from "./shortcut";
import { join, basename, resolve } from "path";
import { join, basename, resolve, relative, isAbsolute } from "path";
import { download } from "electron-dl";
import { checkUpdate, startDownloadUpdate } from "./update";
import fs from "fs/promises";
Expand Down Expand Up @@ -173,8 +173,10 @@ const initWinIpcMain = (
// 遍历音乐文件
ipcMain.handle("get-music-files", async (_, dirPath: string) => {
try {
// 规范化路径
const filePath = resolve(dirPath);
// 查找指定目录下的所有音乐文件
const musicFiles = await fg("**/*.{mp3,wav,flac}", { cwd: dirPath });
const musicFiles = await fg("**/*.{mp3,wav,flac}", { cwd: filePath });
// 解析元信息
const metadataPromises = musicFiles.map(async (file) => {
const filePath = join(dirPath, file);
Expand Down Expand Up @@ -623,6 +625,16 @@ const initLyricIpcMain = (
lyricWin.setIgnoreMouseEvents(false);
}
});

// 检查是否是子文件夹
ipcMain.handle("check-if-subfolder", (_, localFilesPath: string[], selectedDir: string) => {
const resolvedSelectedDir = resolve(selectedDir);
const allPaths = localFilesPath.map((p) => resolve(p));
return allPaths.some((existingPath) => {
const relativePath = relative(existingPath, resolvedSelectedDir);
return relativePath && !relativePath.startsWith("..") && !isAbsolute(relativePath);
});
});
};

// tray
Expand Down
2 changes: 1 addition & 1 deletion electron/server/netease/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const initNcmAPI = async (fastify: FastifyInstance) => {
fastify.get("/netease", (_, reply) => {
reply.send({
name: "NeteaseCloudMusicApi",
version: "4.20.0",
version: "4.25.0",
description: "网易云音乐 Node.js API service",
author: "@binaryify",
license: "MIT",
Expand Down
6 changes: 6 additions & 0 deletions src/components/Setting/LocalSetting.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
</div>
<n-switch class="set" v-model:value="settingStore.showLocalCover" :round="false" />
</n-card>
<n-card class="set-item">
<div class="label">
<n-text class="name">显示本地默认歌曲目录</n-text>
</div>
<n-switch class="set" v-model:value="settingStore.showDefaultLocalPath" :round="false" />
</n-card>
<n-card class="set-item" id="local-list-choose" content-style="flex-direction: column">
<n-flex justify="space-between">
<div class="label">
Expand Down
2 changes: 2 additions & 0 deletions src/stores/setting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ interface SettingState {
dynamicCover: boolean;
useKeepAlive: boolean;
excludeKeywords: string[];
showDefaultLocalPath: boolean;
}

export const useSettingStore = defineStore("setting", {
Expand Down Expand Up @@ -148,6 +149,7 @@ export const useSettingStore = defineStore("setting", {
excludeKeywords: keywords, // 排除歌词关键字
// 本地
localFilesPath: [],
showDefaultLocalPath: true, // 显示默认本地路径
localSeparators: ["/", "&"],
showLocalCover: true,
// 下载
Expand Down
8 changes: 5 additions & 3 deletions src/utils/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,11 @@ export const changeLocalPath = async (delIndex?: number) => {
// 检查是否为子文件夹
const defaultMusicPath = await window.electron.ipcRenderer.invoke("get-default-dir", "music");
const allPath = [defaultMusicPath, ...settingStore.localFilesPath];
const isSubfolder = allPath.some((existingPath) => {
return selectedDir.startsWith(existingPath);
});
const isSubfolder = await window.electron.ipcRenderer.invoke(
"check-if-subfolder",
allPath,
selectedDir,
);
if (!isSubfolder) {
settingStore.localFilesPath.push(selectedDir);
} else {
Expand Down
16 changes: 13 additions & 3 deletions src/views/Local/layout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,14 @@
<template #prefix>
<SvgIcon :size="20" name="FolderMusic" />
</template>
<n-thing :title="defaultMusicPath" description="系统默认音乐文件夹,无法更改" />
<template #suffix>
<n-switch
v-model:value="settingStore.showDefaultLocalPath"
:round="false"
class="set"
/>
</template>
<n-thing :title="defaultMusicPath" description="系统默认音乐文件夹" />
</n-list-item>
<n-list-item v-for="(item, index) in settingStore.localFilesPath" :key="index">
<template #prefix>
Expand Down Expand Up @@ -176,7 +183,10 @@ const listData = computed<SongType[]>(() => {
// 获取音乐文件夹
const getMusicFolder = async (): Promise<string[]> => {
defaultMusicPath.value = await window.electron.ipcRenderer.invoke("get-default-dir", "music");
return [defaultMusicPath.value, ...settingStore.localFilesPath];
return [
settingStore.showDefaultLocalPath ? defaultMusicPath.value : "",
...settingStore.localFilesPath,
];
};
// 全部音乐大小
Expand Down Expand Up @@ -262,7 +272,7 @@ localEventBus.on(() => getAllLocalMusic());
// 本地目录变化
watch(
() => settingStore.localFilesPath,
() => [settingStore.localFilesPath, settingStore.showDefaultLocalPath],
async () => await getAllLocalMusic(),
{ deep: true },
);
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.node.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"electron/main/index.d.ts",
"electron/preload/index.d.ts",
"electron/preload/index.ts"
],
, "dist/lastfm.ts" ],
"compilerOptions": {
"composite": true,
"types": ["electron-vite/node"]
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.web.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"src/**/*.vue",
"electron/main/index.d.ts",
"electron/preload/index.d.ts"
],
, "dist/lastfm.ts" ],
"compilerOptions": {
"composite": true,
"maxNodeModuleJsDepth": 2,
Expand Down

0 comments on commit 96a0495

Please sign in to comment.