Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
add router and config
- Loading branch information
andycall
committed
Feb 20, 2017
1 parent
8baf729
commit 39c4e20
Showing
4 changed files
with
107 additions
and
58 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
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,39 @@ | ||
import fs from 'fs' | ||
import parse from 'co-body' | ||
import path from 'path' | ||
import crypto from 'crypto' | ||
|
||
const receiveRootDir = path.join(__dirname, 'receive'); | ||
|
||
function toMd5(buffer) { | ||
let md5 = crypto.createHash('md5'); | ||
md5.update(buffer); | ||
return md5.digest('hex'); | ||
} | ||
|
||
module.exports = function* () { | ||
var body = yield parse(this, { | ||
limit: '1kb' | ||
}); | ||
|
||
let filename = body.filename; | ||
let fileDir = path.join(receiveRootDir, filename); | ||
|
||
let totalBuffer = Buffer.alloc(0); | ||
let dirInfo = fs.readdirSync(fileDir); | ||
|
||
dirInfo = dirInfo.sort(function(pre, next) { | ||
return pre - next; | ||
}); | ||
|
||
dirInfo.forEach(name => { | ||
let chunkPath = path.join(fileDir, name); | ||
let buffer = fs.readFileSync(chunkPath); | ||
totalBuffer = Buffer.concat([totalBuffer, buffer], totalBuffer.length + buffer.length); | ||
}) | ||
|
||
this.body = { | ||
error: 0, | ||
data: toMd5(totalBuffer) | ||
} | ||
} |
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,56 @@ | ||
import parse from 'co-busboy' | ||
import path from 'path' | ||
import fs from 'fs' | ||
|
||
const outputDir = path.join(__dirname, 'receive'); | ||
|
||
module.exports = function* () { | ||
// ignore non-POSTs | ||
if ('POST' != this.method) return yield next; | ||
|
||
// multipart upload | ||
var parts = parse(this); | ||
var part; | ||
|
||
while ((part = yield parts)) { | ||
if (part.constructor.name === 'FileStream') { | ||
let filename = part.filename; | ||
let chunkIndex, prefix; | ||
|
||
if (filename.indexOf('_IDSPLIT_') > 0) { | ||
prefix = filename.split('_IDSPLIT_')[0]; | ||
chunkIndex = filename.split('_IDSPLIT_')[1]; | ||
|
||
// // // 错误处理测试使用 | ||
if (chunkIndex == 1) { | ||
// console.log(`set failed of ${chunkIndex}`); | ||
// this.status = 500; | ||
} | ||
} | ||
else { | ||
prefix = filename; | ||
chunkIndex = '0'; | ||
} | ||
|
||
let fileDir = path.join(outputDir, prefix) | ||
|
||
if (!fs.existsSync(fileDir)) { | ||
fs.mkdirSync(fileDir); | ||
} | ||
|
||
var stream = fs.createWriteStream(path.join(fileDir, chunkIndex)); | ||
part.pipe(stream); | ||
console.log('uploading %s -> %s', part.filename, stream.path); | ||
} | ||
else { | ||
let param = part[0]; | ||
let value = part[1]; | ||
console.log(`${param} : ${value}`); | ||
} | ||
} | ||
|
||
this.body = { | ||
error: 0, | ||
errmsg: 'ok' | ||
} | ||
} |