const uploadFile = uploadS3.single('file');
import { check } from "express-validator";
router.put(
'/',
authJWT,
// multer error handling
function(req, res) { uploadFile(req, res, function (err) {
if (err) {
return res.status(400).json({
success: false,
response: [],
error: '확장자가 .png, .gif, .jpg and .jpeg 이미지 가능합니다!'
});
}
})
},
[
check("name", "name value id required").trim().isLength({ min: 1, max: 20 }).not().isEmpty(),
// errors 체킹하는 부분 추가!! validation
],
update
);
import multer from 'multer'
import multerS3 from 'multer-s3'
import aws from 'aws-sdk'
import dotenv from 'dotenv'
dotenv.config()
const s3 = new aws.S3({
/*
직접 입력 해도되고 , aws configure로 설정해 줘도된다.
*/
})
const MAXSIZE = 1024 * 1024 * 50
const fileFilter = (req, file, cb) => {
if (file.mimetype == "image/png"
|| file.mimetype == "image/jpg"
|| file.mimetype == "image/jpeg"
|| file.mimetype == "image/gif") {
cb(null, true);
} else {
cb(null, false);
cb(new Error('Only .png, .gif, .jpg and .jpeg format allowed!'));
}
}
export const uploadS3 = multer({
storage: multerS3({
s3: s3,
bucket: process.env.BUCKET_NAME,
contentType: multerS3.AUTO_CONTENT_TYPE,
key: function (req, file, callback) {
console.log(file);
callback(null, `${Date.now()}_${file.originalname}`)
},
acl: 'public-read',
}),
limits: {
fileSize: MAXSIZE,
},
fileFilter: fileFilter,
})
export const upload = multer({
storage: multer.memoryStorage({}),
limits: {
fileSize: MAXSIZE,
},
fileFilter: fileFilter
})
import mypageService from '../service/mypage.service.js';
import { validationResult } from 'express-validator';
export async function update(req, res) {
/**
errors 체킹하는 부분 추가!! validation
*/
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({
success: false,
response: [],
error: errors.array() });
}
try {
const searchWallet = await mypageService.findWallet({ id: req.id }); // get from jwt token
if (!isNaN(searchWallet.id)) {
return res.status(401).json({
success: false,
response: null,
error: {
status: 400,
message: "인증되지 않은 사용자",
},
});
}
let wallet = {
id: req.id, // get from jwt token
image: req.file.location, // S3 주소, req.file.location
username: req.body.name,
};
// 업데이트
const updateWallet = await mypageService.updateWalelt(wallet);
console.log(updateWallet[0]);
// 조회하는 로직
const retrieveWallet = await mypageService.findWallet({ id: updateWallet[0].id });
return res.status(200).json({
success: true,
response: retrieveWallet,
error: null,
});
} catch (err) {
return res.status(500).json({
success: false,
response: null,
error: {
status: 500,
message: err.message,
},
});
}
}
export default {
update,
};
express-validator