]> git.openstreetmap.org Git - chef.git/blob - cookbooks/web/templates/default/cleanup-assets.erb
Redirect all names to canonical site
[chef.git] / cookbooks / web / templates / default / cleanup-assets.erb
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Errno;
7 use IO::Dir;
8 use IO::File;
9 use JSON::XS;
10
11 my $root = shift @ARGV;
12
13 my $manifest;
14
15 if (my $file = IO::File->new("${root}/tmp/manifest.json", "r"))
16 {
17     my $manifest = decode_json(join("\n", $file->getlines));
18
19     $file->close;
20
21     expire_assets($manifest, "${root}/public/assets", "");
22 }
23 else
24 {
25     die "Can't open ${root}/tmp/manifest.json: $!" unless $!{ENOENT};
26 }
27
28 exit 0;
29
30 sub expire_assets
31 {
32     my $manifest = shift;
33     my $assetroot = shift;
34     my $prefix = shift;
35
36     if (my $dir = IO::Dir->new("${assetroot}/${prefix}"))
37     {
38         $prefix = "${prefix}/" unless $prefix eq "";
39
40         while (my $name = $dir->read)
41         {
42             next if $name eq "." || $name eq "..";
43
44             my $path = $name = "${prefix}${name}";
45
46             $name =~ s/\.gz$//;
47
48             if (-d "${assetroot}/${path}")
49             {
50                 expire_assets($manifest, $assetroot, $path);
51             }
52             elsif (exists($manifest->{files}->{$name}))
53             {
54                 # Still a live asset (with checksum)
55             }
56             elsif (exists($manifest->{assets}->{$name}))
57             {
58                 # Still a live asset (without checksum)
59             }
60             elsif (-A "${assetroot}/${path}" > 28)
61             {
62                 unlink("${assetroot}/${path}") || die "Can't remove ${assetroot}/${path}: $!";
63             }
64         }
65
66         $dir->close;
67     }
68     else
69     {
70         die "Can't read ${assetroot}/${prefix}: $!";
71     }
72
73     return;
74 }