]> Untitled Git - public.git/blob - public
Added image version
[public.git] / public
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4
5 use File::Spec;
6 use File::Path qw/ make_path /;
7
8 my $root_dir = $ENV{PUBLIC_ROOT_DIR}
9         or die "Need PUBLIC_ROOT_DIR\n";
10 my $link_dir = $ENV{PUBLIC_LINK_DIR}
11         or die "Need PUBLIC_LINK_DIR\n";
12 my $http_uri = $ENV{PUBLIC_HTTP_URI}
13         or die "Need PUBLIC_HTTP_URI\n";
14
15
16 sub nonce {
17         my $n = shift;
18         $n = 10 unless $n > 0;
19
20         my @chars = ( 'a' .. 'z', 'A' .. 'Z', '0' .. '9' );
21         my @path = ();
22         push @path, $chars[ rand @chars ] for 1 .. $n;
23
24         return join '', @path;
25 }
26
27 sub makeNonceDir {
28         my $nonce = shift;
29         my $path  = File::Spec->catdir($link_dir, $nonce);
30         make_path($path, { chmod => 0777 });
31         return $path, ;
32 }
33
34 sub addFiles {
35         # get list of files matching name from root directory
36         my $name = shift
37                 or die "Need filename!\n";
38
39         opendir DIR, $root_dir
40         or die $!;
41         my @files = grep /$name/i, readdir(DIR);
42         closedir DIR;
43
44         # exit if no files found
45         die "No match: $name\n" unless @files;
46
47         # create nonce dir and add symbolic links
48         my $nonce = nonce(20);
49         my $nonce_dir = makeNonceDir($nonce);
50
51         for my $file (@files) {
52                 my $root_filepath = File::Spec->catfile($root_dir,  $file);
53                 my $link_filepath = File::Spec->catfile($nonce_dir, $file);
54
55                 symlink $root_filepath, $link_filepath
56                         or die "Unable to create symlink: $root_filepath -> $link_filepath";
57
58                 my $uri_link = join '/', $http_uri, $nonce, $file;
59                 print "$uri_link\n";
60         }
61 }
62
63 sub listFiles {
64         opendir DIR, $link_dir;
65         my @nonces = readdir(DIR);
66         closedir DIR;
67
68         for my $nonce (@nonces) {
69                 next if $nonce eq '.';
70                 next if $nonce eq '..';
71
72                 # ensure it's a directory
73                 my $nonce_dir = File::Spec->catdir($link_dir, $nonce);
74                 next unless -d $nonce_dir;
75
76                 # make URIs for all the files in the nonce dirs
77                 opendir DIR, $nonce_dir;
78                 my @files = readdir(DIR);
79                 closedir DIR;
80
81                 for my $file (@files) {
82                         next if $file eq '.';
83                         next if $file eq '..';
84
85                         my $uri_link = join '/', $http_uri, $nonce, $file;
86                         print "$uri_link\n";
87                 }
88         }
89 }
90
91
92 # script begins
93 if (@ARGV) {
94         addFiles($_) for @ARGV;
95 }
96 else {
97         listFiles;
98 }