feat: adding icon parser to download notification

This commit is contained in:
Chubby Granny Chaser
2024-06-05 20:15:59 +01:00
parent 6b8ab895e3
commit 4a4a800b07
17 changed files with 343 additions and 146 deletions

View File

@@ -1,5 +1,7 @@
import Aria2, { StatusResponse } from "aria2";
import path from "node:path";
import { downloadQueueRepository, gameRepository } from "@main/repository";
import { WindowManager } from "./window-manager";
@@ -67,7 +69,11 @@ export class DownloadManager {
private static getFolderName(status: StatusResponse) {
if (status.bittorrent?.info) return status.bittorrent.info.name;
return "";
const [file] = status.files;
if (file) return path.win32.basename(file.path);
return null;
}
private static async getRealDebridDownloadUrl() {
@@ -198,7 +204,7 @@ export class DownloadManager {
}
if (progress === 1 && this.game && !isDownloadingMetadata) {
await publishDownloadCompleteNotification(this.game);
publishDownloadCompleteNotification(this.game);
await downloadQueueRepository.delete({ game: this.game });
@@ -220,7 +226,9 @@ export class DownloadManager {
},
});
this.resumeDownload(nextQueueItem!.game);
if (nextQueueItem) {
this.resumeDownload(nextQueueItem.game);
}
}
}
@@ -237,7 +245,7 @@ export class DownloadManager {
const gid = this.downloads.get(gameId);
if (gid) {
await this.aria2.call("remove", gid);
await this.aria2.call("forceRemove", gid);
if (this.gid === gid) {
this.clearCurrentDownload();

View File

@@ -1,13 +1,40 @@
import { Notification } from "electron";
import { Notification, nativeImage } from "electron";
import { t } from "i18next";
import { parseICO } from "icojs";
import { Game } from "@main/entity";
import { userPreferencesRepository } from "@main/repository";
import { gameRepository, userPreferencesRepository } from "@main/repository";
const getGameIconNativeImage = async (gameId: number) => {
try {
const game = await gameRepository.findOne({
where: {
id: gameId,
},
});
if (!game?.iconUrl) return undefined;
const images = await parseICO(
Buffer.from(game.iconUrl.split("base64,")[1], "base64")
);
const highResIcon = images.find((image) => image.width >= 128);
if (!highResIcon) return undefined;
return nativeImage.createFromBuffer(Buffer.from(highResIcon.buffer));
} catch (err) {
return undefined;
}
};
export const publishDownloadCompleteNotification = async (game: Game) => {
const userPreferences = await userPreferencesRepository.findOne({
where: { id: 1 },
});
const icon = await getGameIconNativeImage(game.id);
if (userPreferences?.downloadNotificationsEnabled) {
new Notification({
title: t("download_complete", {
@@ -19,6 +46,7 @@ export const publishDownloadCompleteNotification = async (game: Game) => {
lng: userPreferences.language,
title: game.title,
}),
icon,
}).show();
}
};
@@ -28,7 +56,7 @@ export const publishNewRepacksNotifications = async (count: number) => {
where: { id: 1 },
});
if (count > 0 && userPreferences?.repackUpdatesNotificationsEnabled) {
if (userPreferences?.repackUpdatesNotificationsEnabled) {
new Notification({
title: t("repack_list_updated", {
ns: "notifications",