From f9200425b0b21f213a6c334557cb39beeb390a61 Mon Sep 17 00:00:00 2001 From: Aaron.Liu <427787340@qq.com> Date: Wed, 2 May 2018 16:53:48 +0800 Subject: [PATCH] 增加文件上传功能 --- app/controller/file.js | 28 ++++++++++++++++++++++++++++ app/router.js | 2 ++ app/service/contract.js | 1 - config/config.default.js | 10 ++++++++++ package.json | 4 +++- 5 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 app/controller/file.js diff --git a/app/controller/file.js b/app/controller/file.js new file mode 100644 index 0000000..cdb84f6 --- /dev/null +++ b/app/controller/file.js @@ -0,0 +1,28 @@ +'use strict'; + +const fs = require('fs'); +const path = require('path'); +const Controller = require('egg').Controller; +const awaitWriteStream = require('await-stream-ready').write; +const sendToWormhole = require('stream-wormhole'); +const uuid = require('node-uuid'); + +class FileController extends Controller { + + async upload() { + const stream = await this.ctx.getFileStream(); + const filename = uuid.v1() + path.extname(stream.filename).toLowerCase(); + const target = path.join(this.config.baseDir, 'app/public', filename); + const writeStream = fs.createWriteStream(target); + try { + await awaitWriteStream(stream.pipe(writeStream)); + } catch (err) { + await sendToWormhole(stream); + throw err; + } + + this.ctx.body = { url: '/public/' + filename }; + } +} + +module.exports = FileController; diff --git a/app/router.js b/app/router.js index 1ef805f..3371f30 100644 --- a/app/router.js +++ b/app/router.js @@ -8,6 +8,8 @@ module.exports = app => { router.get('/', controller.home.index); // 首页统计 router.get('/statistic/count', controller.statistic.count); + // 文件上传 + router.post('/file/upload', controller.file.upload); // 用户管理 router.post('/user/password', controller.user.password); router.post('/user/validate', controller.user.validate); diff --git a/app/service/contract.js b/app/service/contract.js index b3bf6e7..5c6e401 100644 --- a/app/service/contract.js +++ b/app/service/contract.js @@ -11,7 +11,6 @@ class ContractService extends Service { select contract.*, user.name as manager_name from contract, user where contract.manager_id = user.id ${where !== '' ? `and ${where}` : ''} `; - console.log(sql); const result = await this.app.mysql.query(sql); return result; } diff --git a/config/config.default.js b/config/config.default.js index e62321e..d01224b 100644 --- a/config/config.default.js +++ b/config/config.default.js @@ -39,5 +39,15 @@ module.exports = appInfo => { credentials: true, }; + config.multipart = { + // will append to whilelist + fileExtensions: [ + '.doc', + '.docx', + '.xls', + '.xlsx', + ], + }; + return config; }; diff --git a/package.json b/package.json index 24a5d09..00e6d66 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,9 @@ "egg-mysql": "^3.0.0", "egg-scripts": "^2.5.0", "moment": "^2.22.1", - "node-uuid": "^1.4.8" + "node-uuid": "^1.4.8", + "await-stream-ready": "^1.0.1", + "stream-wormhole": "^1.0.3" }, "devDependencies": { "autod": "^3.0.1", -- libgit2 0.21.0