Created
February 12, 2014 06:28
-
-
Save nbqx/8950898 to your computer and use it in GitHub Desktop.
koa + leveldbでgyazoみたいやつ
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
var fs = require('fs'), | |
crypto = require('crypto'); | |
var through = require('through'), | |
base64 = require('base64-stream'), | |
mime = require('mime'), | |
koa = require('koa'), | |
router = require('koa-router'), | |
parse = require('co-busboy'), | |
level = require('level'), | |
wrap = require('co-level'); | |
// levelDB // | |
var dbPath = __dirname+'/my-db'; | |
var db = level(dbPath); | |
// KoaApp // | |
var app = koa(); | |
app.use(router(app)); | |
// util // | |
function randomToken(){ | |
var token = crypto.randomBytes(8); | |
return token.toString('hex'); | |
}; | |
// middlewares // | |
function *generateId(next){ | |
this.uid = randomToken(); | |
yield next; | |
}; | |
function *getImgData(next){ | |
this.uid = this.params.id; | |
this.type = mime.lookup(this.uid); | |
this.img_data = yield wrap(db).get(this.uid); | |
yield next; | |
}; | |
function *putImgData(next){ | |
var part; | |
var saveHere = function(id){ | |
var uid = id; | |
var bf = []; | |
return through(function write(data){ | |
var encoded = data.toString(); | |
bf.push(encoded); | |
this.queue(data); | |
},function end(){ | |
var self = this; | |
db.put(uid,bf.join(''),function(){ | |
self.queue(null); | |
}); | |
}); | |
}; | |
var parts = parse(this); | |
while(part = yield parts){ | |
var ext = require('path').extname(part.filename); | |
this.key = this.uid+ext; | |
part.pipe(base64.encode()).pipe(saveHere(this.key)); | |
}; | |
yield next; | |
}; | |
function *removeImgData(next){ | |
this.uid = this.params.id; | |
yield (wrap(db)).del(this.uid); | |
yield next; | |
}; | |
// routing // | |
app.get('/',function *(next){ | |
var self = this; | |
db.createKeyStream().on('data',function(data){ | |
console.log('http://'+self.header.host+'/'+data); | |
}); | |
this.body = 'hello world'; | |
}); | |
// show image // | |
// curl 'http://localost:3000/<uid>' | |
app.get( | |
'/:id', | |
getImgData, | |
function *(next){ | |
var res = new Buffer(this.img_data,'base64'); | |
this.body = res; | |
} | |
); | |
// post image // | |
// curl -XPOST -F image=@test.png http://localhost:3000/ | |
app.post( | |
'/', | |
generateId, | |
putImgData, | |
function *(next){ | |
this.body = "http://"+this.header.host+'/'+this.key+"\n"; | |
} | |
); | |
// delete image // | |
// curl -XDELETE 'http://localost:3000/<uid>' | |
app.del( | |
'/:id', | |
removeImgData, | |
function *(next){ | |
this.body = 'ok'; | |
} | |
); | |
app.listen(3000); | |
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
{ | |
"name": "ngy", | |
"version": "0.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"start": "node --harmony index.js", | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "nbqx", | |
"license": "ISC", | |
"dependencies": { | |
"koa": "~0.4.0", | |
"koa-router": "~3.0.1", | |
"co-busboy": "~0.1.1", | |
"co-level": "~1.0.1", | |
"level": "~0.18.0", | |
"base64-stream": "~0.1.2", | |
"through": "~2.3.4", | |
"mime": "~1.2.11" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment