mirror of
https://github.com/hydralauncher/hydra.git
synced 2026-01-23 02:41:02 +00:00
feat: adding background to item
This commit is contained in:
@@ -50,5 +50,7 @@ export const databasePath = path.join(
|
||||
"hydra.db"
|
||||
);
|
||||
|
||||
export const imageCachePath = path.join(app.getPath("userData"), ".imagecache");
|
||||
|
||||
export const INSTALLATION_ID_LENGTH = 6;
|
||||
export const ACTIVATION_KEY_MULTIPLIER = 7;
|
||||
|
||||
@@ -2,7 +2,6 @@ import { DataSource } from "typeorm";
|
||||
import {
|
||||
Game,
|
||||
GameShopCache,
|
||||
ImageCache,
|
||||
Repack,
|
||||
RepackerFriendlyName,
|
||||
UserPreferences,
|
||||
@@ -19,7 +18,6 @@ export const createDataSource = (options: Partial<SqliteConnectionOptions>) =>
|
||||
database: databasePath,
|
||||
entities: [
|
||||
Game,
|
||||
ImageCache,
|
||||
Repack,
|
||||
RepackerFriendlyName,
|
||||
UserPreferences,
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
import {
|
||||
Entity,
|
||||
PrimaryGeneratedColumn,
|
||||
Column,
|
||||
CreateDateColumn,
|
||||
UpdateDateColumn,
|
||||
} from "typeorm";
|
||||
|
||||
@Entity("image_cache")
|
||||
export class ImageCache {
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
|
||||
@Column("text", { unique: true })
|
||||
url: string;
|
||||
|
||||
@Column("text")
|
||||
data: string;
|
||||
|
||||
@CreateDateColumn()
|
||||
createdAt: Date;
|
||||
|
||||
@UpdateDateColumn()
|
||||
updatedAt: Date;
|
||||
}
|
||||
@@ -1,32 +1,35 @@
|
||||
import { imageCacheRepository } from "@main/repository";
|
||||
import crypto from "node:crypto";
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
|
||||
import { registerEvent } from "../register-event";
|
||||
import { getImageBase64 } from "@main/helpers";
|
||||
import { getFileBuffer } from "@main/helpers";
|
||||
import { logger } from "@main/services";
|
||||
import { imageCachePath } from "@main/constants";
|
||||
|
||||
const getOrCacheImage = async (
|
||||
_event: Electron.IpcMainInvokeEvent,
|
||||
url: string
|
||||
) => {
|
||||
const cache = await imageCacheRepository.findOne({
|
||||
where: {
|
||||
url,
|
||||
},
|
||||
});
|
||||
if (!fs.existsSync(imageCachePath)) fs.mkdirSync(imageCachePath);
|
||||
|
||||
if (cache) return cache.data;
|
||||
const extname = path.extname(url);
|
||||
|
||||
getImageBase64(url).then((data) =>
|
||||
imageCacheRepository
|
||||
.save({
|
||||
url,
|
||||
data,
|
||||
})
|
||||
.catch(() => {
|
||||
logger.error(`Failed to cache image "${url}"`, {
|
||||
const checksum = crypto.createHash("sha256").update(url).digest("hex");
|
||||
const cachePath = path.join(imageCachePath, `${checksum}${extname}`);
|
||||
|
||||
const cache = fs.existsSync(cachePath);
|
||||
|
||||
if (cache) return `hydra://${cachePath}`;
|
||||
|
||||
getFileBuffer(url).then((buffer) =>
|
||||
fs.writeFile(cachePath, buffer, (err) => {
|
||||
if (err) {
|
||||
logger.error(`Failed to cache image`, err, {
|
||||
method: "getOrCacheImage",
|
||||
});
|
||||
})
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
return url;
|
||||
|
||||
@@ -74,12 +74,15 @@ export const getSteamAppAsset = (
|
||||
return `https://cdn.cloudflare.steamstatic.com/steamcommunity/public/images/apps/${objectID}/${clientIcon}.ico`;
|
||||
};
|
||||
|
||||
export const getImageBase64 = async (url: string) =>
|
||||
export const getFileBuffer = async (url: string) =>
|
||||
fetch(url, { method: "GET" }).then((response) =>
|
||||
response.arrayBuffer().then((buffer) => {
|
||||
return `data:image/jpeg;base64,${Buffer.from(buffer).toString("base64")}`;
|
||||
})
|
||||
response.arrayBuffer().then((buffer) => Buffer.from(buffer))
|
||||
);
|
||||
|
||||
export const getImageBase64 = async (url: string) =>
|
||||
getFileBuffer(url).then((buffer) => {
|
||||
return `data:image/jpeg;base64,${Buffer.from(buffer).toString("base64")}`;
|
||||
});
|
||||
|
||||
export * from "./formatters";
|
||||
export * from "./ps";
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { app, BrowserWindow } from "electron";
|
||||
import { app, BrowserWindow, net, protocol } from "electron";
|
||||
import { init } from "@sentry/electron/main";
|
||||
import i18n from "i18next";
|
||||
import path from "node:path";
|
||||
@@ -52,6 +52,10 @@ if (process.defaultApp) {
|
||||
app.whenReady().then(() => {
|
||||
electronApp.setAppUserModelId("site.hydralauncher.hydra");
|
||||
|
||||
protocol.handle("hydra", (request) =>
|
||||
net.fetch("file://" + request.url.slice("hydra://".length))
|
||||
);
|
||||
|
||||
dataSource.initialize().then(async () => {
|
||||
await resolveDatabaseUpdates();
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@ import { dataSource } from "./data-source";
|
||||
import {
|
||||
Game,
|
||||
GameShopCache,
|
||||
ImageCache,
|
||||
Repack,
|
||||
RepackerFriendlyName,
|
||||
UserPreferences,
|
||||
@@ -12,8 +11,6 @@ import {
|
||||
|
||||
export const gameRepository = dataSource.getRepository(Game);
|
||||
|
||||
export const imageCacheRepository = dataSource.getRepository(ImageCache);
|
||||
|
||||
export const repackRepository = dataSource.getRepository(Repack);
|
||||
|
||||
export const repackerFriendlyNameRepository =
|
||||
|
||||
@@ -1,16 +1,12 @@
|
||||
import { parentPort } from "worker_threads";
|
||||
import parseTorrent from "parse-torrent";
|
||||
import { getFileBuffer } from "@main/helpers";
|
||||
|
||||
const port = parentPort;
|
||||
if (!port) throw new Error("IllegalState");
|
||||
|
||||
const getTorrentBuffer = (url: string) =>
|
||||
fetch(url, { method: "GET" }).then((response) =>
|
||||
response.arrayBuffer().then((buffer) => Buffer.from(buffer))
|
||||
);
|
||||
|
||||
port.on("message", async (url: string) => {
|
||||
const buffer = await getTorrentBuffer(url);
|
||||
const buffer = await getFileBuffer(url);
|
||||
const torrent = await parseTorrent(buffer);
|
||||
|
||||
port.postMessage(torrent);
|
||||
|
||||
Reference in New Issue
Block a user