package RSApache::Response::DownloadStream;

use strict;
use warnings;

use Apache2::Const qw(OK);

use lib ('/app/tools/common/lib');
use RSApache::Response;
use base 'RSApache::Response';

use Data::Dumper;

sub _init {
    my ( $self, $data, $contentType, $fileName ) = @_;
    $contentType = 'application/octet-stream' unless $contentType;
    $fileName    = 'RoyaltyShareFileDownload' unless $fileName;

    $self->{data}        = $data;
    $self->{contentType} = $contentType;
    $self->{fileName}    = $fileName;
}

sub respond {
    my ( $self, $apache ) = @_;
    my $size     = length( $self->{data} );
    my $filename = $self->{fileName};

    $apache->headers_out->add( 'Content-Length'      => $size );
    $apache->headers_out->add( 'Content-Disposition' => "attachment; filename=\"$filename\"" );

    $apache->send_http_header( $self->{contentType} );

    binmode STDOUT;
    print $self->{data};

    return OK;
}

###
1;    # Play nicely.
###
