Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F10360172
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
5 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/@types/index.d.ts b/@types/index.d.ts
index 17e127d..1a4361e 100644
--- a/@types/index.d.ts
+++ b/@types/index.d.ts
@@ -1,25 +1,25 @@
/// <reference types="koa__router" />
import Router from "@koa/router";
import { Middleware } from "koa";
export default class KoaResponsiveImageRouter extends Router {
static_path: string;
tmp_dir: string;
router: Router;
hashToResolutions: Record<string, number[]>;
hashToLossless: Record<string, boolean>;
constructor(static_path: string, tmp_dir: string);
image({ resolutions, sizes_attr, path, lossless, }: {
resolutions: number[];
sizes_attr: string;
path: string;
- lossless: boolean;
+ lossless?: boolean;
}): Promise<string>;
getRoutes(): Middleware;
private getHash;
private getHashedPath;
private generateDirectory;
private copySourceFile;
private getImage;
private saveImage;
private generateImage;
}
diff --git a/src/index.ts b/src/index.ts
index d67668c..c36a492 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -1,198 +1,198 @@
import Router from "@koa/router";
import sharp from "sharp";
import crypto from "crypto";
import { access, mkdir, copyFile, readFile, stat } from "fs/promises";
import { extname, basename } from "path";
import { Middleware } from "koa";
type correctExtension = "jpeg" | "png" | "avif" | "webp";
function isCorrectExtension(type: unknown): type is correctExtension {
const extensions = ["avif", "webp", "jpeg", "png"];
return extensions.includes(type as string);
}
export default class KoaResponsiveImageRouter extends Router {
router: Router;
hashToResolutions: Record<string, number[]> = {};
hashToLossless: Record<string, boolean> = {};
constructor(public static_path: string, public tmp_dir: string) {
super();
this.router = new Router();
this.router.get("/:hash/:filename", async (ctx) => {
const { hash, filename } = ctx.params;
const resolution = parseInt(filename.split(".")[0]);
const destination = `${this.getHashedPath(hash)}`;
const type = extname(filename).split(".").pop();
if (
this.hashToResolutions[hash].find(
(el: number) => el === resolution
) &&
type !== undefined &&
isCorrectExtension(type)
) {
try {
await access(destination);
ctx.body = await this.getImage({ hash, resolution, type });
ctx.type = `image/${type}`;
} catch (error) {
ctx.response.status = 404;
}
} else {
ctx.response.status = 404;
}
});
}
async image({
resolutions,
sizes_attr,
path,
lossless = false,
}: {
resolutions: number[];
sizes_attr: string;
path: string;
lossless?: boolean;
}): Promise<string> {
const hash = await this.getHash(path, resolutions);
this.hashToResolutions[hash] = resolutions;
this.hashToLossless[hash] = lossless;
await this.generateDirectory(path, hash);
await this.copySourceFile(path, hash);
const destination = `${this.static_path}/${hash}`;
const extensions = [
"avif",
"webp",
"png",
...(lossless ? [] : ["jpg"]),
];
let html = "<picture>";
for (let j = 0; j < extensions.length; j++) {
html += '\n<source\nsrcset="\n';
for (let i = 0; i < resolutions.length; i++) {
html += `${destination}/${resolutions[i]}.${extensions[j]} ${resolutions[i]}w`;
if (i !== resolutions.length - 1) {
html += ",";
} else {
html += `\n"`;
}
html += `\n`;
}
html += `src="${destination}/${
resolutions[Math.round(resolutions.length / 2)]
}.${extensions[j]}"\n`;
html += `sizes="${sizes_attr}"\ntype="image/${extensions[j]}"\n/>\n`;
}
- html += `<img src="${destination}/${
+ html += `<img loading="lazy" src="${destination}/${
resolutions[Math.round(resolutions.length / 2)]
}.jpeg" /></picture>`;
return html;
}
getRoutes(): Middleware {
return this.router.routes();
}
private async getHash(original_file_path: string, resolutions: number[]) {
return crypto
.createHash("md5")
.update(
`
${basename(original_file_path)}${(
await stat(original_file_path)
).atime.getTime()}${JSON.stringify(resolutions)}`
)
.digest("hex");
}
private getHashedPath(hash: string) {
return `${this.tmp_dir}/${hash}`;
}
private async generateDirectory(original_file_path: string, hash: string) {
const destination = `${this.tmp_dir}/${hash}`;
try {
await access(destination);
} catch {
try {
await mkdir(destination, { recursive: true });
} catch (error) {
console.log("directory exist");
}
}
}
private async copySourceFile(original_file_path: string, hash: string) {
const source = original_file_path;
const destination = `${this.getHashedPath(hash)}/original-file`;
await copyFile(source, destination);
}
private async getImage({
hash,
resolution,
type,
}: {
hash: string;
resolution: number;
type: correctExtension;
}): Promise<Buffer> {
try {
return Buffer.from(
((await readFile(
`${this.getHashedPath(hash)}/${resolution}.${type}`
)) as unknown) as string,
"base64" as BufferEncoding
);
} catch {
const buffer = await this.generateImage({ hash, resolution, type });
this.saveImage({ hash, resolution, type, buffer });
return buffer;
}
}
private saveImage({
hash,
resolution,
type,
buffer,
}: {
hash: string;
resolution: number;
type: string;
buffer: Buffer;
}) {
void sharp(buffer).toFile(
`${this.getHashedPath(hash)}/${resolution}.${type}`
);
}
private async generateImage({
hash,
resolution,
type,
}: {
hash: string;
resolution: number;
type: correctExtension;
}) {
const lossless = this.hashToLossless[hash];
return await sharp(`${this.getHashedPath(hash)}/original-file`)
.resize(resolution)
.toFormat(type, lossless ? { quality: 100, lossless: true } : {})
.toBuffer();
}
}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Sat, Nov 8, 04:55 (1 d, 4 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1034041
Default Alt Text
(5 KB)
Attached To
Mode
rRIMAGEROUTER koa-responsive-image-router
Attached
Detach File
Event Timeline
Log In to Comment