package RSApache::Response::DownloadZipDirectory;

use strict;
use warnings;

use Apache2::Const qw(OK);

use lib ('/app/tools/common/lib');
use Common::Assert;
use Common::Log;
use RSApache::Response;
use base 'RSApache::Response::Download';

use Data::Dumper;

use constant kZipProg => '/usr/bin/zip';

sub _init {
    my ( $self, $directoryPath, $contentType, $fileName ) = @_;

    # Keep a reference to the directory path.
    # We are going to _delete_ this directory afterwards!
    #
    assert( -d $directoryPath, "ERROR - $directoryPath is not a valid path to a directory" );

    $self->{directoryPath} = $directoryPath;

    # Change to that directory, so the compressed file won't have a bunch of extraneous
    # directories in it.
    #
    chdir($directoryPath) || die "ERROR - unable to change directories to $directoryPath: $!";

    # Open a pipe to the zip utility, then pass that long to our base class _init method.
    #
    my $zipCmd = kZipProg . ' -r - *';

    my $fh = new IO::File;
    open( $fh, "$zipCmd |" ) || die "ERROR invoking $zipCmd : $!\n";

    # We can't stat the file handle, since we're reading from a pipe.
    # So, I have no way to know how many bytes we're going to be sending.
    #

    return $self->SUPER::_init( $fh, $contentType, $fileName );
}

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