Yes,
and i found out that file size differs when i retrieve the binary data
from the cache.
I'm using "connect-form" to parse the file upload using readable
stream:
app.post('/upload', function (req, res, next) {
if (req.form) {
req.form.complete(function (err, fields, files){
if(err) console.log(err);
var key = generateId();
res.writeHead(200, {
"X-Key-Received": key
});
console.log(files.file.size);
var mimetype = mime.lookup(files.file.path);
// from
http://blog.andrewvc.com/node-js-and-binary-data
var chunkSize = 64 * 1024;
var bufSize = files.file.size;
var bufPos = 0;
var buf = new Buffer(bufSize);
fs.createReadStream(files.file.path,{
'flags': 'r',
'encoding':'binary',
'mode': 0666,
'bufferSize': chunkSize})
.addListener("data", function(chunk){
var bufNextPos = bufPos + chunk.length;
if (bufNextPos == bufSize) {
buf.write(chunk,'binary',bufPos);
res.write(buf);
bufPos = 0;
} else {
buf.write(chunk,'binary',bufPos);
bufPos = bufNextPos;
}
})
.addListener("close",function() {
if (bufPos != 0) {
console.log("bufPos!=0");
} else {
client.hset(key, "type", mimetype, redis.print);
client.hset(key, "data", buf, redis.print);
console.log(buf.length);
res.end();
}
});
});
}
});
and to retrieve images:
app.get('/get/:key', function (req, res) {
client.hget(req.params.key, "type", function (err, type) {
if(type != null){
client.hget(req.params.key, "data", function (err, data) {
if(err) {
console.log("ERROR");
} else {
res.writeHead(200, {
'Content-Type': type,
'Content-Length': data.length
});
res.end(data, 'binary');
console.log(data.length);
}
});
} else {
res.writeHead(404, {});
res.end("File Not Found!");
}
});
});
but data.length differs between uploading and serving.
What's happening when i retrieve data?i build the response too soon?