feat: add some shit
This commit is contained in:
21
lib/cache/image.ts
vendored
21
lib/cache/image.ts
vendored
@ -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);
|
||||
|
||||
|
Reference in New Issue
Block a user