Created
June 12, 2012 20:07
-
-
Save mnem/2919832 to your computer and use it in GitHub Desktop.
Hacked file.js to work around node_redis breaking binary data.
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
// Read a file from disk, store it in Redis, then read it back from Redis. | |
var redis = require("redis"), | |
client = redis.createClient(), | |
fs = require("fs"), | |
filename = "kids_in_cart.jpg"; | |
// Get the file I use for testing like this: | |
// curl http://ranney.com/kids_in_cart.jpg -o kids_in_cart.jpg | |
// or just use your own file. | |
// Read a file from fs, store it in Redis, get it back from Redis, write it back to fs. | |
fs.readFile(filename, function (err, data) { | |
if (err) throw err | |
console.log("Read " + data.length + " bytes from filesystem."); | |
//// Encode the binary data as base64 | |
var encoded = data.toString('base64'); | |
client.set(filename, encoded, redis.print); // set entire file | |
client.get(filename, function (err, reply) { // get entire file | |
if (err) { | |
console.log("Get error: " + err); | |
} else { | |
//// Decode the base64 data | |
var decoded = new Buffer(reply, 'base64'); | |
fs.writeFile("duplicate_" + filename, decoded, function (err) { | |
if (err) { | |
console.log("Error on write: " + err) | |
} else { | |
console.log("File written."); | |
} | |
client.end(); | |
}); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment