mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-16 04:41:47 +00:00
use pseudo decorators
This commit is contained in:
@ -4,6 +4,8 @@ const fetch = require("node-fetch");
|
||||
|
||||
const config = require("../config");
|
||||
|
||||
const { cache } = require("../providers/decorators")
|
||||
|
||||
// Fill songInfo with empty values
|
||||
/**
|
||||
* @typedef {songInfo} SongInfo
|
||||
@ -25,16 +27,21 @@ const songInfo = {
|
||||
};
|
||||
|
||||
// Grab the native image using the src
|
||||
const getImage = async (src) => {
|
||||
const result = await fetch(src);
|
||||
const buffer = await result.buffer();
|
||||
const output = nativeImage.createFromBuffer(buffer);
|
||||
if (output.isEmpty() && !src.endsWith(".jpg") && src.includes(".jpg")) { // fix hidden webp files (https://github.com/th-ch/youtube-music/issues/315)
|
||||
return getImage(src.slice(0, src.lastIndexOf(".jpg") + 4));
|
||||
} else {
|
||||
return output;
|
||||
const getImage = cache (
|
||||
/**
|
||||
* @returns {Promise<Electron.NativeImage>}
|
||||
*/
|
||||
async (src) => {
|
||||
const result = await fetch(src);
|
||||
const buffer = await result.buffer();
|
||||
const output = nativeImage.createFromBuffer(buffer);
|
||||
if (output.isEmpty() && !src.endsWith(".jpg") && src.includes(".jpg")) { // fix hidden webp files (https://github.com/th-ch/youtube-music/issues/315)
|
||||
return getImage(src.slice(0, src.lastIndexOf(".jpg") + 4));
|
||||
} else {
|
||||
return output;
|
||||
}
|
||||
}
|
||||
};
|
||||
);
|
||||
|
||||
const handleData = async (responseText, win) => {
|
||||
const data = JSON.parse(responseText);
|
||||
@ -60,13 +67,10 @@ const handleData = async (responseText, win) => {
|
||||
songInfo.videoId = videoDetails.videoId;
|
||||
songInfo.album = data?.videoDetails?.album; // Will be undefined if video exist
|
||||
|
||||
const oldUrl = songInfo.imageSrc;
|
||||
const thumbnails = videoDetails.thumbnail?.thumbnails;
|
||||
songInfo.imageSrc = thumbnails[thumbnails.length - 1]?.url.split("?")[0];
|
||||
if (oldUrl !== songInfo.imageSrc) {
|
||||
songInfo.image = await getImage(songInfo.imageSrc);
|
||||
}
|
||||
|
||||
songInfo.image = await getImage(songInfo.imageSrc);
|
||||
|
||||
win.webContents.send("update-song-info", JSON.stringify(songInfo));
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user