| 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(error => "\$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( URI->new($rewrite->{url} . $local_path) ); |
|---|
| 34 |
$context->log(info => "enclosure URL set to " . $enclosure->url); |
|---|
| 35 |
$enclosure->auto_set_type(); |
|---|
| 36 |
$context->log(info => "enclosure type set to " . $enclosure->type); |
|---|
| 37 |
last; |
|---|
| 38 |
} |
|---|
| 39 |
} |
|---|
| 40 |
} |
|---|
| 41 |
} |
|---|
| 42 |
|
|---|
| 43 |
1; |
|---|
| 44 |
|
|---|
| 45 |
__END__ |
|---|
| 46 |
|
|---|
| 47 |
=head1 NAME |
|---|
| 48 |
|
|---|
| 49 |
Plagger::Plugin::Filter::RewriteEnclosureURL - Rewrite enclosure URL for republishing |
|---|
| 50 |
|
|---|
| 51 |
=head1 SYNOPSIS |
|---|
| 52 |
|
|---|
| 53 |
- module: Filter::FetchEnclosure |
|---|
| 54 |
config: |
|---|
| 55 |
dir: /home/miyagawa/public_html |
|---|
| 56 |
|
|---|
| 57 |
- module: Filter::RewriteEnclosureURL |
|---|
| 58 |
config: |
|---|
| 59 |
rewrite: |
|---|
| 60 |
- local: /home/miyagawa/public_html/ |
|---|
| 61 |
url: http://rock/~miyagawa/ |
|---|
| 62 |
|
|---|
| 63 |
=head1 DESCRIPTION |
|---|
| 64 |
|
|---|
| 65 |
This plugin rewrites enclosure URL using rewrite rule. |
|---|
| 66 |
|
|---|
| 67 |
=head1 CONFIG |
|---|
| 68 |
|
|---|
| 69 |
=over 4 |
|---|
| 70 |
|
|---|
| 71 |
=item rewrite |
|---|
| 72 |
|
|---|
| 73 |
List of hash that defines rule to rewrite URL. C<local> to represent |
|---|
| 74 |
local path, which should match with enclosure's local_path and C<url> |
|---|
| 75 |
to represent publicly accessible HTTP base URL. |
|---|
| 76 |
|
|---|
| 77 |
In this example, I</home/miyagawa/public_html/foo.mp3> is rewritten to |
|---|
| 78 |
I<http://rock/~miyagawa/foo.mp3>. |
|---|
| 79 |
|
|---|
| 80 |
=back |
|---|
| 81 |
|
|---|
| 82 |
=head1 AUTHOR |
|---|
| 83 |
|
|---|
| 84 |
Tatsuhiko Miyagawa |
|---|
| 85 |
|
|---|
| 86 |
=head1 SEE ALSO |
|---|
| 87 |
|
|---|
| 88 |
L<Plagger> |
|---|
| 89 |
|
|---|
| 90 |
=cut |
|---|
| 91 |
|
|---|
| 92 |
|
|---|