package Labyrinth::Media; use warnings; use strict; use vars qw($VERSION @ISA %EXPORT_TAGS @EXPORT @EXPORT_OK); $VERSION = '5.13'; =head1 NAME Labyrinth::Media - Media File Management for Labyrinth =head1 DESCRIPTION This module collates many media and image file handling functionality used within Labyrinth. It should be noted that internally images and media files are stored in the same, although images also record dimensions. When retrieving the required files, it is recommend you call the appropriate method to ensure you are getting the correct format of data for the file format. For example, GetImage and GetMedia, both return file information, but GetImage adds deminsion data. Also note that Images and Photos differ in the directory structure storage, so saving and copying need to reference different functions. See below for a more detailed explanation. =cut # ------------------------------------- # Export Details require Exporter; @ISA = qw(Exporter); %EXPORT_TAGS = ( 'all' => [ qw( CGIFile StockSelect StockName StockPath StockType PathMove GetImage SaveImageFile MirrorImageFile CopyPhotoFile SavePhotoFile GetMedia SaveMedia SaveFile UnZipFile GetImageSize GetGravatar ) ] ); @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); @EXPORT = ( @{ $EXPORT_TAGS{'all'} } ); # ------------------------------------- # Library Modules use Archive::Extract; use Digest::MD5 qw(md5_hex); use File::Basename; use File::Copy; use File::Path; use File::Slurp; use Image::Size; use URI::Escape qw(uri_escape); use WWW::Mechanize; use Labyrinth::Audit; use Labyrinth::Globals qw(:default); use Labyrinth::DBUtils; use Labyrinth::DIUtils; use Labyrinth::Metadata; use Labyrinth::MLUtils; use Labyrinth::Plugins; use Labyrinth::Support; use Labyrinth::Variables; # ------------------------------------- # Constants use constant MaxDefaultImageWidth => 120; use constant MaxDefaultImageHeight => 120; use constant MaxDefaultThumbWidth => 120; use constant MaxDefaultThumbHeight => 120; # ------------------------------------- # Variables { # START Stock Control my @CHARS = ( qw/A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9 _ /); my %stock; # ------------------------------------- # The Functions =head1 PUBLIC INTERFACE FUNCTIONS =head2 Stock Control Functions =over =item CGIFile When uploading a file via a web form, this function will save the file to the local filesystem. =back =cut sub CGIFile { my $param = shift; my $stock = shift || 1; _init_stock() unless(%stock); $stock = 1 unless($stock{$stock}); my $path = "$settings{webdir}/$stock{$stock}->{path}"; mkpath($path); my $f = $cgi->upload($param) || die "Cannot access filehandle\n"; my ($name,$suffix) = ($f =~ m!([^/\\]*)(\.\w+)$!); my $filename; my $tries = 0; while(1) { last if($tries++ > 10); $filename = "$path/" . _randname('imgXXXXXX') . lc($suffix); next if(-f $filename); last; } my $buffer = read_file($f, binmode => ':raw'); my $bytes = length($buffer); write_file($filename, { binmode => ':raw' }, $buffer); if($bytes == 0) { LogError("CGIFile: no bytes read for input file [$param]"); return; } $filename =~ s!^$settings{webdir}/!!; return ($name,$filename,$suffix); } =head2 Stock Control Functions The stock list relates to the directory paths where uploaded files should be saved on the local filesystem. =over =item StockName Return the name for the given stock id. =item StockPath Return the path for the given stock id. =item StockType Return the stock id for the given stock code. =item StockSelect Returns an XHTML snippet for a dropdown selection box of stock entries. =item PathMove =back =cut sub StockName { my $stock = shift || 1; _init_stock() unless(%stock); return $stock{$stock}->{title}; } sub StockPath { my $stock = shift || 1; _init_stock() unless(%stock); return $stock{$stock}->{path}; } sub StockType { my $stock = shift || 'DRAFT'; _init_stock() unless(%stock); for(keys %stock) { return $_ if($stock{$_}->{title} eq $stock); } return 1; # default } sub StockSelect { my $opt = shift || 0; my $blank = shift || 1; _init_stock() unless(%stock); my $html = "