]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blobdiff - IkiWiki/Plugin/amazon_s3.pm
git: Add test coverage for reverting attachments
[git.ikiwiki.info.git] / IkiWiki / Plugin / amazon_s3.pm
index 9cb74fb1e805e14dfc5fae71cfd1dd694655aea5..a9da6bf12b69dc62f2ee05f22aaaef2c88fe7dde 100644 (file)
@@ -4,7 +4,7 @@ package IkiWiki::Plugin::amazon_s3;
 use warnings;
 no warnings 'redefine';
 use strict;
 use warnings;
 no warnings 'redefine';
 use strict;
-use IkiWiki 2.00;
+use IkiWiki 3.00;
 use IkiWiki::Render;
 use Net::Amazon::S3;
 
 use IkiWiki::Render;
 use Net::Amazon::S3;
 
@@ -16,13 +16,13 @@ BEGIN {
        }
 };
 
        }
 };
 
-sub import { #{{{
+sub import {
        hook(type => "getopt", id => "amazon_s3", call => \&getopt);
        hook(type => "getsetup", id => "amazon_s3", call => \&getsetup);
        hook(type => "checkconfig", id => "amazon_s3", call => \&checkconfig);
        hook(type => "getopt", id => "amazon_s3", call => \&getopt);
        hook(type => "getsetup", id => "amazon_s3", call => \&getsetup);
        hook(type => "checkconfig", id => "amazon_s3", call => \&checkconfig);
-} # }}}
+}
 
 
-sub getopt () { #{{{
+sub getopt () {
         eval q{use Getopt::Long};
         error($@) if $@;
         Getopt::Long::Configure('pass_through');
         eval q{use Getopt::Long};
         error($@) if $@;
         Getopt::Long::Configure('pass_through');
@@ -38,27 +38,30 @@ sub getopt () { #{{{
                debug(gettext("done"));
                exit(0);
        });
                debug(gettext("done"));
                exit(0);
        });
