Skip to content

Commit

Permalink
新增上传模块
Browse files Browse the repository at this point in the history
  • Loading branch information
chengweiou committed Jan 23, 2019
1 parent 1ec3782 commit 319c4cc
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 2 deletions.
17 changes: 17 additions & 0 deletions src/controller/bb/uploadController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const Router = require('koa-router')
const router = new Router()

const service = require('../../service/uploadService')
const valid = require('../../valid')

router.post('/bb/upload', async(ctx, next) => {
let file = ctx.params.file
valid.string('file', file).is().include(['data:image/', ';base64'])
let type = file.substring(file.indexOf('data:image/')+11, file.indexOf(';base64,'))
let content = file.substring(file.indexOf(';base64,')+8)
let filename = `upload/${Math.random().toString(36).substr(2)}.${type}`
valid.string('file.type', type).is().of(['png', 'jpeg', 'jpg', 'gif'])
service.save({type, content, filename})
ctx.ok(`/${filename}`)
})
module.exports = router
2 changes: 2 additions & 0 deletions src/router/admin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const Router = require('koa-router')
const router = new Router()

const uploadController = require('../controller/bb/uploadController')
router.use('', uploadController.routes(), uploadController.allowedMethods())
const weaponController = require('../controller/bb/weaponController')
router.use('', weaponController.routes(), weaponController.allowedMethods())
const userController = require('../controller/bb/accountController')
Expand Down
22 changes: 22 additions & 0 deletions src/service/uploadService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const failError = require('../error/failError')
const fs = require('fs')
const path = require('path')

module.exports = {
save: async(e) => {
let folderList = e.filename.split('/')
let folder = ''
for(let i=0; i<folderList.length-1; i++) {
folder = path.join(folder, folderList[i])
if (!fs.existsSync(folder)) {
fs.mkdirSync(folder)
}
}
let buffer = Buffer.from(e.content, 'base64')
try {
await fs.promises.writeFile(e.filename, buffer)
} catch(err) {
throw new failError(`write file to ${e.filename}`)
}
},
}
8 changes: 7 additions & 1 deletion src/valid/validString.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,16 @@ class validString {
if ((this.v.length === 24 || this.v.length === 12) && isNaN(parseInt(this.v,16))!== true) throw new paramError(`${this.name}: ${this.showV}, must be mongodb id`)
return this
}
of(list) {
of(list) { // 考虑接受js可变参数
if (!list.includes(this.v)) throw new paramError(`${this.name}: ${this.showV}, must one of ${list}`)
return this
}
include(list) { // 考虑接受js可变参数
for (let e of list) {
if (!this.v.includes(e)) throw new paramError(`${this.name}: ${this.showV}, must include all of ${list}`)
}
return this
}
}

module.exports = validString
2 changes: 1 addition & 1 deletion tests/rest/bbAccount.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const operator = require('../data/operator')

const wait = require('../../src/util/wait')

describe('bb user controller', () => {
xdescribe('bb user controller', () => {
let token
let refreshToken
it('save, delete', async() => {
Expand Down
27 changes: 27 additions & 0 deletions tests/rest/upload.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const {expect} = require('chai')
const fetch = require('../../src/fetch')
const config = require('../../config/env')
const host = `http://localhost:${config.server.port}`
const db = require('../../src/db')
const operator = require('../data/operator')

const wait = require('../../src/util/wait')

describe('upload controller', () => {
let token
it('upload', async() => {
let rest = await fetch(`${host}/bb/upload`, {method: 'post', body: 'file=data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs=', headers: {authorization: token}})
expect(rest.code).to.be.deep.equal('SUCCESS')
})

before(async() => {
await db.drop()
operator.saveAccount()
await wait(50) // 不然有可能数据还没插进去
let rest = await fetch(`${host}/bb/login`, {method: 'post', body: 'username=admin&password=111111'})
token = `Bearer ${rest.data.token}`
})
after(async() => {
// await db.drop()
})
})
Binary file added upload/llib3r5byzr.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added upload/zo3obbfn4id.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 319c4cc

Please sign in to comment.