File planting

This commit is contained in:
斟酌 鵬兄 2015-05-26 17:02:42 +08:00
parent efd618845c
commit 53cd9f5912

View File

@ -112,8 +112,8 @@ Framework.prototype.parseResult = function()
Framework.prototype.plantResult = function() Framework.prototype.plantResult = function()
{ {
if( !this.planted ) if( this.planted ) return;
{
this.planted = true; this.planted = true;
if( this.HTTP ) if( this.HTTP )
{ {
@ -126,8 +126,34 @@ Framework.prototype.plantResult = function()
this.HTTP.response.write( this.result ); this.HTTP.response.write( this.result );
this.HTTP.response.end(); this.HTTP.response.end();
} }
};
// This won't handle path exists
// throwing an error is better than handling it
// Need to handle it somewhere else
Framework.prototype.plantFile = function( path, name )
{
if( this.planted ) return;
var _self = this;
this.planted = true;
var resp = this.HTTP.response;
if( !name )
{
var p = require( "path" );
name = p.basename( path );
} }
resp.headers[ "Content-Disposition" ] = "attachment; filename=\"" + name + "\"";
var fs = require( "fs" );
Dragonfly.Debug( "Stream out: " + path );
var rs = fs.createReadStream( path );
rs.addListener( "data", ( x ) => resp.write( x ) );
rs.addListener( "end", () => resp.end() );
}; };
module.exports = Framework; module.exports = Framework;