Skip to content

Commit a4227d7

Browse files
committed
feat: update image
1 parent ad8ffe1 commit a4227d7

File tree

7 files changed

+270
-6
lines changed

7 files changed

+270
-6
lines changed

package-lock.json

Lines changed: 119 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"jsonwebtoken": "^9.0.2",
2626
"knex": "^3.1.0",
2727
"morgan": "^1.10.0",
28+
"multer": "^1.4.5-lts.1",
2829
"node-cron": "^3.0.3",
2930
"nodemailer": "^6.9.9",
3031
"nodemailer-express-handlebars": "^6.1.0",

src/app/v2/controllers/image.controller.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,23 @@ const { SuccessResponse } = require("../../../cores/success.response");
33
const imageService = require("../services/image.service");
44

55
class UploadController {
6-
async upload(_, res, ___) {
6+
async upload(req, res, ___) {
77
new SuccessResponse({
8-
metadata: await imageService.upload(),
8+
metadata: await imageService.upload(req.userInfo, req.file),
9+
}).send(res);
10+
}
11+
12+
async uploadMulti(req, res, ___) {
13+
new SuccessResponse({
14+
metadata: await imageService.uploadMulti(req.userInfo, {
15+
files: req.files,
16+
}),
17+
}).send(res);
18+
}
19+
20+
async removeImage(req, res, ___) {
21+
new SuccessResponse({
22+
metadata: await imageService.removePublicId(req.body),
923
}).send(res);
1024
}
1125
}

src/app/v2/routes/images/index.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,27 @@ const express = require("express");
44
//* IMPORT
55
const imageController = require("../../controllers/image.controller");
66
const { asyncHandler } = require("../../../../commons/helpers/asyncHandler");
7+
const { uploadDisk } = require("../../../../commons/configs/multer.config");
8+
const {
9+
checkAuthorizationAccessToken,
10+
} = require("../../../../auth/check.auth");
711

812
const router = express.Router();
913

10-
router.post("/upload", asyncHandler(imageController.upload));
14+
router.use(checkAuthorizationAccessToken);
15+
16+
router.post(
17+
"/upload",
18+
uploadDisk.single("file", 1),
19+
asyncHandler(imageController.upload)
20+
);
21+
22+
router.post(
23+
"/upload/multi",
24+
uploadDisk.array("files", 3),
25+
asyncHandler(imageController.uploadMulti)
26+
);
27+
28+
router.post("/remove", asyncHandler(imageController.removeImage));
1129

1230
module.exports = router;

src/app/v2/services/image.service.js

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,96 @@
1+
//* LIB
2+
const _ = require("lodash");
3+
const fs = require("fs");
4+
5+
//* IMPORT
6+
const cloudinary = require("../../../databases/init.cloudinary");
7+
const { BadRequestRequestError } = require("../../../cores/error.response");
8+
19
class ImageService {
2-
async upload() {
3-
return "Ok";
10+
async upload(info, { path, folderName = "class/" }) {
11+
console.log(path, "----");
12+
if (_.isEmpty(info) || _.isEmpty(path)) {
13+
await this.removeTmp(path);
14+
throw new BadRequestRequestError();
15+
}
16+
17+
const uploadResult = await cloudinary.uploader.upload(
18+
path,
19+
function (res) {
20+
console.log(res);
21+
},
22+
{
23+
public_id: Date.now() + "thumb",
24+
folder: folderName + info.id,
25+
use_filename: true,
26+
}
27+
);
28+
29+
const thumbUrl = await cloudinary.url(uploadResult.public_id, {
30+
height: 100,
31+
width: 100,
32+
format: "jpg",
33+
});
34+
35+
return {
36+
public_id: uploadResult.public_id,
37+
image_url: uploadResult.secure_url,
38+
thumb_size: thumbUrl,
39+
info,
40+
};
41+
}
42+
async uploadMulti(info, { files }, folderName = "class/") {
43+
if (_.isEmpty(info) || _.isEmpty(files)) {
44+
throw new BadRequestRequestError();
45+
}
46+
47+
const uploadTasks = files.map(async (file) => {
48+
const result = await cloudinary.uploader.upload(
49+
file.path,
50+
{},
51+
{
52+
public_id: Date.now() + "thumb",
53+
folder: folderName + info.id,
54+
}
55+
);
56+
57+
const thumbSize = await cloudinary.url(result.public_id, {
58+
height: 100,
59+
width: 100,
60+
format: "jpg",
61+
});
62+
63+
return {
64+
image_url: result.secure_url,
65+
shopId: 8409,
66+
thumb_size: thumbSize,
67+
};
68+
});
69+
return await Promise.all(uploadTasks);
70+
}
71+
72+
async removePublicId({ public_id }) {
73+
if (_.isEmpty(public_id)) {
74+
throw new BadRequestRequestError();
75+
}
76+
77+
const result = await cloudinary.uploader.destroy(public_id, {});
78+
79+
return result;
80+
}
81+
82+
async removeTmp(path) {
83+
return new Promise((resolve, reject) => {
84+
fs.unlink(path, (err) => {
85+
if (err) {
86+
console.error("Error removing temporary file:", err);
87+
reject(err);
88+
} else {
89+
console.log("Temporary file removed successfully.");
90+
resolve();
91+
}
92+
});
93+
});
494
}
595
}
696

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"use strict";
2+
3+
//* LIB
4+
const multer = require("multer");
5+
6+
const uploadMemory = multer({ storage: multer.memoryStorage() });
7+
8+
const uploadDisk = multer({
9+
storage: multer.diskStorage({
10+
destination: function (req, file, cb) {
11+
cb(null, "./src/uploads");
12+
},
13+
filename: function (req, file, cb) {
14+
const uniqueSuffix = Date.now() + file.originalname;
15+
cb(null, uniqueSuffix);
16+
},
17+
}),
18+
});
19+
20+
module.exports = {
21+
uploadMemory,
22+
uploadDisk,
23+
};

src/uploads/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)