root/trunk/plagger/lib/Plagger/Plugin/Filter/RewriteEnclosureURL.pm

Revision 749 (checked in by miyagawa, 14 years ago)
  • New plugin Filter::FindEnclosures? to auto-discover enclosures
  • New plugin Filter:::HEADEnclosureMetadata to get metadata from enclosures with HEAD requests
  • Added is_inline attribute to Plagger::Enclosure. Fixes #230
  • Publish::Feed: Only publish enclosures with URL set and no is_line flag. Fixes #234
  • Added Plagger::Util::mime_type_of
  • Added flv/m4a to known MIME types list of MIME::Types
Line 
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__
44
45 =head1 NAME
46
47 Plagger::Plugin::Filter::RewriteEnclosureURL - Rewrite enclosure URL for republishing
48
49 =head1 SYNOPSIS
50
51   - module: Filter::FetchEnclosure
52     config:
53       dir: /home/miyagawa/public_html
54
55   - module: Filter::RewriteEnclosureURL
56     config:
57       rewrite:
58         - local: /home/miyagawa/public_html/
59           url:   http://rock/~miyagawa/
60
61 =head1 DESCRIPTION
62
63 This plugin rewrites enclosure URL using rewrite rule.
64
65 =head1 CONFIG
66
67 =over 4
68
69 =item rewrite
70
71 List of hash that defines rule to rewrite URL. C<local> to represent
72 local path, which should match with enclosure's local_path and C<url>
73 to represent publicly accessible HTTP base URL.
74
75 In this example, I</home/miyagawa/public_html/foo.mp3> is rewritten to
76 I<http://rock/~miyagawa/foo.mp3>.
77
78 =back
79
80 =head1 AUTHOR
81
82 Tatsuhiko Miyagawa
83
84 =head1 SEE ALSO
85
86 L<Plagger>
87
88 =cut
89
Note: See TracBrowser for help on using the browser.