Page 1 of 1

Email all images in an event as a zip attachment

Posted: Sun Dec 31, 2006 4:57 pm
by mlilja
Hi,

I have added a "feature" to Zoneminder where an event will be zipped and mailed to my gmail account. I do not have an external FTP server, and wanted to be able have my events stored outside my house, because what is the use of capturing events on a local storage if the PC is stolen?

I would like it if this "feature" could be default, it would at least save me some patching time everytime a new version of Zoneminder is released. I guess other would use the feature also?

Regards,
mlilja

Posted: Sun Jan 14, 2007 5:25 pm
by zoneminder
By all means post the patch here. If it's simple I can include it in the main distro, or it can be added to the Wiki as a patch.

Posted: Mon Jan 15, 2007 9:23 pm
by mlilja
Hmmm, simple... I believe it is, but it could perhaps be made even simpler if the zmpack.pl was merged together with the FTP upload stuff, the two archive create codes are similar.

I could not find any way to attach a file so the patch is below.

If "%EZ%" is given to the filter email message, an attachment is created and attached to the email.

----------------- Patch for 1.22.3 --------------------------

Code: Select all

diff -Naur ZoneMinder-1.22.3/scripts/Makefile.am ZoneMinder-1.22.3_patched/scripts/Makefile.am
--- ZoneMinder-1.22.3/scripts/Makefile.am	2006-11-16 21:54:50.000000000 +0100
+++ ZoneMinder-1.22.3_patched/scripts/Makefile.am	2007-01-15 22:02:56.000000000 +0100
@@ -7,6 +7,7 @@
 	( cd ZoneMinder; perl Makefile.PL )
 
 bin_SCRIPTS = \
+	zmpack.pl \
 	zmdc.pl \
 	zmaudit.pl \
 	zmfilter.pl \
@@ -29,6 +30,7 @@
     ZoneMinder
 
 EXTRA_DIST = \
+	zmpack.pl \
 	zmdc.pl \
 	zmaudit.pl \
 	zmfilter.pl \
diff -Naur ZoneMinder-1.22.3/scripts/Makefile.in ZoneMinder-1.22.3_patched/scripts/Makefile.in
--- ZoneMinder-1.22.3/scripts/Makefile.in	2006-11-29 17:00:13.000000000 +0100
+++ ZoneMinder-1.22.3_patched/scripts/Makefile.in	2007-01-15 22:03:14.000000000 +0100
@@ -166,6 +166,7 @@
 target_alias = @target_alias@
 AUTOMAKE_OPTIONS = gnu
 bin_SCRIPTS = \
+	zmpack.pl \
 	zmdc.pl \
 	zmaudit.pl \
 	zmfilter.pl \
@@ -188,6 +189,7 @@
     ZoneMinder
 
 EXTRA_DIST = \
+	zmpack.pl \
 	zmdc.pl \
 	zmaudit.pl \
 	zmfilter.pl \
diff -Naur ZoneMinder-1.22.3/scripts/zmfilter.pl ZoneMinder-1.22.3_patched/scripts/zmfilter.pl
--- ZoneMinder-1.22.3/scripts/zmfilter.pl	2006-10-23 17:32:19.000000000 +0200
+++ ZoneMinder-1.22.3_patched/scripts/zmfilter.pl	2007-01-15 21:58:55.000000000 +0100
@@ -886,6 +886,21 @@
 			push( @$attachments_ref, { type=>"video/$format", path=>$path } );
 		}
 	}
