diff --git a/.gitignore b/.gitignore index 8839e2d..8e42a12 100644 --- a/.gitignore +++ b/.gitignore @@ -105,5 +105,3 @@ dist uploads/ .vscode - -awsconfig.json \ No newline at end of file diff --git a/config.js b/config.js index edbe991..f52c54a 100644 --- a/config.js +++ b/config.js @@ -31,5 +31,10 @@ export const config = { }, serverUrl: { serverUrl: required('SERVER_URL','http://localhost:8080/') + }, + s3: { + access_key_id: required('ACCESS_KEY_ID'), + secret_access_key: required('SECRET_ACCESS_KEY'), + region: required('REGION'), } -}; +}; \ No newline at end of file diff --git a/controller/board.controller.js b/controller/board.controller.js index 0378aee..62b0729 100644 --- a/controller/board.controller.js +++ b/controller/board.controller.js @@ -1,5 +1,5 @@ import * as boardRepository from '../data/board.js'; - +import {s3} from '../s3.js'; //게시글 생성 export async function createPost(req, res) { const { title, contents, eventId, attendMembers } = req.body; @@ -117,25 +117,42 @@ export async function deleteComment(req, res){ await boardRepository.deleteComment(id); res.sendStatus(204); } -4 + //파일 업로드 export async function upload(req, res, err) { const boardId = req.body.boardId; const image = req.files; - console.log(image.length); - console.log(req.fileValidationError); + console.log(`image length = ${image.length}, fileValidationError = ${req.fileValidationError}`); console.log(image[0]); const path = image.map(img => img.location); - console.log(`path = ${path}`); if(req.fileValidationError){//파일이 크거나 형식이 다를때 return res.status(400).send({message: req.fileValidationError}); } if(image.length < 1){//이미지가 없을때 return res.status(400).send(util.fail(400, "이미지가 없습니다")); } - const result = await boardRepository.imageUpload(boardId, image ) - return res.status(200).send(util.success(200, "업로드를 완료했습니다", path)); + const imageId = await boardRepository.imageUpload(boardId, image ) + return res.status(200).send(util.success(200, "업로드를 완료했습니다", { imageId: imageId, path: path})); +} + +export async function deleteImage(req, res){ + const id = req.params.id; + const deleteId = await boardRepository.findByImageId(id); + console.log(deleteId); + + if(!deleteId) //삭제할 이미지가 없다면 + return res.status(404).json({message: '삭제할 사진이 없습니다'}); + s3.deleteObject({ + Bucket: 'together42', + Key: deleteId.fileKey + }, function(err, data){ + if(err) + console.log(err); + console.log(data); + }); + await boardRepository.deleteImage(id); + res.sendStatus(204); } const util = { diff --git a/data/board.js b/data/board.js index 044e1e3..a315cd0 100644 --- a/data/board.js +++ b/data/board.js @@ -157,7 +157,7 @@ export async function imageUpload(boardId, images) { return db .query('INSERT INTO image_info (boardNum, filePath, fileName, fileType, fileSize, fileKey) VALUES ?', [values]) - .then((result) => result[0]); + .then((result) => result[0].insertId); } export async function getImages(boardId){ @@ -167,4 +167,14 @@ export async function getImages(boardId){ SELECT id as imageId, boardNum as boardId, filePath FROM image_info WHERE boardNum = ? `,[boardId]) .then((result)=>result[0]); +} + +export async function findByImageId(id) { + return db + .execute('SELECT * FROM image_info WHERE id=?',[id]) + .then((result) => result[0][0]); +} + +export async function deleteImage(id) { + return db.execute('DELETE FROM image_info WHERE id=?',[id]); } \ No newline at end of file diff --git a/routes/board.js b/routes/board.js index 02a8e68..0b3982b 100644 --- a/routes/board.js +++ b/routes/board.js @@ -1,19 +1,20 @@ import express from 'express'; import multer from 'multer'; import multerS3 from 'multer-s3'; -import aws from 'aws-sdk'; +//import aws from 'aws-sdk'; import path from 'path'; import 'express-async-errors'; import { body } from 'express-validator'; import { validate } from '../middleware/validator.js' import { isAuth } from '../middleware/auth.js'; import * as boardController from '../controller/board.controller.js'; +import { s3 } from '../s3.js'; const __dirname = path.resolve(); const router = express.Router(); -aws.config.loadFromPath(__dirname + '/awsconfig.json'); +//aws.config.loadFromPath(__dirname + '/awsconfig.json'); -const s3 = new aws.S3(); +//const s3 = new aws.S3(); const storage = multer.diskStorage({ destination: (req, file, cb) => { cb(null, "./uploads/"); @@ -93,5 +94,7 @@ router.delete('/comment/:id', isAuth, boardController.deleteComment); //사진 업로드 router.post('/upload', upload.array("image",8), fileSizeLimitErrorHandler, boardController.upload); +//사진 삭제 +router.delete('/image/remove/:id', boardController.deleteImage); export default router; \ No newline at end of file diff --git a/s3.js b/s3.js new file mode 100644 index 0000000..42be886 --- /dev/null +++ b/s3.js @@ -0,0 +1,8 @@ +import aws from 'aws-sdk'; +import { config } from './config.js'; + +export const s3 = new aws.S3({ + accessKeyId: config.s3.access_key_id, + secretAccessKey: config.s3.secret_access_key, + region: config.s3.region +}) \ No newline at end of file