@@ -10,7 +10,7 @@ import { LocaleParams } from "@/types/Params";
10
10
11
11
export default async function IsValidImage ( {
12
12
params : { locale } ,
13
- } : LocaleParams ) {
13
+ } : Readonly < LocaleParams > ) {
14
14
setStaticParamsLocale ( locale ) ;
15
15
16
16
const t = await getScopedI18n ( "DocumentationJsFunctions" ) ;
@@ -45,10 +45,13 @@ export default async function IsValidImage({
45
45
46
46
< SyntaxHighlighter language = "javascript" style = { a11yDark } >
47
47
{ `import { isValidImage } from 'multiform-validator';
48
- import ImageBuffer from 'image-buffer';
48
+ import * as path from 'path';
49
+ import * as fs from 'fs';
49
50
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);
52
55
53
56
console.log(isValid); // true if the image is valid, false otherwise` }
54
57
</ SyntaxHighlighter >
@@ -59,37 +62,22 @@ console.log(isValid); // true if the image is valid, false otherwise`}
59
62
60
63
< SyntaxHighlighter language = "javascript" style = { a11yDark } >
61
64
{ `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);
63
70
64
- const buffer: Buffer = ImageBuffer;
65
- const isValid = isValidImage(buffer, { exclude: ['gif'] });
71
+ const isValid = isValidImage(fileBuffer, { exclude: ['gif'] });
66
72
67
73
console.log(isValid); // true if the image is valid, false otherwise` }
68
74
</ SyntaxHighlighter >
69
75
70
76
< 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 >
76
77
77
78
< 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");
93
81
}` }
94
82
</ SyntaxHighlighter >
95
83
< p className = "mt-6" >
0 commit comments