tagging: Allow using titles in for related stories.
[matthijs/upstream/blosxom-plugins.git] / barijaona / static_file
1 # Blosxom Plugin: static_file
2 # Author(s): Barijaona Ramaholimihaso <blosxom@barijaona.com>
3 # original idea by Raffi Krikorian <r@bitwaste.com> (binary plugin)
4 # Version: 2007-09-09
5 # Documentation: See the bottom of this file or type: perldoc static_file
6
7 package static_file;
8 use strict;
9 use lib qq{$blosxom::plugin_dir/lib};
10 use CGI qw/:standard :netscape/;
11 use File::Copy;
12 use File::Find;
13 use File::Path;
14 use FileHandle;
15 use MIME::Types;
16
17
18 # --- Configuration Variables ---
19 use vars qw( @exclude_extensions_dynamic @exclude_extensions_static $default_type );
20 # You may want to hide from the public some kind of files,
21 # for instance the files Blosxom renders, generally .txt files.
22 # Files whose filename extension is in the following list
23 # will be ignored in dynamic rendering 
24 @exclude_extensions_dynamic = qw(txt_ ) unless scalar(@exclude_extensions_dynamic);
25
26 # Files whose filename extension is in the following list
27 # will be ignored in static rendering
28 @exclude_extensions_static = qw(txt txt_) unless scalar(@exclude_extensions_static); 
29
30 # This is the MIME type that will be used if nothing else
31 # can be found. If your server contains mostly text or HTML
32 # documents, "text/plain" is a good value.  If most of your
33 # content is binary, such as applications or images, you may
34 # want to use "application/octet-stream" instead to keep browsers
35 # from trying to display binary files as though they are text.
36 $default_type="text/plain" unless defined $default_type;
37
38 # --------------------------------
39
40 sub start {
41         return 1;
42 }
43
44 sub skip {
45         if( $blosxom::static_or_dynamic eq "dynamic" ) {
46         # if Blosxom is being dynamically generated -- then we have to
47         # see if the URL requested specifies a file that is on the
48         # filesystem, then it will return that file with the right
49         # Content-Type header
50         if ( ($blosxom::path_info =~ m!^(.*?/?)([^/]*?\.?)([^\.]*?)$!) &&
51         ($blosxom::others{"$blosxom::datadir/$1$2$3"} || $blosxom::files{"$blosxom::datadir/$1$2$3"}) )
52                 {
53                 # access path and name, name and extension of the file
54                 my $fileaccess = "$blosxom::datadir/$1$2$3" ;
55                 # adequate file extension ?
56                 if ( grep(/^$3$/, @exclude_extensions_dynamic) ) {return 0 ;}
57                 my MIME::Types $types = MIME::Types->new;
58                 my MIME::Type $mimetype = $types->mimeTypeOf($blosxom::flavour);
59                 $blosxom::header = 
60                 header( { -type=>
61                                   ( $mimetype ) ? 
62                                   $mimetype->mediaType . "/" . $mimetype->subType :
63                                   $default_type } );
64                 my @data = stat( $fileaccess );
65                 # forge some HTTP header stuff
66                 print("Accept-Ranges: bytes\nContent-Length: $data[7]\n$blosxom::header");
67 #               one could use cat from File::Cat but apparently it causes problem with big binaries files
68                 open( FILE, $fileaccess );
69                 binmode FILE;
70                 my $buffer;
71                 while (my $num_bytes_read = read( FILE, $buffer, 65536 ))
72                 {
73                         print STDOUT $buffer ;
74                 }
75                 close(FILE);
76
77                 # empty what blosxom main script may use
78                 $blosxom::header= "";
79                 $blosxom::output='';
80                 # tell blosxom main script to end processing for this file
81                 return 1;
82                 }
83                 return 0 ;
84         }
85         return 0;
86 }
87
88 sub end {
89         # if we are statically rendering the blog, then we have to copy
90         # over the entire data tree to the static tree so that it can be
91         # served out of there
92         if( $blosxom::static_or_dynamic eq "static" ) {
93         find( 
94                 sub {
95                         if( $File::Find::name =~ 
96                                 m!^$blosxom::datadir(.*?/?)([^/]*?\.?)([^\.]*?)$! ) {
97                                 # do we have to create a directory ?
98                                 if ( ( ! -e "$blosxom::static_dir$1$2$3") 
99                                         &&  (-d $File::Find::name) ) {
100                                                 mkpath ("$blosxom::static_dir$1$2$3");}
101                                 # is it a file to be updated ?
102                                 elsif ( ! grep(/^$3$/, @exclude_extensions_static) )    {
103                                         my @static = stat( "$blosxom::static_dir$1$2$3" ); 
104                                         my @data = stat( $File::Find::name );
105                                         ( ( ! -e "$blosxom::static_dir$1$2$3" ) ||
106                                         ( ( ( $static[9] < $data[9] ) ||
107                                         ( $static[7] != $data[7] ) ) ) ) &&
108                                         copy( $File::Find::name, 
109                                                 "$blosxom::static_dir$1$2$3" );
110                                 }
111                         }
112                 }, $blosxom::datadir );
113
114         }
115
116         return 1;
117
118 }
119
120
121
122 1;
123
124 __END__
125
126 =head1 NAME
127
128 Blosxom Plug-in: static_file
129
130 =head1 SYNOPSIS
131
132 Cause Blosxom to see if the URL requested actually exists on the filesystem.  If it does, then return the file, and then exit the script allowing for binary file serves.  If the file does not exist, then the Bloxsom script will continue noting whether it needs to do a flavour translation on the data file, etc.
133
134 This allows you to integrate existing webpages to a Blosxom site.
135
136 Another convinience of this is that when you wish to include an image or anything besides just the blog entry, you can place the file in the same directory as your text file, and reference it with a <$url /><$path /> (supposing you are using interpolate_fancy and its ability to interpolate variables in stories).
137
138 =head1 VERSION
139
140 2007-09-09
141
142 Version number is the date on which this version of the plug-in was created.
143
144 =head2 CHANGES
145
146 2007-09-09 :
147 - Bugfix : change how we check if we should serve the required extension (checks the complete extension, forgot that qw does not interpolate)
148
149 2007-09-08 :
150 - replaced File::Cat with a routine submitted by stephen2eq ;
151 - checks the presence of the file directly in %blosxom::files and %blosxom::others
152 - use strict ; programming style more compliant with some evolution ideas for blosxom
153
154 2004-09-19 :
155 - the default MIME type is configurable
156
157 =head1 AUTHOR
158
159 Barijaona Ramaholimihaso <blosxom@barijaona.com>
160
161 Original idea by Raffi Krikorian <r@bitwaste.com>, http://www.bitwaste.com/, author of the binary plugin.
162
163 =head1 SEE ALSO
164
165 Blosxom Home/Docs/Licensing: http://blosxom.sourceforge.net
166
167 Blosxom Plugin Docs: http://blosxom.sourceforge.net/plugins/
168
169 =head1 NOTES
170
171 If you are using this plugin in Blosxom's dynamic mode, you might consider creating a custom B<error> flavour, because Blosxom's default B<error> flavour could be confusing for the user.
172
173 It is recommanded, for performance optimization, to put a number in front of the name of this plugin, for instance to name it B<00static_file>, so that it loads among the first.
174
175 =head1 BUGS
176
177 None known; please send bug reports and feedback to the Blosxom development mailing list <blosxom-devel@lists.sourceforge.net>.
178
179 =head1 LICENSE
180
181 static_file Blosxom plugin
182 Copyright 2003-2007, Barijaona Ramaholimihaso
183 Copyright 2003, Raffi Krikorian
184
185 Permission is hereby granted, free of charge, to any person obtaining a
186 copy of this software and associated documentation files (the "Software"),
187 to deal in the Software without restriction, including without limitation
188 the rights to use, copy, modify, merge, publish, distribute, sublicense,
189 and/or sell copies of the Software, and to permit persons to whom the
190 Software is furnished to do so, subject to the following conditions:
191
192 The above copyright notice and this permission notice shall be included
193 in all copies or substantial portions of the Software.
194
195 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
196 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
197 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
198 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
199 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
200 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
201 OTHER DEALINGS IN THE SOFTWARE.