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