FTP upload images rather than zips (solved)
Posted: Mon Jun 02, 2008 7:13 pm
So I never really saw the point in uploading zips of events. Zip doesn't further compress JPGs by default, so all you get is a single file that has to be downloaded and uncompressed rather than being viewed in a browser. I want offsite backups that are quick and easy to scan, and by the above they take up no more room than the zips did anyway. So I modified "sub uploadArchFile" in zmfilter.pl a little bit to:
1. Create a directory called <monitor name>-<event Id>
2. Upload the *capture.jpg files into that directory
3. Create a temporary "event.html" file that shows 160x120 thumbnails of all the uploaded jpgs on one page and allows clicking on them to enlarge to the full image, uploads the event.html file into the directory in #1 above, and then erases the event.html file from ZM_UPLOAD_FTP_LOC_DIR.
Then you can have your email alert do something like:
http://www.MYDOMAIN.com/MY_FTP_UPLOAD_F ... event.html
where MY_FTP_UPLOAD_FOLDER requires http basic authentication (at least). Then when you click on the link in the email, you authenticate and go straight to the offsite-backup location and view the group of thumbnails that make up the event.
4. Oh, and I just commented out the code that actually creates the archive (after applying some edits that fixed the "blank zip upload" issue on ubuntu 7.10).
My theory behind this is that for events I generally want to quickly scan what happened, and maybe download a few frames here and there after looking at the equivalent of an online contact sheet, rather than download zips, unpack them, view each image, and save the ones I want.
Here are the changed bits from zmfilter.pl:
Obviously this is a total hack job, but maybe it'll provide some ideas to someone down the road. You can do a *lot* with event.html. The clever could create a little slideshow javascript, or something with next/prev buttons...possibilities are wonderful here.
1. Create a directory called <monitor name>-<event Id>
2. Upload the *capture.jpg files into that directory
3. Create a temporary "event.html" file that shows 160x120 thumbnails of all the uploaded jpgs on one page and allows clicking on them to enlarge to the full image, uploads the event.html file into the directory in #1 above, and then erases the event.html file from ZM_UPLOAD_FTP_LOC_DIR.
Then you can have your email alert do something like:
http://www.MYDOMAIN.com/MY_FTP_UPLOAD_F ... event.html
where MY_FTP_UPLOAD_FOLDER requires http basic authentication (at least). Then when you click on the link in the email, you authenticate and go straight to the offsite-backup location and view the group of thumbnails that make up the event.
4. Oh, and I just commented out the code that actually creates the archive (after applying some edits that fixed the "blank zip upload" issue on ubuntu 7.10).
My theory behind this is that for events I generally want to quickly scan what happened, and maybe download a few frames here and there after looking at the equivalent of an online contact sheet, rather than download zips, unpack them, view each image, and save the ones I want.
Here are the changed bits from zmfilter.pl:
Code: Select all
sub uploadArchFile
{
my $filter = shift;
my $event = shift;
my $ftp_create_dir = $event->{MonitorName} . '-' . $event->{Id};
my $ftp_html_file = ZM_UPLOAD_FTP_LOC_DIR . '/' . "event.html";
my $arch_file = ZM_UPLOAD_FTP_LOC_DIR.'/'.$event->{MonitorName}.'-'.$event->{Id};
my $arch_image_path = getEventPath( $event ) . "/";
#my $arch_image_path = getEventPath( $event )."/".((ZM_UPLOAD_ARCH_ANALYSE)?'{*analyse,*capture}':'*capture').".jpg";
my $arch_error;
if ( ZM_UPLOAD_ARCH_FORMAT eq "zip" )
{
# $arch_file .= '.zip';
# my $zip = Archive::Zip->new();
# Info( "Creating upload file '$arch_file'\n" );
# Info( "Arch_image_path is '<$arch_image_path>'\n" );
my $status = &AZ_OK;
# foreach my $image_file ( <$arch_image_path/*capture.jpg> )
# {
# Info( "Adding $image_file\n" );
# my $member = $zip->addFile( $image_file );
# last unless ( $member );
# $member->desiredCompressionMethod( (ZM_UPLOAD_ARCH_COMPRESS)?&COMPRESSION_DEFLATED:&COMPRESSION_STORED );
# }
# $status = $zip->writeToFileNamed( $arch_file );
# if ( $arch_error = ($status != &AZ_OK) )
# {
# Error( "Zip error: $status\n " );
# }
}
elsif ( ZM_UPLOAD_ARCH_FORMAT eq "tar" )
{
if ( ZM_UPLOAD_ARCH_COMPRESS )
{
$arch_file .= '.tar.gz';
}
else
{
$arch_file .= '.tar';
}
Info( "Creating upload file '$arch_file'\n" );
if ( $arch_error = !Archive::Tar->create_archive( $arch_file, ZM_UPLOAD_ARCH_COMPRESS, <*$arch_image_path> ) )
{
Error( "Tar error: ".Archive::Tar->error()."\n " );
}
}
if ( $arch_error )
{
return( 0 );
}
else
{
Info( "Uploading to ".ZM_UPLOAD_FTP_HOST."\n" );
my $ftp = Net::FTP->new( ZM_UPLOAD_FTP_HOST, Timeout=>ZM_UPLOAD_FTP_TIMEOUT, Passive=>ZM_UPLOAD_FTP_PASSIVE, Debug=>ZM_UPLOAD_FTP_DEBUG );
if ( !$ftp )
{
warn( "Can't create ftp connection: $@" );
return( 0 );
}
$ftp->login( ZM_UPLOAD_FTP_USER, ZM_UPLOAD_FTP_PASS ) or warn( "FTP - Can't login" );
$ftp->binary() or warn( "FTP - Can't go binary" );
$ftp->cwd( ZM_UPLOAD_FTP_REM_DIR ) or warn( "FTP - Can't cwd" );
$ftp->mkdir($ftp_create_dir) or warn( "Cannot create directory $ftp_create_dir");
$ftp->cwd($ftp_create_dir) or warn( "FTP - can't cwd to $ftp_create_dir");
my $event_html_text = '';
foreach my $image_file ( <$arch_image_path/*capture.jpg> ) {
$ftp->put( $image_file ) or warn( "FTP - Can't upload '$image_file'" );
(my $short_image_file = $image_file) =~ s/.*\///;
$event_html_text = $event_html_text . "<a href=\"$short_image_file\"><img src=\"$short_image_file\" width=160 height=120 border=0></a>";
}
open MYHTML, ">> $ftp_html_file" or warn ("Can't create or open HTML file");
print MYHTML "$event_html_text" or die ("Can't print to $ftp_html_file");
close MYHTML;
$ftp->ascii();
$ftp->put( $ftp_html_file );
unlink( $ftp_html_file);
#$ftp->put( $arch_file ) or warn( "FTP - Can't upload '$arch_file'" );
$ftp->quit() or warn( "FTP - Can't quit" );
unlink( $arch_file );
my $sql = "update Events set Uploaded = 1 where Id = ?";
my $sth = $dbh->prepare_cached( $sql ) or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
my $res = $sth->execute( $event->{Id} ) or Fatal( "Can't execute '$sql': ".$sth->errstr() );
}
return( 1 );
}