-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
feat: QR 및 갤러리에서 사진 업로드 / 삭제 기능 구현
- Loading branch information
Showing
12 changed files
with
748 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
const Photo = require('../models/photo'); | ||
const PhotoTemp = require('../models/photoTemp'); | ||
const User = require('../models/user'); | ||
const { deleteTemp } = require('../middlewares/uploadPhoto'); | ||
const { deleteImages } = require('../middlewares/s3'); | ||
|
||
const createTemp = async (req, res) => { | ||
try{ | ||
console.log(req.body); | ||
const { user_id, uploadedUrl } = req.body; | ||
console.log(uploadedUrl); | ||
|
||
const newTemp = await PhotoTemp.create({ | ||
user_id: user_id, | ||
image_url: uploadedUrl | ||
}); | ||
|
||
res.status(201).json( {status: 'success', newTemp}); | ||
} catch (err) { | ||
console.error('이미지 업로드 중 오류: ', err); | ||
res.status(500).json({ status: 'fail' , message: '이미지 업로드에 실패했습니다.'}); | ||
} | ||
}; | ||
|
||
const updateInfo = async(req, res) => { | ||
try { | ||
const photoTemp_id = req.params.photoTemp_id; | ||
const { date, photobooth_id } = req.body; | ||
|
||
const temp = await PhotoTemp.findByPk(photoTemp_id); | ||
temp.date = date; | ||
temp.photobooth_id = photobooth_id; | ||
await temp.save(); | ||
return res.status(200).json({status: 'success', message: '날짜 및 포토부스 장소 정보가 추가되었습니다.', temp}); | ||
} catch (err) { | ||
console.error('업데이트 중 오류: ', err); | ||
res.status(500).json({ status: 'fail' , message: '이미지 업로드에 실패했습니다.'}); | ||
} | ||
}; | ||
|
||
const updateRecord = async (req, res) => { | ||
try { | ||
const photoTemp_id = req.params.photoTemp_id; | ||
const { hashtag_1, hashtag_2, hashtag_3, record } = req.body; | ||
|
||
const temp = await PhotoTemp.findByPk(photoTemp_id); | ||
temp.hashtag_1 = hashtag_1; | ||
temp.hashtag_2 = hashtag_2; | ||
temp.hashtag_3 = hashtag_3; | ||
temp.record = record; | ||
await temp.save(); | ||
return res.status(200).json({status: 'success', message: '해시태그와 기록이 추가되었습니다.', temp}); | ||
} catch (err) { | ||
console.error('업데이트 중 오류: ', err); | ||
res.status(500).json({ status: 'fail' , message: '이미지 업로드에 실패했습니다.'}); | ||
} | ||
}; | ||
|
||
const savePhoto = async (req, res) => { | ||
try { | ||
const photoTemp_id = req.params.photoTemp_id; | ||
const temp = await PhotoTemp.findByPk(photoTemp_id); | ||
|
||
const newPhoto = await Photo.create({ | ||
user_id: temp.user_id, | ||
photobooth_id: temp.photobooth_id, | ||
image_url: temp.image_url, | ||
date: temp.date, | ||
record: temp.record, | ||
hashtag_1: temp.hashtag_1, | ||
hashtag_2: temp.hashtag_2, | ||
hashtag_3: temp.hashtag_3 | ||
}); | ||
|
||
if(!await deleteTemp(photoTemp_id)){ | ||
res.status(400).json({status: 'fail', message: 'Temp 삭제에 실패했습니다.'}); | ||
}; | ||
|
||
return res.status(201).json({status: 'success', message: '사진이 성공적으로 저장되었습니다!', newPhoto}); | ||
} catch (err) { | ||
console.error('사진 저장 중 오류: ', err); | ||
res.status(500).json({ status: 'fail' , message: '이미지 저장에 실패했습니다.'}); | ||
} | ||
}; | ||
|
||
const deletePhoto = async (req, res) => { | ||
try { | ||
const photo_id = req.params.photo_id; | ||
const photo = await Photo.findByPk(photo_id); | ||
|
||
await deleteImages([photo.image_url]); | ||
await photo.destroy(); | ||
|
||
return res.status(204).json({message: '이미지를 삭제했습니다.'}); | ||
} catch(err) { | ||
console.error('사진 삭제 중 오류: ', err); | ||
res.status(500).json({message: '이미지 삭제에 실패했습니다.'}); | ||
} | ||
}; | ||
|
||
|
||
module.exports = { createTemp, updateInfo, updateRecord, savePhoto, deletePhoto }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
const axios = require('axios'); | ||
const cheerio = require('cheerio'); | ||
const PhotoTemp = require('../models/photoTemp'); | ||
const { deleteImages } = require('../middlewares/s3'); | ||
|
||
const photoismQR = async (uid) => { | ||
try { | ||
url = 'https://cmsapi.seobuk.kr/v1/etc/seq/resource'; | ||
const jsonData = { uid: uid }; | ||
console.log(uid); | ||
|
||
const response = await axios.post(url, jsonData); | ||
const data = response.data; | ||
|
||
if (data.code === 1003) { | ||
return 'expired'; | ||
} | ||
|
||
const imagePath = data.content.fileInfo.picFile.path; | ||
console.log("Image Path: ", imagePath); | ||
|
||
return imagePath; | ||
} catch (err) { | ||
console.error('Error:', err); | ||
} | ||
}; | ||
|
||
const harufilmQR = async (url) => { | ||
try { | ||
const basePath = 'http://haru8.mx2.co.kr'; | ||
|
||
const response = await axios.get(url); | ||
const html = response.data; | ||
|
||
const $ = cheerio.load(html); | ||
const imgPath = $('.main_cont img').attr('src'); | ||
const imagePath = basePath + imgPath; | ||
console.log("Image Path: ", imagePath); | ||
|
||
return imagePath; | ||
} catch (err) { | ||
console.error('Error:', err); | ||
res.status(500).send('Failed to download an upload image'); | ||
} | ||
}; | ||
|
||
|
||
const uploadImageByQR = async(req, res, next) => { | ||
console.log(req.body); | ||
const qr_url = req.query.url; | ||
console.log(qr_url); | ||
|
||
try { | ||
const parsedUrl = new URL(qr_url); | ||
const domain = parsedUrl.hostname; | ||
|
||
let file; | ||
if (domain === 'haru8.mx2.co.kr') { | ||
imageUrl = await harufilmQR(qr_url); | ||
// 하루필름 | ||
} else if (domain === 'qr.seobuk.kr') { | ||
const uid = parsedUrl.searchParams.get('u'); | ||
if (uid === null) | ||
res.status(400).json({status: 'fail', message: '이미 만료된 QR입니다.'}); | ||
imageUrl = await photoismQR(uid); | ||
// 포토이즘 | ||
} else { | ||
res.status(400).send('지원되지 않는 도메인입니다.'); | ||
return; | ||
} | ||
|
||
console.log(file); | ||
|
||
if (file === 'expired') | ||
res.status(400).json({status: 'fail', message: '이미 만료된 QR 입니다.'}); | ||
|
||
req.body.imageUrl = imageUrl; | ||
next(); | ||
} | ||
catch (err) { | ||
console.error('Error:', err); | ||
res.status(500).send('Failed to download an upload image'); | ||
} | ||
}; | ||
|
||
const deleteTemp = async (photoTemp_id) => { | ||
try { | ||
const temp = await PhotoTemp.findByPk(photoTemp_id); | ||
await temp.destroy(); | ||
return true; | ||
} catch (error) { | ||
console.error(error); | ||
return false; | ||
} | ||
}; | ||
|
||
module.exports = { uploadImageByQR, deleteTemp }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.