diff --git a/src/controllers/api/emoticon.js b/src/controllers/api/emoticon.js index a944bd1..0ded072 100644 --- a/src/controllers/api/emoticon.js +++ b/src/controllers/api/emoticon.js @@ -1,6 +1,8 @@ const jwt = require('../../jwt') const model = require('../../models/emoticon') const multer = require('multer') +const fs = require('fs'); +const emoticon = require('../../models/emoticon'); let result, dbResult; const getemoticon = async (req, res) => { @@ -22,6 +24,22 @@ const getemoticons = async (req, res) => { res.send(JSON.stringify(result)) } const uploadProcessing = multer({ + fileFilter:(req, file, cb) => { + // 파일 확장자 체크 + const allowExt = [ + 'png', + 'jpg', + 'gif', + 'webp' + ] + const ext = file.originalname.split('.')[file.originalname.split('.').length-1]; + // 이모티콘 썸네일은 png만 허용 + if(file.fieldname=='file'){ + if(ext != 'png') return cb(null, false); + } + if(allowExt.indexOf(ext)) return cb(null, false); + cb(null, true) + }, storage:multer.diskStorage({ destination:(req, file, cb) => { cb(null, 'public/resource/board/emoticon/temp') @@ -31,6 +49,9 @@ const uploadProcessing = multer({ const ext = file.originalname.split('.')[file.originalname.split('.').length-1]; file.name = name; file.ext = ext; + if(file.fieldname=='file'){ + return cb(null, `icons/${name}.${ext}`) + } cb(null, `${name}.${ext}`) } }) @@ -43,25 +64,59 @@ const uploadCheck = async (req, res) => { if(!req.body.name || !req.body.description || !req.files.file || !req.files.files || !req.body.emoticons){res.send(JSON.stringify({status:3,subStatus:0}));return;} if(req.body.name.length<2){res.send(JSON.stringify({status:3,subStatus:0}));return;} if(req.body.description.length<2){res.send(JSON.stringify({status:3,subStatus:0}));return;} - if(req.files.length<2){res.send(JSON.stringify({status:3,subStatus:0}));return;} + if(req.files.files.length<4){res.send(JSON.stringify({status:3,subStatus:0}));return;} req.body.emoticons = JSON.parse(req.body.emoticons) if(req.files.files.length != (Object.keys(req.body.emoticons).length)){res.send(JSON.stringify({status:3,subStatus:0}));return;} + let emoticonList = [] for(let i=0;i{ + return fs.promises.copyFile(e.path, `public/resource/board/emoticon/${req.body.id}/${req.body.emoticons[e.name].idx}.${req.body.emoticons[e.name].type}`) + }) + copyList.push(fs.promises.copyFile(req.files.file[0].path, `public/resource/board/emoticon/${req.body.id}.png`)) + // 파일 복사 프로미스 + await Promise.all(copyList); + }catch(err){ + console.error(err) + res.send(JSON.stringify({status:2,subStatus:0}));return; + } + try{ + // 이모티콘들 db에 저장 + await model.uploadEmoticons(req.body.id, emoticonList); + }catch(err){ + console.error(err) + res.send(JSON.stringify({status:2,subStatus:0}));return; } res.send(JSON.stringify({status:1,subStatus:0}));return; - // dbResult = await model.uploadEmoticonInfo(req.body.name, req.body.description, jwtValue.memberCode) - // if(!dbResult){ - // res.send(JSON.stringify({status:2,subStatus:0}));return; - // }else{ - // req.body.id=dbResult; - // next(); - // } } module.exports = { diff --git a/src/controllers/api/version.ts b/src/controllers/api/version.ts index 405da6b..043deb3 100644 --- a/src/controllers/api/version.ts +++ b/src/controllers/api/version.ts @@ -13,8 +13,8 @@ const get = async (req:express.Request, res:express.Response) =>{ result={ status:1, subStatus:0, - versionCode:2, - versionName:'1.2.0' + versionCode:3, + versionName:'1.3.0' } break case 'app': diff --git a/src/models/emoticon.js b/src/models/emoticon.js index ff0cbf9..d5b7d05 100644 --- a/src/models/emoticon.js +++ b/src/models/emoticon.js @@ -3,7 +3,7 @@ const pool = require('../db') const getemoticon = async (id) => { let result = {} let rows - const getEmoticonQuery = "SELECT `name`, `created` FROM `emoticon` WHERE `id`=?" + const getEmoticonQuery = "SELECT `name`, `description`, `created` FROM `emoticon` WHERE `id`=?" try{ [rows] = await pool.query(getEmoticonQuery, [id]) }catch(err){ @@ -13,8 +13,9 @@ const getemoticon = async (id) => { if(!rows.length) return null; result = { id:id, - alt:rows[0].name, + name:rows[0].name, created:rows[0].created, + description:rows[0].description, e:[] } let n @@ -93,17 +94,24 @@ const uploadEmoticonInfo = async (name, description, memberCode) => { } const insertEmoticonsQuery = "INSERT INTO emoticon values(?, ?, ?, now(), ?)" try{ - pool.query(insertEmoticonsQuery, [rows[0].AUTO_INCREMENT], name, description, memberCode) + pool.query(insertEmoticonsQuery, [rows[0].AUTO_INCREMENT, name, description, memberCode]) }catch(err){ console.error(err) return null; } return rows[0].AUTO_INCREMENT } -const uploadEmoticon = (id, idx, type) => { - const insertEmoticonsQuery = "INSERT INTO emoticons values(?, ?, ?)" +const uploadEmoticons = (id, emoticonList) => { + let temp = [] + let params = [] + // 한 번에 insert 하기 위해 + emoticonList.forEach(e => { + params.push(id, e.idx, e.type); + temp.push('(?,?,?)') + }); + const insertEmoticonsQuery = `INSERT INTO emoticons values ${temp.join(',')}`; try{ - pool.query(insertEmoticonsQuery, id, idx, type) + pool.query(insertEmoticonsQuery, params) }catch(err){ console.error(err) return null; @@ -114,5 +122,5 @@ module.exports = { getemoticon:getemoticon, getemoticons:getemoticons, uploadEmoticonInfo:uploadEmoticonInfo, - uploadEmoticon:uploadEmoticon + uploadEmoticons:uploadEmoticons } \ No newline at end of file