Gitlab@Informatics

Skip to content
Snippets Groups Projects
Commit 3d64fdfc authored by 65160118's avatar 65160118
Browse files

commit 6

parent fbe75346
No related branches found
No related tags found
No related merge requests found
......@@ -2,28 +2,6 @@ const BookModel = require('../models/bookModel');
const multer = require('multer');
const path = require('path');
// Configure multer for file uploads
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'public/uploads/')
},
filename: (req, file, cb) => {
cb(null, `${Date.now()}-${file.originalname}`)
}
});
const upload = multer({
storage,
limits: { fileSize: 5 * 1024 * 1024 },
fileFilter: (req, file, cb) => {
const filetypes = /jpeg|jpg|png/;
const mimetype = filetypes.test(file.mimetype);
const extname = filetypes.test(path.extname(file.originalname).toLowerCase());
if (mimetype && extname) return cb(null, true);
cb(new Error('Only .png, .jpg and .jpeg format allowed!'));
}
}).single('image_file');
exports.getBookLists = async (req, res) => {
if (!req.session.user) {
return res.redirect('/login');
......@@ -144,31 +122,19 @@ exports.getAddBook = async (req, res) => {
};
exports.postBook = async (req, res) => {
if (!req.session.user) return res.redirect('/login');
upload(req, res, async (err) => {
if (err) {
console.error('Upload error:', err);
return res.status(400).send('Error uploading file: ' + err.message);
if (!req.session.user) {
return res.redirect('/login');
}
try {
const { bookname, author, category, type, list_id, imageOption } = req.body;
const image_url = imageOption === 'file' && req.file ?
`/uploads/${req.file.filename}` : req.body.image_url;
const { bookname, author, category, type, list_id, image_url } = req.body;
const bookModel = new BookModel(req.db);
await bookModel.createBook({
bookname, author, category, type, image_url, list_id,
user_id: req.session.user.user_id
});
try {
await bookModel.addBook(bookname, author, category, type, list_id, req.session.user.user_id, image_url);
res.redirect(`/books/${list_id}`);
} catch (error) {
console.error('Error creating book:', error);
res.status(500).send('เกิดข้อผิดพลาดในการเพิ่มหนังสือ');
}
});
};
exports.getEditBook = async (req, res) => {
......@@ -286,3 +252,69 @@ exports.getBookInfo = async (req, res) => {
res.status(500).send('Error loading book information');
}
};
// Configure multer for file uploads
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'public/uploads/')
},
filename: (req, file, cb) => {
cb(null, `${Date.now()}-${file.originalname}`)
}
});
const upload = multer({
storage: storage,
limits: {
fileSize: 5 * 1024 * 1024 // 5MB limit
},
fileFilter: (req, file, cb) => {
const filetypes = /jpeg|jpg|png/;
const mimetype = filetypes.test(file.mimetype);
const extname = filetypes.test(path.extname(file.originalname).toLowerCase());
if (mimetype && extname) {
return cb(null, true);
}
cb(new Error('Only .png, .jpg and .jpeg format allowed!'));
}
}).single('image_file');
exports.postBook = async (req, res) => {
if (!req.session.user) {
return res.redirect('/login');
}
upload(req, res, async (err) => {
if (err) {
console.error('Upload error:', err);
return res.status(400).send('Error uploading file: ' + err.message);
}
try {
const { bookname, author, category, type, list_id, imageOption } = req.body;
let image_url = '';
if (imageOption === 'file' && req.file) {
image_url = `/uploads/${req.file.filename}`;
} else {
image_url = req.body.image_url;
}
const bookModel = new BookModel(req.db);
await bookModel.createBook({
bookname,
author,
category,
type,
image_url,
list_id,
user_id: req.session.user.user_id
});
res.redirect(`/books/${list_id}`);
} catch (error) {
console.error('Error creating book:', error);
res.status(500).send('เกิดข้อผิดพลาดในการเพิ่มหนังสือ');
}
});
};
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment