| 1 | package Plagger::Plugin::Filter::RewriteEnclosureURL; | ||
| 2 | use strict; | ||
| 3 | use base qw( Plagger::Plugin ); | ||
| 4 | |||
| 5 | sub register { | ||
| 6 | my($self, $context) = @_; | ||
| 7 | $context->register_hook( | ||
| 8 | $self, | ||
| 9 | 'update.entry.fixup' => \&filter, | ||
| 10 | ); | ||
| 11 | } | ||
| 12 | |||
| 13 | sub init { | ||
| 14 | my $self = shift; | ||
| 15 | $self->SUPER::init(@_); | ||
| 16 | |||
| 17 | $self->conf->{rewrite} or Plagger->context->error("config 'rewrite' is not set."); | ||
| 18 | $self->conf->{rewrite} = [ $self->conf->{rewrite} ] unless ref $self->conf->{rewrite}; | ||
| 19 | } | ||
| 20 | |||
| 21 | sub filter { | ||
| 22 | my($self, $context, $args) = @_; | ||
| 23 | |||
| 24 | for my $enclosure ($args->{entry}->enclosures) { | ||
| 25 | my $local_path = $enclosure->local_path; | ||
| 26 | unless ($local_path) { | ||
| 27 | $context->log(warn => "\$enclosure->local_path is not set. You need to load Filter::FetchEnclosure to use this plugin."); | ||
| 28 | return; | ||
| 29 | } | ||
| 30 | |||
| 31 | for my $rewrite (@{ $self->conf->{rewrite} }) { | ||
| 32 | if ($local_path =~ s/^$rewrite->{local}//) { | ||
| 33 | $enclosure->url( $rewrite->{url} . $local_path ); | ||
| 34 | $context->log(info => "enclosure URL set to " . $enclosure->url); | ||
| 35 | last; | ||
| 36 | } | ||
| 37 | } | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | 1; | ||
| 42 | |||
| 43 | __END__ |