Created
January 7, 2013 06:21
-
-
Save erhangundogan/4472906 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| /* | |
| * File Operations | |
| */ | |
| var fs = require("fs"), | |
| path = require("path"), | |
| util = require("util"); | |
| exports.read = function(req, res) { | |
| var fileName = req.params.filename; | |
| if (fileName) { | |
| var resourcePath = path.resolve(__dirname, "..", fileName); | |
| path.exists(resourcePath, function(exists) { | |
| if (exists) { | |
| res.header("Content-Type", "image/jpeg"); | |
| return fs.createReadStream(resourcePath).pipe(res); | |
| } else { | |
| return res.end(); | |
| } | |
| }); | |
| } | |
| }; | |
| exports.read2 = function(req, res) { | |
| var fileName = req.params.filename; | |
| if (fileName) { | |
| var resourcePath = path.resolve(__dirname, "..", fileName); | |
| path.exists(resourcePath, function(exists) { | |
| if (exists) { | |
| var fileStats = fs.statSync(resourcePath), | |
| readStream = fs.createReadStream(resourcePath); | |
| res.header("Content-Type", "image/jpeg"); | |
| res.header("Content-Length", fileStats.size); | |
| // old style | |
| readStream.on("data", function(data) { | |
| res.write(data); | |
| }); | |
| readStream.on("end", function() { | |
| res.end(); | |
| }); | |
| } else { | |
| return res.end(); | |
| } | |
| }); | |
| } | |
| }; | |
| exports.read3 = function(req, res) { | |
| var fileName = req.params.filename; | |
| if (fileName) { | |
| var resourcePath = path.resolve(__dirname, "..", fileName); | |
| path.exists(resourcePath, function(exists) { | |
| if (exists) { | |
| var fileStats = fs.statSync(resourcePath), | |
| readStream = fs.createReadStream(resourcePath); | |
| res.header("Content-Type", "image/jpeg"); | |
| res.header("Content-Length", fileStats.size); | |
| // util.pump | |
| util.pump(readStream, res); | |
| } else { | |
| return res.end(); | |
| } | |
| }); | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment