Skip to content

Commit

Permalink
chore: clean up things
Browse files Browse the repository at this point in the history
  • Loading branch information
KagChi committed Dec 2, 2022
1 parent e5bd9c2 commit ee15cc3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "template",
"version": "1.0.0",
"description": "A TypeScript template project.",
"name": "@nezuchan/image-proxy",
"version": "1.0.1",
"description": "A Image proxy service, such as resizing.",
"main": "dist",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
Expand Down
21 changes: 11 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import "dotenv/config";

import fastify from "fastify";
import crypto from "crypto";
import jimp from "jimp";
import Fastify from "fastify";
import Crypto from "crypto";
import Jimp from "jimp";
import { FastifyReply } from "fastify/types/reply";
import { FastifyRequest } from "fastify/types/request";

const fastifyInstance = fastify({
const fastifyInstance = Fastify({
logger: {
name: "image-proxy",
timestamp: true,
Expand All @@ -22,7 +22,8 @@ const fastifyInstance = fastify({
]
}
},
maxParamLength: Number.MAX_SAFE_INTEGER });
maxParamLength: Number.MAX_SAFE_INTEGER
});

fastifyInstance.get("/:size/:key", {
schema: {
Expand All @@ -39,17 +40,17 @@ fastifyInstance.get("/:size/:key", {
const [width, height] = size.split("x").map((s) => parseInt(s, 10));
if (width > (parseInt(process.env.MAX_WITDH ?? "4096")) || height > (parseInt(process.env.MAX_HEIGHT ?? "4096"))) throw new Error("Image too large");

const decipher = crypto.createDecipheriv("aes-256-cbc", process.env.KEY!, process.env.IV!);
const decipher = Crypto.createDecipheriv("aes-256-cbc", process.env.KEY!, process.env.IV!);
const decrypted = decipher.update(key, "hex");
const decryptedString = Buffer.concat([decrypted, decipher.final()]).toString();

const image = await jimp.read(decryptedString);
image.resize(width, height, jimp.RESIZE_HERMITE);
const image = await Jimp.read(decryptedString);
image.resize(width, height, Jimp.RESIZE_HERMITE);
image.quality(100);

const buffer = await image.getBufferAsync(jimp.MIME_PNG);
const buffer = await image.getBufferAsync(Jimp.MIME_PNG);

reply.header("Content-Type", jimp.MIME_PNG);
reply.header("Content-Type", Jimp.MIME_PNG);
return buffer;
}
);
Expand Down

0 comments on commit ee15cc3

Please sign in to comment.