#!/usr/bin/perl
use strict;
use warnings FATAL => qw( all );
use CGI::Carp qw(fatalsToBrowser);
use List::Util qw(sum min max);
use URI::Encode qw(uri_encode);
use lib 'files/lib';
use Base::HTML::Elements qw(table);
# I would add this in, however, it is just too big.
# print_menu was taken out for this example.
# link_color applies color styles to links based on file extension.
# I included link_color below.
use Base::Menu qw(print_menu link_color);
# Gets root data for my site. You can get rid of this.
use Base::Roots qw(get_root);
# Adds commas and rounds numbers.
use Base::Number qw(pretty_number);
use Base::Nifty qw(line);
print "content-type: text/html \n\n";
# change this to the path of whatever directory you want.
my $root_path = get_root('path');
my %extensions;
my %file_sizes;
my $file_sizes_sum;
sub file_list {
my $directory = shift;
opendir(my $dir,$directory) or die "Can't open $directory $!";
my @temp = grep {/^\w/} readdir($dir);
for (@temp) {
if (-f "$directory/$_") {
my $key = (split(/\./,$_))[-1];
++$extensions{$key};
my $file_size = -s "$directory/$_";
my $file = "$directory/$_";
$file =~ s/$root_path\///;
$file_sizes{$file}{bytes} = $file_size;
$file_sizes{$file}{kilobytes} = $file_size/1024;
$file_sizes{$file}{megabytes} = ($file_size/1024)/1024;
$file_sizes_sum += $file_size;
}
if (-d "$directory/$_") {
file_list("$directory/$_");
}
}
}
file_list("$root_path");
my $extensions_sum = sum(values %extensions);
my $extensions_types = keys %extensions;
my $file_sizes_total = keys %file_sizes;
Now, I have all kinds of wonderful data to pass to the table object, if only I know what was needed to get the other objects to work. So, let's start with %extensions and the cells which will go in the more than a dozen rows of the table; then we can go onto %file_sizes.
my @ext_rows;
for my $key (sort keys %extensions) {
my $value = $extensions{$key};
my $color = link_color($key);
# As you can see, the two cells in the rows each have $opt fields.
# The first cell in the row gets a custom color for its text.
# The second cell is part of a CSS class which is right aligned.
push @ext_rows, [[$key, { style=> "$color" }],[$value, { class => 'r
+ight' }]];
}
# Here are the final two rows in the extensions table. Later they will
+ be plugged in
# under whead.
# The first cells in these two rows are headers which get no other spe
+cial formatting.
# The second cells in these two rows get right aligned with a CSS clas
+s.
my @ext_end_rows;
push @ext_end_rows, ['Total files',[$extensions_sum, { class => 'right
+' }]];
push @ext_end_rows, ['Total types',[$extensions_types, { class => 'rig
+ht' }]];
# The table with file paths and file sizes is much much larger.
# In the root directory I use, there are over 2,000 files meaning over
+ 2,000 rows.
my @size_rows;
for my $key (sort { $file_sizes{$b}{bytes} <=> $file_sizes{$a}{bytes}
+|| $a cmp $b } keys %file_sizes) {
my $bytes = $file_sizes{$key}{bytes};
my $kbytes = $file_sizes{$key}{kilobytes};
my $mbytes = $file_sizes{$key}{megabytes};
my $color = link_color($key);
my $link = uri_encode($key);
$key =~ s!&!&!g;
# Again, what do I store here?
# The first cell is a link to the file.
# The second through fourth are numbers which I mapped to be
# pretty and be right aligned with a CSS class.
push @size_rows, [qq(<a href="$link" style="$color">$key</a>),
map { [pretty_number(5,$_), { class => 'right' }] } ($b
+ytes,$kbytes,$mbytes)
];
}
my $sum_bytes = $file_sizes_sum;
my $sum_kbytes = $file_sizes_sum/1024;
my $sum_mbytes = ($file_sizes_sum/1024)/1024;
my $avg_bytes = $file_sizes_sum/$file_sizes_total;
my $avg_kbytes = ($file_sizes_sum/$file_sizes_total)/1024;
my $avg_mbytes = (($file_sizes_sum/$file_sizes_total)/1024)/1024;
# Now these two rows are being stored with the other rows in the sizes
+ table
# with the same formatting.
push @size_rows, ['Totals',map { [pretty_number(5,$_), { class => 'rig
+ht' }] } ($sum_bytes,$sum_kbytes,$sum_mbytes)];
push @size_rows, ['Averages',map { [pretty_number(5,$_), { class => 'r
+ight' }] } ($avg_bytes,$avg_kbytes,$avg_mbytes)];
# With the way the data was gathered above, I was able to plug in the
+data
# into the tables in a one liner (though they wrapped here). I did not
+ store
# the headings, since they did not require any real munging.
table(3, { style => 'float:right', headings => ['Ext','Count'], data =
+> [@ext_rows], whead => [@ext_end_rows] });
table(3, { headings => [qw(File bytes kilobytes megabytes)], whead =>
+[@size_rows] });
So, what do I need to store in arrayrefs for each row for each cell in the row?
link_color
sub link_color {
my ($file,$style) = @_;
my $color = "000";
my %colors;
$colors{pl} = "f00";
$colors{pm} = "900";
$colors{html} = "00f";
$colors{shtml} = "009";
$colors{svg} = "003";
$colors{css} = "060";
$colors{csv} = "0f0";
$colors{txt} = "090";
$colors{zip} = "990";
$colors{js} = "099";
$colors{pdf} = "c33";
$colors{wav} = "939";
$colors{xls} = "696";
$colors{doc} = "669";
$colors{pub} = "699";
$colors{$_} = "909" for (qw(gif ico jpg png bmp));
my ($extension,$name) = reverse split(/\./,$file);
$color = $colors{$extension} ? $colors{$extension} : $color;
return $style ? qq( style="color:#$color") : qq(color:#$color);
}
Have a cookie and a very nice day!
Lady Aleena
|