mirror of
https://github.com/qier222/YesPlayMusic.git
synced 2024-11-22 12:47:27 +08:00
refactor: improve lyric file download implement
This commit is contained in:
parent
345f3588bd
commit
41b72563ff
|
@ -6,7 +6,7 @@ import shortcuts from '@/utils/shortcuts';
|
|||
import { createMenu } from './menu';
|
||||
import { isCreateTray, isMac } from '@/utils/platform';
|
||||
import { resolve } from 'path';
|
||||
import { existsSync, mkdirSync, writeFileSync } from 'fs';
|
||||
import { mkdir, rm, writeFile } from 'fs/promises';
|
||||
|
||||
const clc = require('cli-color');
|
||||
const log = text => {
|
||||
|
@ -135,6 +135,43 @@ function parseSourceStringToList(executor, sourceString) {
|
|||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Write lyrics into local file
|
||||
*
|
||||
* @param {string} name
|
||||
* @param {string} content
|
||||
*/
|
||||
async function writeLyric(name, content) {
|
||||
name = name.replaceAll('/', '⁄');
|
||||
|
||||
const lyricsDir = resolve(process.env.HOME, '.lyrics');
|
||||
const destination = resolve(lyricsDir, name + '.lrc');
|
||||
|
||||
try {
|
||||
await writeFile(destination, content);
|
||||
} catch (e) {
|
||||
switch (e.code) {
|
||||
// ENOENT (no such file or directory),
|
||||
case 'ENOENT':
|
||||
await mkdir(lyricsDir, { recursive: true });
|
||||
|
||||
// Try again. Should be succeed.
|
||||
return await writeLyric(name, content);
|
||||
// ENOTDIR (not a directory), 指 TMPDIR 有問題。
|
||||
case 'ENOTDIR':
|
||||
// 砍掉 TMPDIR「檔案」。
|
||||
await rm(lyricsDir);
|
||||
|
||||
// 預期接下來的流程是 ENOENT 建立資料夾的流程。
|
||||
return await writeLyric(name, content);
|
||||
default:
|
||||
log(e);
|
||||
break;
|
||||
}
|
||||
log(e);
|
||||
}
|
||||
}
|
||||
|
||||
export function initIpcMain(win, store, trayEventEmitter) {
|
||||
// WIP: Do not enable logging as it has some issues in non-blocking I/O environment.
|
||||
// UNM.enableLogging(UNM.LoggingType.ConsoleEnv);
|
||||
|
@ -311,13 +348,10 @@ export function initIpcMain(win, store, trayEventEmitter) {
|
|||
registerGlobalShortcut(win, store);
|
||||
});
|
||||
|
||||
ipcMain.on('saveLyric', (event, { name, lyric }) => {
|
||||
let lyricsDirPath = resolve(process.env.HOME, '.lyrics');
|
||||
if (!existsSync(lyricsDirPath)) mkdirSync(lyricsDirPath);
|
||||
if (!existsSync(resolve(lyricsDirPath, name + '.lrc'))) {
|
||||
writeFileSync(resolve(lyricsDirPath, name + '.lrc'), lyric);
|
||||
}
|
||||
win.webContents.send('saveLyricFinished');
|
||||
ipcMain.on('saveLyric', async (_, { name, lyric }) => {
|
||||
await writeLyric(name, lyric);
|
||||
|
||||
return win.webContents.send('saveLyricFinished');
|
||||
});
|
||||
|
||||
if (isCreateTray) {
|
||||
|
|
|
@ -181,7 +181,7 @@ export default {
|
|||
minimizeToTray: 'Minimize to tray',
|
||||
},
|
||||
enableOsdlyricsSupport: {
|
||||
title: '"Desktop Lyrics" support',
|
||||
title: 'desktop lyrics support',
|
||||
desc: 'Only takes effect under Linux. After enabled, it downloads the lyrics file to the local, and tries to launch OSDLyrics at startup.',
|
||||
},
|
||||
unm: {
|
||||
|
|
|
@ -182,7 +182,7 @@ export default {
|
|||
minimizeToTray: '最小化到托盘',
|
||||
},
|
||||
enableOsdlyricsSupport: {
|
||||
title: '启用「桌面歌词」支持',
|
||||
title: '桌面歌词支持',
|
||||
desc: '仅 Linux 下生效。启用后会将歌词文件下载到本地,并在开启播放器时尝试拉起 OSDLyrics。',
|
||||
},
|
||||
unm: {
|
||||
|
|
|
@ -179,7 +179,7 @@ export default {
|
|||
minimizeToTray: '最小化到工作列角落',
|
||||
},
|
||||
enableOsdlyricsSupport: {
|
||||
title: '啟用「桌面歌詞」支援',
|
||||
title: '桌面歌詞支援',
|
||||
desc: '只在 Linux 環境下生效。啟用後會將歌詞檔案下載至本機位置,並在開啟播放器時嘗試連帶啟動 OSDLyrics。',
|
||||
},
|
||||
unm: {
|
||||
|
|
Loading…
Reference in New Issue
Block a user