diff --git a/app.js b/app.js index 0bf214a663a6e3e56d6e3e1f1020aada400039e5..ae371fba14a8bed3ab137fd3251f6a1cb8ee214d 100644 --- a/app.js +++ b/app.js @@ -8,6 +8,7 @@ const path = require("path"); var bookRouter = require('./routes/BookGate'); var borrowRouter = require('./routes/BorrowGate'); const loginRouter = require('./routes/LoginGate') +const ImgRouter = require('./routes/ImgGate') var bodyParser = require('body-parser') @@ -58,6 +59,7 @@ app.use(cors()); app.use('/book', bookRouter); app.use('/borrow', borrowRouter); app.use('/login', loginRouter) +app.use('/Img', ImgRouter) //建立https服务器 https.createServer({ ca, cert, key }, app).listen(8443, () => { diff --git a/asserts/img01.png b/asserts/img01.png new file mode 100644 index 0000000000000000000000000000000000000000..845b1375444b0c1acdd3f9945f17e7f10d8a6653 Binary files /dev/null and b/asserts/img01.png differ diff --git a/controllers/ImgCtrl.js b/controllers/ImgCtrl.js index d78bdf1c48b883873dca25f3f9af360e74d113c4..35042dfe0947a473a0017294847fc4a1f14922d2 100644 --- a/controllers/ImgCtrl.js +++ b/controllers/ImgCtrl.js @@ -1,30 +1,67 @@ const express = require('express'); const app = express(); const path = require('path'); -const multer = require('multer'); const axios = require('axios'); const fs = require('fs'); -// 设置存储上传文件的目录 -const storage = multer.diskStorage({ - // 定义了文件存储的目录,这里是uploads/目录 - destination: (req, file, cb) => { - cb(null, 'uploads/'); - }, - // 使用了当前时间戳加上原始文件名的扩展名作为文件名 - filename: (req, file, cb) => { - const ext = path.extname(file.originalname); - cb(null, Date.now() + ext); - } -}); - -// 创建multer中间件 -const upload = multer({ storage }) +const { client } = require('../util/ossconfig') +// 定义filepath下载地址 +// const filePath = 'D:/Word/Study/跨平台脚本/大作业/bigwork/asserts/' +const filePath = path.dirname(__dirname) + '/asserts/' // 处理post请求,上传文件到阿里云图床 uploadImg = async (req, res) => { console.log(`enter controller uploadImg`); try { - const - } -} \ No newline at end of file + const { file } = req + const { filename } = file + + // 获取文件后缀名,如'.jpg' + let index = filename.lastIndexOf('.') + let type = filename.substr(index) + + // 随机生成16进制 + let fileName = Math.random().toString(16).slice(2) + // 拼接新文件名(随机数+文件后缀名) + fileName = `${fileName}${type}` + + // 上传文件,这里是上传到OSS的 upload文件夹下 + const result = await client.put('upload/' + fileName, fs.createReadStream(file.path)) + console.log('success put'); + // 删除该文件 + // fs.unlinkSync(file.path) + if (result) { + res.status(200).send('success uploadImg') + } + else { + res.status(500).send('failed upload!') + } + } catch (error) { + console.log(error); + res.status(400).send('error') + } +} + +// 处理get请求,下载相应文件到本地 +downloadImg = async (req, res) => { + console.log(`enter controller downloadImg`); + console.log(filePath); + try { + const { fileName } = req.body + + const result = await client.get('upload/' + fileName, filePath + fileName) + if (result) { + console.log('success downloadImg'); + res.status(200).send('success downloadImg') + } + else { + console.log('failed downloadImg!'); + res.status(500).send('failed download!') + } + } catch (error) { + console.log(error); + res.status(400).send('error') + } +} + +module.exports = { uploadImg, downloadImg } \ No newline at end of file diff --git a/controllers/borrowCtrl.js b/controllers/borrowCtrl.js index 5b234a0c771e5774a5e03b8761fcdbf2741477cd..a67d9e2e746e240a15f02e895a2df573f8f242ed 100644 --- a/controllers/borrowCtrl.js +++ b/controllers/borrowCtrl.js @@ -29,8 +29,13 @@ borrowBook = async (req, res) => { userId: user.id, borrowDate: new Date() }) - res.status(200).send('Success Borrow') - console.log('Success Borrow'); + if (isInserted) { + res.status(200).send('Success Borrow') + console.log('Success Borrow'); + } else { + console.log('failed insert!'); + res.status(500).send('failed insert!') + } } else { res.status(401).send('Not Found') @@ -40,4 +45,30 @@ borrowBook = async (req, res) => { } } -module.exports = { borrowBook } \ No newline at end of file +// 还书 +backBook = async (req, res) => { + console.log('enter controller backBook'); + + // 获取数据 + const { bookId, userId } = req.body + + try { + const isDestoryed = await Borrow.destroy({ + where: { + bookId: bookId, + userId: userId + } + }) + if (isDestoryed) { + res.status(200).send('Success Borrow') + console.log('Success Borrow'); + } else { + console.log('failed insert!'); + res.status(500).send('failed insert!') + } + } catch (error) { + res.status(404).send(error.message) + } + +} +module.exports = { borrowBook, backBook } \ No newline at end of file diff --git a/routes/BorrowGate.js b/routes/BorrowGate.js index 26837c165ca47db953d5720b48c0b94dd24d928e..44575734043b2c7b902fc7448664de2ffd7fccec 100644 --- a/routes/BorrowGate.js +++ b/routes/BorrowGate.js @@ -5,5 +5,6 @@ const Borrow = require('../controllers/borrowCtrl'); // router.post('/getBorrowList', Borrow.getBorrowList) // 借书 router.post('/borrowBook', Borrow.borrowBook) +router.post('/backBook', Borrow.backBook) module.exports = router \ No newline at end of file diff --git a/routes/ImgGate.js b/routes/ImgGate.js index 100f1f6902cc9dbadafcf31b18c8c0b385bef08a..d7969029300eb6475cab36705055dd832db7f6df 100644 --- a/routes/ImgGate.js +++ b/routes/ImgGate.js @@ -2,23 +2,28 @@ const express = require('express'); var router = express.Router(); const Img = require('../controllers/ImgCtrl') const multer = require('multer'); +const path = require('path'); +const { authenticateToken } = require('../util/JWT') // 设置存储上传文件的目录 const storage = multer.diskStorage({ // 定义了文件存储的目录,这里是uploads/目录 destination: (req, file, cb) => { - cb(null, 'uploads/'); + cb(null, 'D:/Word/Study/跨平台脚本/大作业/bigwork/asserts'); }, - // 使用了当前时间戳加上原始文件名的扩展名作为文件名 filename: (req, file, cb) => { - const ext = path.extname(file.originalname); - cb(null, Date.now() + ext); + // 使用了当前时间戳加上原始文件名的扩展名作为文件名 + // const ext = path.extname(file.originalname); + // cb(null, 'img01' + ext); + + cb(null, file.originalname); // 保留原始文件名 } }); // 创建multer中间件 const upload = multer({ storage }) // 上传图片至阿里云图床 -router.post('/upload', upload.single('img'), Img.uploadImg) +router.post('/upload', authenticateToken, upload.single('file'), Img.uploadImg) +router.get('/download', authenticateToken, Img.downloadImg) module.exports = router \ No newline at end of file diff --git a/util/ossconfig.js b/util/ossconfig.js index 9ef80902862f4a1ada7bd5d25c83d33e68cebd64..3bac9ab91212b80cad4cd7a547725e5e38010c20 100644 --- a/util/ossconfig.js +++ b/util/ossconfig.js @@ -10,4 +10,4 @@ const client = new OSS({ bucket: 'oasis712' }) -export { client } \ No newline at end of file +module.exports = { client } \ No newline at end of file