mirror of
https://github.com/GiriNeko/YesPlayMusic.git
synced 2025-12-16 05:08:04 +00:00
refactor: improve lyric file download implement
This commit is contained in:
parent
345f3588bd
commit
41b72563ff
4 changed files with 45 additions and 11 deletions
|
|
@ -6,7 +6,7 @@ import shortcuts from '@/utils/shortcuts';
|
||||||
import { createMenu } from './menu';
|
import { createMenu } from './menu';
|
||||||
import { isCreateTray, isMac } from '@/utils/platform';
|
import { isCreateTray, isMac } from '@/utils/platform';
|
||||||
import { resolve } from 'path';
|
import { resolve } from 'path';
|
||||||
import { existsSync, mkdirSync, writeFileSync } from 'fs';
|
import { mkdir, rm, writeFile } from 'fs/promises';
|
||||||
|
|
||||||
const clc = require('cli-color');
|
const clc = require('cli-color');
|
||||||
const log = text => {
|
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) {
|
export function initIpcMain(win, store, trayEventEmitter) {
|
||||||
// WIP: Do not enable logging as it has some issues in non-blocking I/O environment.
|
// WIP: Do not enable logging as it has some issues in non-blocking I/O environment.
|
||||||
// UNM.enableLogging(UNM.LoggingType.ConsoleEnv);
|
// UNM.enableLogging(UNM.LoggingType.ConsoleEnv);
|
||||||
|
|
@ -311,13 +348,10 @@ export function initIpcMain(win, store, trayEventEmitter) {
|
||||||
registerGlobalShortcut(win, store);
|
registerGlobalShortcut(win, store);
|
||||||
});
|
});
|
||||||
|
|
||||||
ipcMain.on('saveLyric', (event, { name, lyric }) => {
|
ipcMain.on('saveLyric', async (_, { name, lyric }) => {
|
||||||
let lyricsDirPath = resolve(process.env.HOME, '.lyrics');
|
await writeLyric(name, lyric);
|
||||||
if (!existsSync(lyricsDirPath)) mkdirSync(lyricsDirPath);
|
|
||||||
if (!existsSync(resolve(lyricsDirPath, name + '.lrc'))) {
|
return win.webContents.send('saveLyricFinished');
|
||||||
writeFileSync(resolve(lyricsDirPath, name + '.lrc'), lyric);
|
|
||||||
}
|
|
||||||
win.webContents.send('saveLyricFinished');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (isCreateTray) {
|
if (isCreateTray) {
|
||||||
|
|
|
||||||
|
|
@ -181,7 +181,7 @@ export default {
|
||||||
minimizeToTray: 'Minimize to tray',
|
minimizeToTray: 'Minimize to tray',
|
||||||
},
|
},
|
||||||
enableOsdlyricsSupport: {
|
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.',
|
desc: 'Only takes effect under Linux. After enabled, it downloads the lyrics file to the local, and tries to launch OSDLyrics at startup.',
|
||||||
},
|
},
|
||||||
unm: {
|
unm: {
|
||||||
|
|
|
||||||
|
|
@ -182,7 +182,7 @@ export default {
|
||||||
minimizeToTray: '最小化到托盘',
|
minimizeToTray: '最小化到托盘',
|
||||||
},
|
},
|
||||||
enableOsdlyricsSupport: {
|
enableOsdlyricsSupport: {
|
||||||
title: '启用「桌面歌词」支持',
|
title: '桌面歌词支持',
|
||||||
desc: '仅 Linux 下生效。启用后会将歌词文件下载到本地,并在开启播放器时尝试拉起 OSDLyrics。',
|
desc: '仅 Linux 下生效。启用后会将歌词文件下载到本地,并在开启播放器时尝试拉起 OSDLyrics。',
|
||||||
},
|
},
|
||||||
unm: {
|
unm: {
|
||||||
|
|
|
||||||
|
|
@ -179,7 +179,7 @@ export default {
|
||||||
minimizeToTray: '最小化到工作列角落',
|
minimizeToTray: '最小化到工作列角落',
|
||||||
},
|
},
|
||||||
enableOsdlyricsSupport: {
|
enableOsdlyricsSupport: {
|
||||||
title: '啟用「桌面歌詞」支援',
|
title: '桌面歌詞支援',
|
||||||
desc: '只在 Linux 環境下生效。啟用後會將歌詞檔案下載至本機位置,並在開啟播放器時嘗試連帶啟動 OSDLyrics。',
|
desc: '只在 Linux 環境下生效。啟用後會將歌詞檔案下載至本機位置,並在開啟播放器時嘗試連帶啟動 OSDLyrics。',
|
||||||
},
|
},
|
||||||
unm: {
|
unm: {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue