Skip to content
This repository has been archived by the owner on Oct 31, 2022. It is now read-only.

Commit

Permalink
Add: 이모티콘 업로드 기능
Browse files Browse the repository at this point in the history
  • Loading branch information
leehj050211 committed Jan 30, 2022
1 parent 1b1a695 commit 726cea0
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 18 deletions.
73 changes: 64 additions & 9 deletions src/controllers/api/emoticon.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -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')
Expand All @@ -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}`)
}
})
Expand All @@ -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<req.files.files.length;i++){
const e = req.files.files[i];
if(!req.body.emoticons[e.name]){res.send(JSON.stringify({status:3,subStatus:0}));return;}
if(e.ext != req.body.emoticons[e.name].type){
// 같은 확장자인지, 번호가 숫자가 맞는지 체크
if(e.ext != req.body.emoticons[e.name].type || !(/^\d+$/.test(req.body.emoticons[e.name].idx))){
res.send(JSON.stringify({status:3,subStatus:0}));
return;
}
emoticonList.push({
idx:req.body.emoticons[e.name].idx,
type:req.body.emoticons[e.name].type
});
}

// 이모티콘 정보 db에 저장
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;
}
// 폴더 생성
try{
await fs.promises.mkdir(`public/resource/board/emoticon/${req.body.id}`)
}catch(err){
console.error(err)
res.send(JSON.stringify({status:2,subStatus:0}));return;
}
try{
// 복사할 파일 리스트 생성
let copyList = []
copyList = req.files.files.map(e=>{
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 = {
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/api/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
22 changes: 15 additions & 7 deletions src/models/emoticon.js
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -114,5 +122,5 @@ module.exports = {
getemoticon:getemoticon,
getemoticons:getemoticons,
uploadEmoticonInfo:uploadEmoticonInfo,
uploadEmoticon:uploadEmoticon
uploadEmoticons:uploadEmoticons
}

0 comments on commit 726cea0

Please sign in to comment.