feat: add some shit

This commit is contained in:
2023-08-02 02:51:20 +02:00
parent 3dc9692eef
commit 32b6b04eb9
3 changed files with 47 additions and 12 deletions

21
lib/cache/image.ts vendored
View File

@ -1,5 +1,6 @@
import { hash } from "@lib/hash.ts";
import * as cache from "@lib/cache/cache.ts";
import { ImageMagick } from "https://deno.land/x/imagemagick_deno@0.0.25/mod.ts";
type ImageCacheOptions = {
url: string;
@ -19,6 +20,20 @@ function getCacheKey({ url: _url, width, height }: ImageCacheOptions) {
);
}
function verifyImage(
imageBuffer: Uint8Array,
) {
return new Promise<boolean>((resolve) => {
try {
ImageMagick.read(imageBuffer, (image) => {
resolve(image.height !== 0 && image.width !== 0);
});
} catch (_err) {
resolve(false);
}
});
}
export async function getImage({ url, width, height }: ImageCacheOptions) {
const cacheKey = getCacheKey({ url, width, height });
@ -44,6 +59,12 @@ export async function setImage(
) {
const clone = new Uint8Array(buffer);
const imageCorrect = await verifyImage(clone);
if (!imageCorrect) {
console.log("[cache/image] failed to store image", { url });
return;
}
const cacheKey = getCacheKey({ url, width, height });
const pointerId = await hash(cacheKey);