FTP upload images rather than zips (solved)

If you've made a patch to quick fix a bug or to add a new feature not yet in the main tree then post it here so others can try it out.
Post Reply
Posts: 195
Joined: Thu Oct 11, 2007 5:10 am

FTP upload images rather than zips (solved)

Post by haus » 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:

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" )
            $arch_file .= '.tar.gz';
            $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 );
        Info( "Uploading to ".ZM_UPLOAD_FTP_HOST."\n" );
        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->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 );
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.

Posts: 20
Joined: Fri May 29, 2009 9:56 pm

Post by joeinbend » Wed Feb 10, 2010 11:56 pm

Hey Haus, I see this is a very old post, but interestingly enough, this issue is still not fixed in the actual released versions! I backed up my zmfilter.pl and added your modifications to mine, and it works great!

One question: Would it be possible to make the event.html a little more rich, with just maybe a couple options:

1. When you go to event.html initially, if it would do a streaming .jpg like the way ZM does in the regular monitoring view, rather than just the thumbnails. and if so, also just a "thumbs" link on the event.html which would take you to the full page of thumbs, as the event.html currently displays. What do you think?

Second question for you: Is there a way to have it convert to .mpg video when a filter is triggered, upload that video, then notify by email of the event, with a link to the video? I'm probably asking way too much, but hopefully not!

Posts: 15
Joined: Sat Oct 15, 2016 2:35 am

Re: FTP upload images rather than zips (solved)

Post by esprit1st » Sun Oct 16, 2016 8:46 pm

This sounds exactly what I would like to do. Additionally I would like to just upload images every 60 seconds or so. How can I achieve that?

Thank you guys!

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests