use File::Spec;
use File::Path qw/ make_path /;
-my $root_dir = '/mnt/Shared/Personal/My art';
-my $link_dir = '/var/www/html/public';
-my $http_uri = 'http://136.63.171.222/public';
+my $root_dir = $ENV{PUBLIC_ROOT_DIR}
+ or die "Need PUBLIC_ROOT_DIR\n";
+my $link_dir = $ENV{PUBLIC_LINK_DIR}
+ or die "Need PUBLIC_LINK_DIR\n";
+my $http_uri = $ENV{PUBLIC_HTTP_URI}
+ or die "Need PUBLIC_HTTP_URI\n";
sub nonce {
my $n = shift;
- $n = 10 unless $n;
+ $n = 10 unless $n > 0;
my @chars = ( 'a' .. 'z', 'A' .. 'Z', '0' .. '9' );
my @path = ();
- push @path, $chars[ rand @chars ] for 1 .. 10;
+ push @path, $chars[ rand @chars ] for 1 .. $n;
return join '', @path;
}
return $path, ;
}
+sub addFiles {
+ # get list of files matching name from root directory
+ my $name = shift
+ or die "Need filename!\n";
-# get list of files matching name from root directory
-my $name = shift
- or die "Need filename!\n";
+ opendir DIR, $root_dir
+ or die $!;
+ my @files = grep /$name/i, readdir(DIR);
+ closedir DIR;
-opendir DIR, $root_dir;
-my @files = grep /$name/, readdir(DIR);
-closedir DIR;
+ # exit if no files found
+ die "No match: $name\n" unless @files;
-# exit if no files found
-die "No match: $name\n" unless @files;
+ # create nonce dir and add symbolic links
+ my $nonce = nonce(20);
+ my $nonce_dir = makeNonceDir($nonce);
-# create nonce dir and add symbolic links
-my $nonce = nonce();
-my $nonce_dir = makeNonceDir($nonce);
+ for my $file (@files) {
+ my $root_filepath = File::Spec->catfile($root_dir, $file);
+ my $link_filepath = File::Spec->catfile($nonce_dir, $file);
-for my $file (@files) {
- my $root_filepath = File::Spec->catfile($root_dir, $file);
- my $link_filepath = File::Spec->catfile($nonce_dir, $file);
+ symlink $root_filepath, $link_filepath
+ or die "Unable to create symlink: $root_filepath -> $link_filepath";
- symlink $root_filepath, $link_filepath
- or die "Unable to create symlink: $root_filepath -> $link_filepath";
+ my $uri_link = join '/', $http_uri, $nonce, $file;
+ print "$uri_link\n";
+ }
+}
+
+sub listFiles {
+ opendir DIR, $link_dir;
+ my @nonces = readdir(DIR);
+ closedir DIR;
+
+ for my $nonce (@nonces) {
+ next if $nonce eq '.';
+ next if $nonce eq '..';
+
+ # ensure it's a directory
+ my $nonce_dir = File::Spec->catdir($link_dir, $nonce);
+ next unless -d $nonce_dir;
+
+ # make URIs for all the files in the nonce dirs
+ opendir DIR, $nonce_dir;
+ my @files = readdir(DIR);
+ closedir DIR;
- my $uri_link = join '/', ($http_uri, $nonce, $file);
- print "$uri_link\n";
+ for my $file (@files) {
+ next if $file eq '.';
+ next if $file eq '..';
+
+ my $uri_link = join '/', $http_uri, $nonce, $file;
+ print "$uri_link\n";
+ }
+ }
+}
+
+
+# script begins
+if (@ARGV) {
+ addFiles($_) for @ARGV;
+}
+else {
+ listFiles;
}