use HTML::TokeParser; # minimalist HTML sanitation/normalization of stories use HTML::Entities; # ditto use File::Path; # to save user uploaded media use Path::Class::File; # ditto use Digest::MD5 qw( md5_hex ); # ditto, for creating well-distributed dir names use Image::Size qw( imgsize ); # capture image dimensions to facilitate auto-display sub create { my $self = shift; my $hashref = shift; my $story = $hashref->{'story'} ; my $ext = $hashref->{'ext'} ; my $path = $hashref->{'path'} ; my $width = $hashref->{'width'}; my $height = $hashref->{'height'} ; my $acctid = $hashref->{'acctid'} ; # Sanitize/normalize filename my $name = lc $path; $name =~ s/[^[:print:]¥W]+/_/g; my $path_seed = $acctid . $name; my $md5 = md5_hex($path_seed); my @dirs = ($md5 =~ /^(...)(...)(...)/); my $path_new = Path::Class::File->new(@dirs, $name); my $file_system_path = Path::Class::File->new(Local::Config::MEDIA_DIR_FILE, $path_new->stringify); print STDERR ("Creating directory at $file_system_path¥n"); eval { mkpath([ $file_system_path->dir ]) }; print STDERR "Opening media file for writing: $file_system_path¥n"; open my $destination, ">", $file_system_path; my $fh = new IO::File($path, "r") or die "could not open $path: $!¥n"; my ( $x, $y ); eval { ( $x, $y ) = imgsize($fh) }; # Might not be an image. while ( <$fh> ) { print $destination $_; } close($destination) or print STDERR ("Could not close $path\n"); my $data_ref = { story => $story, width => $x, height => $y, ext => $ext, path => $path_new->stringify, # Relative path so web server can find it. created => \"NOW()", # " }; #change go ahead and add the data my $schema = Prosper::Base->connect(Local::Config::DNS, Local::Config::DNS_USERNAME, Local::Config::DNS_PASSWORD); my $media = $schema->resultset("Media")->create( $data_ref); #add the data to the table return 1; }