refactor: improve lyric file download implement

This commit is contained in:
Revincx 2022-10-22 13:59:49 +08:00
parent 345f3588bd
commit 41b72563ff
No known key found for this signature in database
GPG Key ID: 6E79B88F79CA3126
4 changed files with 45 additions and 11 deletions

View File

@ -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) {

View File

@ -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: {

View File

@ -182,7 +182,7 @@ export default {
minimizeToTray: '最小化到托盘',
},
enableOsdlyricsSupport: {
title: '启用「桌面歌词支持',
title: '桌面歌词支持',
desc: '仅 Linux 下生效。启用后会将歌词文件下载到本地,并在开启播放器时尝试拉起 OSDLyrics。',
},
unm: {

View File

@ -179,7 +179,7 @@ export default {
minimizeToTray: '最小化到工作列角落',
},
enableOsdlyricsSupport: {
title: '啟用「桌面歌詞支援',
title: '桌面歌詞支援',
desc: '只在 Linux 環境下生效。啟用後會將歌詞檔案下載至本機位置,並在開啟播放器時嘗試連帶啟動 OSDLyrics。',
},
unm: {