feat: adding background to item

This commit is contained in:
Hydra
2024-05-03 19:51:13 +01:00
parent f6072eeb5c
commit 1f2f269736
20 changed files with 77 additions and 98 deletions

View File

@@ -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;