-} #}}}
+}
 
 
-sub getsetup () { #{{{
+sub getsetup () {
        return
        return
-                amazon_s3_key_id => {
+               plugin => {
+                       safe => 0,
+                       rebuild => 0,
+               },
+               amazon_s3_key_id => {
                        type => "string",
                        type => "string",
-                       default => "",
+                       example => "XXXXXXXXXXXXXXXXXXXX",
                        description => "public access key id",
                        safe => 1,
                        rebuild => 0,
                },
                amazon_s3_key_id => {
                        type => "string",
                        description => "public access key id",
                        safe => 1,
                        rebuild => 0,
                },
                amazon_s3_key_id => {
                        type => "string",
-                       default => "",
-                       description => "file holding secret key",
+                       example => "$ENV{HOME}/.s3_key",
+                       description => "file holding secret key (must not be readable by others!)",
                        safe => 0, # ikiwiki reads this file
                        rebuild => 0,
                },
                amazon_s3_bucket => {
                        type => "string",
                        safe => 0, # ikiwiki reads this file
                        rebuild => 0,
                },
                amazon_s3_bucket => {
                        type => "string",
-                       default => "",
                        example => "mywiki",
                        description => "globally unique name of bucket to store wiki in",
                        safe => 1,
                        example => "mywiki",
                        description => "globally unique name of bucket to store wiki in",
                        safe => 1,
@@ -66,14 +69,13 @@ sub getsetup () { #{{{
                },
                amazon_s3_prefix => {
                        type => "string",
                },
                amazon_s3_prefix => {
                        type => "string",
-                       default => "wiki/",
+                       example => "wiki/",
                        description => "a prefix to prepend to each page name",
                        safe => 1,
                        rebuild => 1,
                },
                amazon_s3_location => {
                        type => "string",
                        description => "a prefix to prepend to each page name",
                        safe => 1,
                        rebuild => 1,
                },
                amazon_s3_location => {
                        type => "string",
-                       default => "",
                        example => "EU",
                        description => "which S3 datacenter to use (leave blank for default)",
                        safe => 1,
                        example => "EU",
                        description => "which S3 datacenter to use (leave blank for default)",
                        safe => 1,
@@ -81,14 +83,14 @@ sub getsetup () { #{{{
                },
                amazon_s3_dupindex => {
                        type => "boolean",
                },
                amazon_s3_dupindex => {
                        type => "boolean",
-                       default => 0,
-                       description => "store each index file twice? (to allow urls ending in \"/index.html\" and \"/\")",
+                       example => 0,
+                       description => "store each index file twice? (allows urls ending in \"/index.html\" and \"/\")",
                        safe => 1,
                        rebuild => 1,
                },
                        safe => 1,
                        rebuild => 1,
                },
-} #}}}
+}
 
 
-sub checkconfig { #{{{
+sub checkconfig {
        foreach my $field (qw{amazon_s3_key_id amazon_s3_key_file
                              amazon_s3_bucket}) {
                if (! exists $config{$field} || ! defined $config{$field}) {
        foreach my $field (qw{amazon_s3_key_id amazon_s3_key_file
                              amazon_s3_bucket}) {
                if (! exists $config{$field} || ! defined $config{$field}) {
@@ -99,11 +101,11 @@ sub checkconfig { #{{{
            ! defined $config{amazon_s3_prefix}) {
            $config{amazon_s3_prefix}="wiki/";
        }
            ! defined $config{amazon_s3_prefix}) {
            $config{amazon_s3_prefix}="wiki/";
        }
-} #}}}
+}
 
 {
 my $bucket;
 
 {
 my $bucket;
-sub getbucket { #{{{
+sub getbucket {
        return $bucket if defined $bucket;
        
        open(IN, "<", $config{amazon_s3_key_file}) || error($config{amazon_s3_key_file}.": ".$!);
        return $bucket if defined $bucket;
        
        open(IN, "<", $config{amazon_s3_key_file}) || error($config{amazon_s3_key_file}.": ".$!);
@@ -131,16 +133,20 @@ sub getbucket { #{{{
        }
 
        if (! $bucket) {
        }
 
        if (! $bucket) {
-               error(gettext("Failed to create bucket in S3: ").
+               # Try to use existing bucket.
+               $bucket=$s3->bucket($config{amazon_s3_bucket});
+       }
+       if (! $bucket) {
+               error(gettext("Failed to create S3 bucket: ").
                        $s3->err.": ".$s3->errstr."\n");
        }
 
        return $bucket;
                        $s3->err.": ".$s3->errstr."\n");
        }
 
        return $bucket;
-} #}}}
+}
 }
 
 # Given a file, return any S3 keys associated with it.
 }
 
 # Given a file, return any S3 keys associated with it.
-sub file2keys ($) { #{{{
+sub file2keys ($) {
        my $file=shift;
 
        my @keys;
        my $file=shift;
 
        my @keys;
@@ -160,14 +166,14 @@ sub file2keys ($) { #{{{
                }
        }
        return @keys;
                }
        }
        return @keys;
-} #}}}
+}
 
 package IkiWiki;
 use File::MimeInfo;
 use Encode;
 
 # This is a wrapper around the real writefile.
 
 package IkiWiki;
 use File::MimeInfo;
 use Encode;
 
 # This is a wrapper around the real writefile.
-sub writefile ($$$;$$) { #{{{
+sub writefile ($$$;$$) {
         my $file=shift;
         my $destdir=shift;
         my $content=shift;
         my $file=shift;
         my $destdir=shift;
         my $content=shift;
@@ -176,7 +182,7 @@ sub writefile ($$$;$$) { #{{{
 
        # First, write the file to disk.
        my $ret=$IkiWiki::Plugin::amazon_s3::subs{'IkiWiki::writefile'}->($file, $destdir, $content, $binary, $writer);
 
        # First, write the file to disk.
        my $ret=$IkiWiki::Plugin::amazon_s3::subs{'IkiWiki::writefile'}->($file, $destdir, $content, $binary, $writer);
-               
+
        my @keys=IkiWiki::Plugin::amazon_s3::file2keys("$destdir/$file");
 
        # Store the data in S3.
        my @keys=IkiWiki::Plugin::amazon_s3::file2keys("$destdir/$file");
 
        # Store the data in S3.
@@ -223,11 +229,12 @@ sub writefile ($$$;$$) { #{{{
        }
 
        return $ret;
        }
 
        return $ret;
-} #}}}
+}
 
 # This is a wrapper around the real prune.
 
 # This is a wrapper around the real prune.
-sub prune ($) { #{{{
+sub prune ($;$) {
        my $file=shift;
        my $file=shift;
+       my $up_to=shift;
 
        my @keys=IkiWiki::Plugin::amazon_s3::file2keys($file);
 
 
        my @keys=IkiWiki::Plugin::amazon_s3::file2keys($file);
 
@@ -244,7 +251,7 @@ sub prune ($) { #{{{
                }
        }
 
                }
        }
 
-       return $IkiWiki::Plugin::amazon_s3::subs{'IkiWiki::prune'}->($file);
-} #}}}
+       return $IkiWiki::Plugin::amazon_s3::subs{'IkiWiki::prune'}->($file, $up_to);
+}
 
 1
 
 1