# Simple Photo Gallery: Archiving
# (c) 2003--2012 Martin Mares <mj@ucw.cz>

package UCW::Gallery::Archive;

use common::sense;

use Archive::Zip;
use File::Spec;
use UCW::CGI;

sub send_archive($$) {
	my ($gal, $meta) = @_;

	if (!$gal->get('WebAllowArchives')) {
		UCW::CGI::http_error('403 Archiving forbidden by server configuration');
		return;
	}

	my $zip = Archive::Zip->new;
	my $cnt = 0;
	for my $id (@{$meta->{sequence}}) {
		$zip->addFile(File::Spec->catfile($gal->get('PhotoDir'), "$id.jpg"), sprintf("%03d.jpg", $cnt)) or die;
		$cnt++;
	}

	print "Content-type: application/zip\n";
	print "Content-Disposition: attachment; filename=gallery.zip\n";
	print "\n";
	$zip->writeToFileHandle(\*STDOUT, 0);
}

42;
