Skip to content

Commit

Permalink
Merge pull request #27 from MengChiehLiu/develop
Browse files Browse the repository at this point in the history
Version 1.0.16
  • Loading branch information
MengChiehLiu authored Aug 24, 2023
2 parents be7ff27 + 8cdaaf3 commit 014abec
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
1 change: 1 addition & 0 deletions backend/server/controllers/entries.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ async function routerDelete(req, res){
await deleteAnEntry(user_id, entry_id)
return res.json({data: {entry: {id: entry_id}}});
}catch(err){
if (err.message === 'Duplicate subjects is not allowed.') return res.status(422).json({error: err.message});
if (err.name === 'CustomError') return res.status(400).json({error: err.message});
console.error(err);
return res.status(500).json({error: 'Internal Server Error'});
Expand Down
18 changes: 17 additions & 1 deletion backend/server/controllers/users.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const router = require('express').Router();
const multer = require('multer');
const path = require('path');
const fs = require('fs').promises;

require('dotenv').config();


Expand All @@ -12,7 +14,7 @@ const toCheck_usersSignIn = ['email','password']
const toCheck_memo = ['title', 'content']

// import models
const { signUpUsers, signInUsers, updateUserPicture, updateUserMemo, getUserInfo } = require('../models/users')
const { signUpUsers, signInUsers, getUserPicture, updateUserPicture, updateUserMemo, getUserInfo } = require('../models/users')


// Signup
Expand Down Expand Up @@ -86,6 +88,14 @@ const upload = multer({ storage: storage });
async function usersPictureUpdate(req, res) {
try {
const userId = req.user.id;

// 執行刪除舊照片
const url = await getUserPicture(userId)
if (url != null){
const oldPicturePath = 'public/images/' + path.basename(url);
await fs.unlink(oldPicturePath);
console.log("Deleted Picture");
}

await new Promise((resolve, reject) => {
// 執行圖片上傳
Expand All @@ -104,7 +114,13 @@ async function usersPictureUpdate(req, res) {
});
});


const protocol = req.protocol; // 通常是 'http' 或 'https'
const host = process.env.BACKEND_HOST; // 獲取主機名,例如 '127.0.0.1:3000'
const serverUrl = `${protocol}://${host}`;
const pictureUrl = `${serverUrl}/api/1.0/images/${path.basename(req.file.path)}`;
const pictureUrl = `https://${process.env.BACKEND_HOST}/api/1.0/images/${path.basename(req.file.path)}`;


await updateUserPicture(userId, pictureUrl);

Expand Down
26 changes: 26 additions & 0 deletions backend/server/models/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,31 @@ async function signInUsers(email, password) {

}

// Find Picture's URL
async function getUserPicture(userId) {
let connection;
try {
connection = await pool.getConnection();
} catch (err) {
console.error("Failed to get connection:", err);
throw err;
}

try {
const query = 'SELECT picture FROM users WHERE id = ?';
const [rows] = await connection.query(query, [userId]);
if (rows && rows.length> 0){
return rows[0].picture;
}
return null
} catch (error) {
console.error('Error getting user picture:', error);
throw error;
} finally {
await connection.release();
}
}

// User's Picture
async function updateUserPicture(userId, pictureUrl) {
let connection;
Expand Down Expand Up @@ -288,6 +313,7 @@ async function getUserInfo(user_id){
module.exports = {
signUpUsers: signUpUsers,
signInUsers: signInUsers,
getUserPicture:getUserPicture,
updateUserPicture: updateUserPicture,
updateUserMemo: updateUserMemo,
getUserInfo: getUserInfo
Expand Down

0 comments on commit 014abec

Please sign in to comment.