#!/usr/bin/perl # Author: Peter R. Wood, http://prwdot.org/ # # pPhoto # # Usage: pPhoto -s [-k] [-u -h -l -p -f # # ### ### Modules ### use strict; use Image::Magick; use Net::FTP; use File::Copy; use Getopt::Std; ### ### Constants / Configuration ### my $archive = ""; my $tempdir = ""; my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = localtime(time); $year += 1900; $mon++; my $datestring = "$year/$mon/$mday"; ### ### Main Body ### LogIt("Starting run."); my %args = ( u => 1, k => 0, h => '', l => '', p => '' ); getopts( 'ukrs:h:l:p:f:', \%args ); if ( $args{s} ) { # Get list of files to work with my @filelist = GetFileList( $args{s} ); # Move or Copy those files to the archive ArchiveFiles( $args{k}, $args{s}, $args{f}, @filelist ); # If uploading, scale the files, upload them, and clean out the tempdir if ( $args{u} ) { if ( $args{h} && $args{l} && $args{p} && $args{f} ) { DoMagick( $args{r}, $args{f}, @filelist ); UploadFiles( $args{h}, $args{l}, $args{p}, $args{f}, @filelist ); CleanTemp(@filelist); } else { LogIt( "Sorry, can't upload files without a hostname (-h), login (-l), password (-p), or ftpdir (-f)" ); } } } else { LogIt("Sorry, you must specify a source directory (-s)"); } ### ### Subroutines ### ### GetFileList ### Given a source directory, read in a list of 'jpg' files and return them in an array ### sub GetFileList { my ($sourcedir) = @_; LogIt("Getting file list from $sourcedir."); my @filelist = (); opendir( SOURCE, "$sourcedir" ) || die "Couldn't open $sourcedir: $!"; while ( my $filename = readdir(SOURCE) ) { if ( $filename =~ /\w+\.JPG/i ) { push( @filelist, $filename ); LogIt("Adding $sourcedir/$filename"); } } closedir(SOURCE) || die "Couldn't close $sourcedir: $!"; LogIt("Got file list."); return (@filelist); } ### ArchiveFiles ### Given a source directory, read in a list of 'jpg' files and move or copy them to an archive directory ### sub ArchiveFiles { my ( $keep, $sourcedir, $ftpdir, @filelist ) = @_; LogIt("Archiving files."); mkdir( "$archive/$year", 0755 ) if ( !-d "$archive/$year" ); mkdir( "$archive/$year/$mon", 0755 ) if ( !-d "$archive/$year/$mon" ); mkdir( "$archive/$year/$mon/$mday", 0755 ) if ( !-d "$archive/$year/$mon/$mday" ); mkdir( "$archive/$year/$mon/$mday/$ftpdir", 0755 ) if ( !-d "$archive/$year/$mon/$mday/$ftpdir" ); foreach my $filename (@filelist) { my $from = "$sourcedir/$filename"; my $to = "$archive/$datestring/$ftpdir/$filename"; if ($keep) { copy( "$from", "$to" ) || die "Couldn't copy $from to $to: $!"; LogIt("Copied $from to $to"); } else { copy( "$from", "$to" ) || die "Couldn't copy $from to $to: $!"; unlink("$from") || die "Couldn't remove $from: $!"; LogIt("Moved $from to $to"); } } LogIt("Archived files."); } ### DoMagick ### Given a list of files, resize them and save them to a temporary directory ### sub DoMagick { my ( $resize, $ftpdir, @filelist ) = @_; LogIt("Doing Magick"); foreach my $filename (@filelist) { my $magick = Image::Magick->new; my $x; LogIt("Doing Magick on $archive/$datestring/$ftpdir/$filename"); $x = $magick->read( filename => "$archive/$datestring/$ftpdir/$filename" ); warn "$x" if "$x"; if ($resize) { my $height = $magick->Get('rows'); my $width = $magick->Get('columns'); my $new_height = $height / 3.2; my $new_width = $width / 3.2; $x = $magick->Resize( width => $new_width, height => $new_height ); warn "$x" if "$x"; } $x = $magick->write( filename => "$tempdir/$filename" ); warn "$x" if "$x"; LogIt("Wrote $tempdir/$filename"); } LogIt("Did Magick."); } ### UploadFiles ### Given a list of files, upload them to an FTP host ### sub UploadFiles { my ( $hostname, $login, $password, $ftpdir, @filelist ) = @_; LogIt("Uploading files."); my $ftp = new Net::FTP( $hostname, Passive => 1, Hash => \*STDERR ); $ftp->login( $login, $password ); my $newdir = $ftp->mkdir( "$ftpdir", 1 ); $ftp->cwd($newdir); $ftp->binary; foreach my $filename (@filelist) { my $remote_file = $ftp->put("$tempdir/$filename"); LogIt("Uploaded $remote_file"); } $ftp->quit; LogIt("Uploaded files."); } ### CleanTemp ### Given a list of files, remove them from a temporary directory ### sub CleanTemp { my (@filelist) = @_; LogIt("Cleaning temp dir."); foreach my $filename (@filelist) { unlink("$tempdir/$filename") || die "Couldn't unlink $tempdir/$filename: $!"; } LogIt("Cleaned temp dir."); } ### LogIt ### Given a log message, print it out to STDOUT along with the current localized time ### sub LogIt { my ($message) = @_; my $datetime = scalar localtime(time); print "[$datetime] $message\n"; }