mirror of
https://github.com/hydralauncher/hydra.git
synced 2026-01-27 12:51:03 +00:00
feat: adding background to item
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user