+	# MLILJA CHANGE.
+        if ( $attachments_ref )
+	{
+	    if ( $text =~ s/%EZ%//g )
+	    {
+		my $command = ZM_PATH_BIN."/zmpack.pl -event $event->{Id} -monitor_name $event->{MonitorName} -monitor_id $event->{MonitorId}";
+		my $output = qx($command);
+		my $status = $? >> 8;
+		if ( $status == 0 )
+		{
+		    chomp $output;
+		    push( @$attachments_ref, { type=>"application/zip", path=>sprintf( "%d/%d/%s", $event->{MonitorId}, $event->{Id}, $output ) } );
+		}
+	    }
+	}
 	$text =~ s/%FN%/$filter->{Name}/g;
 	( my $filter_name = $filter->{Name} ) =~ s/ /+/g;
 	$text =~ s/%FP%/$url?view=filter&mid=$event->{MonitorId}&filter_name=$filter_name/g;
diff -Naur ZoneMinder-1.22.3/scripts/zmpack.pl ZoneMinder-1.22.3_patched/scripts/zmpack.pl
--- ZoneMinder-1.22.3/scripts/zmpack.pl	1970-01-01 01:00:00.000000000 +0100
+++ ZoneMinder-1.22.3_patched/scripts/zmpack.pl	2007-01-15 21:59:46.000000000 +0100
@@ -0,0 +1,95 @@
+#!/usr/bin/perl -wT
+#
+# ==========================================================================
+#
+# ZoneMinder Tar/Zip Creation Script
+#
+
+use Getopt::Long;
+
+use constant ZM_PACK_FORMAT => "zip"; # or "tar"
+use constant ZM_USE_COMPRESSION => 0; # Speedup if we don't and we do not save that much
+use constant ZM_PATH_WEB => "/var/www/localhost/htdocs/zoneminder";
+use constant ZM_DIR_EVENTS => "events";
+
+my $event_id=0;
+my $monitor_name="";
+my $monitor_id=0;
+
+if ( !GetOptions( 'event=i'=>\$event_id, 'monitor_name=s'=>\$monitor_name, 'monitor_id=s'=>\$monitor_id) )
+{
+    print "error in arguments\n";
+    exit (-1);
+}
+
+if ( ZM_PACK_FORMAT eq "zip" )
+{
+    require Archive::Zip;
+    import Archive::Zip qw( :ERROR_CODES :CONSTANTS );
+}
+else
+{
+    require Archive::Tar;
+}
+
+createPackage();
+
+sub createPackage
+{
+    #untaint the variables
+    $monitor_id=~ m|([0-9]*)|;
+    $monitorid=$1;
+    $event_id=~ m|([0-9]*)|;
+    $eventid = $1;
+    my $event = shift;
+    my $arch_file = ZM_PATH_WEB."/".ZM_DIR_EVENTS."/$monitorid/$eventid/captured";
+    my $arch_image_path = ZM_PATH_WEB."/".ZM_DIR_EVENTS."/"."$monitor_id/$event_id/*capture.jpg";
+    my $arch_error;
+
+    if ( ZM_PACK_FORMAT eq "zip" )
+    {
+	$arch_file .= '.zip';
+	my $zip = Archive::Zip->new();
+
+	my $status = &AZ_OK;
+	foreach my $image_file ( glob($arch_image_path) )
+	{
+	    my $member = $zip->addFile( $image_file );
+	    last unless ( $member );
+	    $member->desiredCompressionMethod( (ZM_USE_COMPRESSION)?&COMPRESSION_DEFLATED:&COMPRESSION_STORED );
+	}
+	$status = $zip->writeToFileNamed( $arch_file );
+	if ( $arch_error = ($status != &AZ_OK) )
+	{
+	    print( "Zip error: $status\n " );
+	    exit (-1);
+	}
+	else
+	{
+	    print "captured.zip";
+	}
+    }
+    elsif ( ZM_PACK_FORMAT eq "tar" )
+    {
+	if ( ZM_USE_COMPRESSION )
+	{
+	    $arch_file .= '.tar.gz';
+	}
+	else
+	{
+	    $arch_file .= '.tar';
+	}
+
+	if ( $arch_error = !Archive::Tar->create_archive( $arch_file, ZM_USE_COMPRESSION, glob($arch_image_path) ) )
+	{
+	    print( "Tar error: ".Archive::Tar->error()."\n " );
+	    exit (-1);
+	}
+	else
+	{
+	    print "captured.zip";
+	}
+    }
+}
+
+

[/b]

Posted: Tue Jan 23, 2007 9:56 pm
by zoneminder
Thanks. I have added it to the bugtracker and will try and remember to add it for the next version once I've had a play.

What is the latest on this patch?

Posted: Tue Jun 05, 2007 5:14 pm
by john.a.sutherland
I would like to attach the movie file (avi) also.

Where is this patch, is it posted anywhere?

Thanks,

John

Posted: Tue Jun 05, 2007 9:15 pm
by cordel
Um, It's posted above :roll:

How to apply patch

Posted: Tue Jun 05, 2007 10:02 pm
by john.a.sutherland
OK...ok....I did see the patch above, actually I am just unsure how it works. Do I cut and paste the text to a file, save the file in the ZM install file, then use the patch -1 filename.ext......what next? I have to give the name of the ZM....file.....what file am I patching? Do I need to run configure, make and make install again or what?

Sorry, never patched a file before.

Any help would be appreciated.

Thanks,

John