-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
chengweiou
committed
Jan 23, 2019
1 parent
1ec3782
commit 319c4cc
Showing
8 changed files
with
76 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`) | ||
} | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.