#!/usr/bin/env perl use strict; =head1 NAME show-message.pl - display the n latest file in a directory (typically uploaded from a radio beacon system) =head1 VERSION Version 0.0.1 =head1 SYNOPSIS =head1 AUTHOR Alexandre Masselot, C<< >> =head1 ACKNOWLEDGEMENTS =head1 COPYRIGHT & LICENSE Copyright 2006 Alexandre Masselot, all rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut use CGI qw(:standard); use File::Basename; my $query = new CGI; if($query->param('showsource')){ print $query->header(-type=>'text/plain'); undef $/; open (FD, "<$0") or die "cannot open myself [$0]:$!"; print ; close FD; exit(0); } my %properties; my $propertiesFile="$0.properties"; readProperties(); if($query->param('action') eq 'commit'){ print $query->header( -type=>'text/plain', ); my $data=$query->param('data'); my $file=$query->param('file'); unless ($properties{'data.dir'}){ print "FAIL\nno {data.dir} properties"; exit(0); } my $filename=$properties{'data.dir'}."/$file"; unless (open(FD, ">$filename")){ print "FAIO\ncannot open for writing file [$filename]: $!"; exit; } print FD<param('out')) eq 'json'){ print $query->header( -type=>'text/plain', ); die "no data.dir properties" unless $properties{'data.dir'}; my $path=$properties{'data.dir'}."/*"; my @files=glob($path); my %file2data; foreach (@files){ my $root=$_; $root=~s/\.([^\.]+)//; my $ext=$1; $file2data{$root}{mtime}=(stat $_)[9] if (!defined $file2data{$root}{mtime}) or ($file2data{$root}{mtime}>(stat $_)[9]); $file2data{$root}{derived}{$ext}=$_; } use Data::Dumper; print Dumper(\%file2data); exit(0); } #TODO images display print $query->header( -type=>'text/html', ); my %sh=( -title=>$properties{title}||'meteosound', ); $sh{-xbase}=$properties{'base.url'} if $properties{'base.url'}; $sh{-style}={src=>$properties{'css.url'}} if $properties{'css.url'}; if($properties{'icon'}){ push @{$sh{-head}}, Link({ -rel=>"SHORTCUT ICON", -href=>$properties{icon}, }); } print $query->start_html(%sh); if($query->param('showproperties')){ print<properties file in $propertiesFile
EOT
  foreach (sort keys %properties){
    print "$_=$properties{$_}\n";
  }
  print $query->end_html;
  exit(0)
}
if(my $f=$properties{'header.url'}){
  local $/;
  if(open (FD, "<$f")){
    print ;
  }else{
    print "cannot read header.html file [$f]: $!
\n"; } } die "no data.dir properties" unless $properties{'data.dir'}; my $path=$properties{'data.dir'}."/*"; my @files=glob($path); print "

Latest uploaded files

\n"; my %file2mtime; foreach (@files){ $file2mtime{$_}=(stat $_)[9]; } my @sortedFiles=sort {$file2mtime{$b}<=>$file2mtime{$a}} @files; if($query->param('cleanit')){ die "cannot clean if no data.limit propety is set" unless $properties{'data.limit'}; my $limit=$properties{'data.limit'}; my $tot; foreach (@sortedFiles){ $tot+=(stat($_))[7]; if($tot>$limit){ unlink $_; print "removing $_
\n"; } } } @sortedFiles=@sortedFiles[0..($properties{'show.last'}-1)] if($properties{'show.last'}); print< EOT my $refile=$properties{'data.filename.urlregexp'}; foreach(@sortedFiles){ next unless defined $_; my $f=$_; eval "$refile" if $refile; my $urlencode=$_; $urlencode=~s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg; my $ext=$_; $ext=~s/.*\.//; next if $ext=~/^(txt)$/i; print " .$ext".localtime($file2mtime{$f})."(".(int((stat $f)[7]/1000))."kb)\n";#\n"; } print<

View properties here
View source code here

EOT print $query->end_html; sub readProperties{ open(FD, "<$propertiesFile") or die "cannot open for reading $propertiesFile: $!"; while(){ chomp; s/\#.*//; next unless /([\w\.]+)=(.*?)\s*$/; $properties{$1}=$2; } }