ayan-pkg 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/blur.js +41 -0
- package/index.js +7 -0
- package/package.json +24 -0
- package/uploader.js +66 -0
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# ayan
|
package/blur.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
const sharp = require("sharp");
|
|
2
|
+
const axios = require("axios");
|
|
3
|
+
const fs = require("fs");
|
|
4
|
+
const path = require("path");
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
// Root folder of your project
|
|
8
|
+
const ROOT_DIR = process.cwd();
|
|
9
|
+
|
|
10
|
+
// Uploads folder and blur folder
|
|
11
|
+
const UPLOAD_DIR = path.join(ROOT_DIR, "public/uploads");
|
|
12
|
+
const BLUR_DIR = path.join(UPLOAD_DIR, "blur");
|
|
13
|
+
if (!fs.existsSync(BLUR_DIR)) fs.mkdirSync(BLUR_DIR, { recursive: true });
|
|
14
|
+
|
|
15
|
+
async function generateBlurImage(imageUrl) {
|
|
16
|
+
try {
|
|
17
|
+
// Fetch image data
|
|
18
|
+
const response = await axios.get(imageUrl, { responseType: "arraybuffer" });
|
|
19
|
+
const buffer = Buffer.from(response.data);
|
|
20
|
+
|
|
21
|
+
// Generate filename
|
|
22
|
+
const filename = `blur-${Date.now()}.webp`;
|
|
23
|
+
const outputPath = path.join(BLUR_DIR, filename);
|
|
24
|
+
|
|
25
|
+
// resize + blur
|
|
26
|
+
await sharp(buffer)
|
|
27
|
+
.resize(20) // placeholder blur
|
|
28
|
+
.blur(10)
|
|
29
|
+
.webp({ quality: 70 })
|
|
30
|
+
.toFile(outputPath);
|
|
31
|
+
|
|
32
|
+
// Return URL
|
|
33
|
+
const baseUrl = process.env.APP_URL.replace(/\/$/, "");
|
|
34
|
+
return `${baseUrl}/uploads/blur/${filename}`;
|
|
35
|
+
} catch (err) {
|
|
36
|
+
console.error("Blur generation failed:", err);
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
module.exports = generateBlurImage
|
package/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ayan-pkg",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/AyanX/ayan.git"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [],
|
|
14
|
+
"author": "",
|
|
15
|
+
"license": "ISC",
|
|
16
|
+
"type": "commonjs",
|
|
17
|
+
"bugs": {
|
|
18
|
+
"url": "https://github.com/AyanX/ayan/issues"
|
|
19
|
+
},
|
|
20
|
+
"homepage": "https://github.com/AyanX/ayan#readme",
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"multer": "^2.1.1"
|
|
23
|
+
}
|
|
24
|
+
}
|
package/uploader.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
const multer = require("multer");
|
|
2
|
+
const path = require("path");
|
|
3
|
+
const fs = require("fs");
|
|
4
|
+
|
|
5
|
+
// Root folder of your project
|
|
6
|
+
const ROOT_DIR = process.cwd();
|
|
7
|
+
|
|
8
|
+
// Uploads folder
|
|
9
|
+
const uploadDir = path.join(ROOT_DIR, "public/uploads");
|
|
10
|
+
|
|
11
|
+
if (!fs.existsSync(uploadDir)) {
|
|
12
|
+
fs.mkdirSync(uploadDir, { recursive: true });
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// Storage config
|
|
16
|
+
const storage = multer.diskStorage({
|
|
17
|
+
destination: (_req, _file, cb) => {
|
|
18
|
+
cb(null, uploadDir);
|
|
19
|
+
},
|
|
20
|
+
filename: (_req, file, cb) => {
|
|
21
|
+
const uniqueSuffix = Date.now() + "-" + Math.round(Math.random() * 1e9);
|
|
22
|
+
const ext = path.extname(file.originalname);
|
|
23
|
+
cb(null, `${file.fieldname}-${uniqueSuffix}${ext}`);
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
// File filter (only images)
|
|
28
|
+
const fileFilter = (_req, file, cb) => {
|
|
29
|
+
if (file.mimetype.startsWith("image/")) {
|
|
30
|
+
cb(null, true);
|
|
31
|
+
} else {
|
|
32
|
+
cb(new Error("Only image files are allowed"), false);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const multerUpload = multer({
|
|
37
|
+
storage,
|
|
38
|
+
limits: { fileSize: 10 * 1024 * 1024 }, // 10MB size cap
|
|
39
|
+
fileFilter,
|
|
40
|
+
}).single("image");
|
|
41
|
+
|
|
42
|
+
// Custom wrapper middleware
|
|
43
|
+
const upload = (req, res, next) => {
|
|
44
|
+
multerUpload(req, res, function (err) {
|
|
45
|
+
if (err) {
|
|
46
|
+
return res.status(400).json({
|
|
47
|
+
message: err.message || "File upload failed",
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (!req.file) {
|
|
52
|
+
req.fileUrl = null // No file uploaded
|
|
53
|
+
return next(); // No file uploaded, continue to next middleware
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Remove trailing slash if exists
|
|
57
|
+
const baseUrl = process.env.APP_URL.replace(/\/$/, "") || "";
|
|
58
|
+
|
|
59
|
+
// Attach full public URL to request
|
|
60
|
+
req.fileUrl = `${baseUrl}/uploads/${req.file.filename}`;
|
|
61
|
+
|
|
62
|
+
next();
|
|
63
|
+
});
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
module.exports = upload;
|