]> Untitled Git - public.git/blob - public
Added initial script. Creates public symlinks to my private files.
[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;
16
17         my @chars = ( 'a' .. 'z', 'A' .. 'Z', '0' .. '9' );
18         my @path = ();
19         push @path, $chars[ rand @chars ] for 1 .. 10;
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
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/, 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();
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 }