Skip to content

Commit 70744d6

Browse files
committed
refactor: image validation code and update dependencies
1 parent a0ab3c9 commit 70744d6

File tree

3 files changed

+17
-29
lines changed

3 files changed

+17
-29
lines changed

src/app/[locale]/(doc-session)/documentation/js/functions/isValidImage/page.tsx

+15-27
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { LocaleParams } from "@/types/Params";
1010

1111
export default async function IsValidImage({
1212
params: { locale },
13-
}: LocaleParams) {
13+
}: Readonly<LocaleParams>) {
1414
setStaticParamsLocale(locale);
1515

1616
const t = await getScopedI18n("DocumentationJsFunctions");
@@ -45,10 +45,13 @@ export default async function IsValidImage({
4545

4646
<SyntaxHighlighter language="javascript" style={a11yDark}>
4747
{`import { isValidImage } from 'multiform-validator';
48-
import ImageBuffer from 'image-buffer';
48+
import * as path from 'path';
49+
import * as fs from 'fs';
4950
50-
const buffer: Buffer = ImageBuffer;
51-
const isValid = isValidImage(buffer);
51+
const filePath = path.join(__dirname, 'image.png');
52+
const fileBuffer = fs.readFileSync(filePath);
53+
54+
const isValid = isValidImage(fileBuffer);
5255
5356
console.log(isValid); // true if the image is valid, false otherwise`}
5457
</SyntaxHighlighter>
@@ -59,37 +62,22 @@ console.log(isValid); // true if the image is valid, false otherwise`}
5962

6063
<SyntaxHighlighter language="javascript" style={a11yDark}>
6164
{`import { isValidImage } from 'multiform-validator';
62-
import ImageBuffer from 'image-buffer';
65+
import * as path from 'path';
66+
import * as fs from 'fs';
67+
68+
const filePath = path.join(__dirname, 'image.png');
69+
const fileBuffer = fs.readFileSync(filePath);
6370
64-
const buffer: Buffer = ImageBuffer;
65-
const isValid = isValidImage(buffer, { exclude: ['gif'] });
71+
const isValid = isValidImage(fileBuffer, { exclude: ['gif'] });
6672
6773
console.log(isValid); // true if the image is valid, false otherwise`}
6874
</SyntaxHighlighter>
6975

7076
<h2 className="mt-6">{t("Example Usage with Nestjs and Multer")}</h2>
71-
<p>
72-
{t(
73-
"In this example it only allocates 4 bytes for performance reasons, but you can pass the entire file.",
74-
)}
75-
</p>
7677

7778
<SyntaxHighlighter language="javascript" style={a11yDark}>
78-
{`const filePath = resolve(process.cwd(), 'public', 'assets', 'images');
79-
80-
const fileGetted = resolve(filePath, filename);
81-
82-
const buffer = Buffer.alloc(4);
83-
84-
const fd = fs.openSync(fileGetted, 'r');
85-
fs.readSync(fd, buffer, 0, 4, 0);
86-
fs.closeSync(fd);
87-
88-
const isValidImageResult = isValidImage(buffer);
89-
90-
if (!isValidImageResult) {
91-
fs.unlinkSync(fileGetted);
92-
throw new BadRequestException('Invalid image');
79+
{`if (!isValidImage(file.buffer)) {
80+
throw new BadRequestException("This is not a valid image");
9381
}`}
9482
</SyntaxHighlighter>
9583
<p className="mt-6">

src/app/[locale]/(doc-session)/documentation/js/page.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515

1616
export default async function DocumentationPageJs({
1717
params: { locale },
18-
}: LocaleParams) {
18+
}: Readonly<LocaleParams>) {
1919
setStaticParamsLocale(locale);
2020

2121
const scopedT = await getScopedI18n("DocumentationJs");

src/app/[locale]/layout.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export async function generateMetadata(): Promise<Metadata> {
2222

2323
return {
2424
metadataBase: new URL(
25-
process.env.NEXT_PUBLIC_WEBSITE_URL ||
25+
process.env.NEXT_PUBLIC_WEBSITE_URL ??
2626
"https://multiformvalidator.netlify.app",
2727
),
2828

0 commit comments

Comments
 (0)