#------------------------------------------------------------
# Copyright (C) 2009 RoyaltyShare, Inc.   All Rights Reserved
#------------------------------------------------------------
package Support::SaleImporter::Command::Download;
use strict;
use warnings;

use IO::Handle;
use File::Basename;

use lib '/app/tools/support/lib';
use Support::SaleImporter::SampleList::RPS;
use Support::SaleImporter::SampleList::BookPub;

use lib '/app/tools/common/lib';
use RSApache::Response::Download;

use base 'RPS::Command';

sub cmd  { return 'download_sample'; }
sub area { return 'support'; }

sub execute {
    my ($self) = @_;

    my $type = $self->getParam('type');
    my $dir;

    if ( $type && $type eq 'bookpub' ) {
        $dir = Support::SaleImporter::SampleList::BookPub->repository();
    } else {
        $dir = Support::SaleImporter::SampleList::RPS->repository();
    }

    my $path = $dir . "/" . $self->getParam('file');
    my $file = basename($path);

    my $fh = new IO::Handle;
    open( $fh, "$path" ) or die "ERROR: Unable to open $path for reading: $!";

    my $contentType = 'application/octet-stream';

    my $response = RSApache::Response::Download->new( $fh, $contentType, $file );
    return $response;
}

1;
