b5629eb4ab383b799a1e2d12e5abbb3addf24e89
[ikiwiki.git] / IkiWiki / Plugin / amazon_s3.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::amazon_s3;
3
4 use warnings;
5 no warnings 'redefine';
6 use strict;
7 use IkiWiki 2.00;
8 use IkiWiki::Render;
9 use Net::Amazon::S3;
10
11 # Store references to real subs before overriding them.
12 our %subs;
13 BEGIN {
14         foreach my $sub (qw{IkiWiki::writefile IkiWiki::prune}) {
15                 $subs{$sub}=\&$sub;
16         }
17 };
18
19 sub import { #{{{
20         hook(type => "checkconfig", id => "amazon_s3", call => \&checkconfig);
21 } # }}}
22
23 sub checkconfig { #{{{
24         foreach my $field (qw{amazon_s3_key_id amazon_s3_key_file
25                               amazon_s3_bucket}) {
26                 if (! exists $config{$field} || ! defined $config{$field}) {
27                         error(sprintf(gettext("Must specify %s"), $field));
28                 }
29         }
30         if (! exists $config{amazon_s3_prefix} ||
31             ! defined $config{amazon_s3_prefix}) {
32             $config{amazon_s3_prefix}="wiki/";
33         }
34 } #}}}
35
36 {
37 my $bucket;
38 sub getbucket { #{{{
39         return $bucket if defined $bucket;
40         
41         open(IN, "<", $config{amazon_s3_key_file}) || error($config{amazon_s3_key_file}.": ".$!);
42         my $key=<IN>;
43         chomp $key;
44         close IN;
45
46         my $s3=Net::Amazon::S3->new({
47                 aws_access_key_id => $config{amazon_s3_key_id},
48                 aws_secret_access_key => $key,
49                 retry => 1,
50         });
51
52         # make sure the bucket exists
53         if (exists $config{amazon_s3_location}) {
54                 $bucket=$s3->add_bucket({
55                         bucket => $config{amazon_s3_bucket},
56                         location_constraint => $config{amazon_s3_location},
57                 });
58         }
59         else {
60                 $bucket=$s3->add_bucket({
61                         bucket => $config{amazon_s3_bucket},
62                 });
63         }
64
65         if (! $bucket) {
66                 error(gettext("Failed to create bucket in S3: ").
67                         $s3->err.": ".$s3->errstr."\n");
68         }
69
70         return $bucket;
71 } #}}}
72 }
73
74 package IkiWiki;
75 use File::MimeInfo;
76 use Encode;
77
78 # This is a wrapper around the real writefile.
79 sub writefile ($$$;$$) { #{{{
80         my $file=shift;
81         my $destdir=shift;
82         my $content=shift;
83         my $binary=shift;
84         my $writer=shift;
85
86         # First, write the file to disk.
87         my $ret=$IkiWiki::Plugin::amazon_s3::subs{'IkiWiki::writefile'}->($file, $destdir, $content, $binary, $writer);
88
89         # Now, determine if the file was written to the destdir.
90         # writefile might be used for writing files elsewhere.
91         # Also, $destdir might be set to a subdirectory of the destdir.
92         my $key;
93         if ($destdir eq $config{destdir}) {
94                 $key=$file;
95         }
96         elsif ("$destdir/$file" =~ /^\Q$config{destdir}\/\E(.*)/) {
97                 $key=$1;
98         }
99
100         # Store the data in S3.
101         if (defined $key) {
102                 $key=$config{amazon_s3_prefix}.$key;
103                 my $bucket=IkiWiki::Plugin::amazon_s3::getbucket();
104
105                 # The http layer tries to downgrade utf-8
106                 # content, but that can fail (see
107                 # http://rt.cpan.org/Ticket/Display.html?id=35710),
108                 # so force convert it to bytes.
109                 $content=encode_utf8($content) if defined $content;
110
111                 if (defined $content && ! length $content) {
112                         # S3 doesn't allow storing empty files!
113                         $content=" ";
114                 }
115                 
116                 my %opts=(
117                         acl_short => 'public-read',
118                         content_type => mimetype("$destdir/$file"),
119                 );
120                 my $res;
121                 if (! $writer) {
122                         $res=$bucket->add_key($key, $content, \%opts);
123                 }
124                 else {
125                         # read back in the file that the writer emitted
126                         $res=$bucket->add_key_filename($key, "$destdir/$file", \%opts);
127                 }
128                 if ($res && $key=~/(^|\/)index.$config{htmlext}$/) {
129                         # index.html files are a special case. Since S3 is
130                         # not a normal web server, it won't serve up
131                         # foo/index.html when foo/ is requested. So the
132                         # file has to be stored twice. (This is bad news
133                         # when usedirs is enabled!)
134                         $key=~s/index.$config{htmlext}$//;
135                         if (! $writer) {
136                                 $res=$bucket->add_key($key, $content, \%opts);
137                         }
138                         else {
139                                 $res=$bucket->add_key_filename($key, "$destdir/$file", \%opts);
140                         }
141                 }
142                 if (! $res) {
143                         error(gettext("Failed to save file to S3: ").
144                                 $bucket->err.": ".$bucket->errstr."\n");
145                 }
146         }
147
148         return $ret;
149 } #}}}
150
151 # This is a wrapper around the real prune.
152 sub prune ($) { #{{{
153         my $file=shift;
154
155         my $bucket=IkiWiki::Plugin::amazon_s3::getbucket();
156         print STDERR "wrapped prune\n";
157
158         return $IkiWiki::Plugin::amazon_s3::subs{'IkiWiki::writefile'}->($file);
159 } #}}}
160
161 1