1 # Blosxom Plugin: static_file
2 # Author(s): Barijaona Ramaholimihaso <blosxom@barijaona.com>
3 # original idea by Raffi Krikorian <r@bitwaste.com> (binary plugin)
5 # Documentation: See the bottom of this file or type: perldoc static_file
9 use lib qq{$blosxom::plugin_dir/lib};
10 use CGI qw/:standard :netscape/;
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);
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_dynamic);
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;
38 # --------------------------------
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
50 if ( ($blosxom::path_info =~ m!^(.*?/?)([^/]*?\.?)([^\.]*?)$!) &&
51 ($blosxom::others{"$blosxom::datadir/$1$2$3"} || $blosxom::files{"$blosxom::datadir/$1$2$3"}) )
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);
62 $mimetype->mediaType . "/" . $mimetype->subType :
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 );
71 while (my $num_bytes_read = read( FILE, $buffer, 65536 ))
73 print STDOUT $buffer ;
77 # empty what blosxom main script may use
80 # tell blosxom main script to end processing for this file
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
92 if( $blosxom::static_or_dynamic eq "static" ) {
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" );
112 }, $blosxom::datadir );
128 Blosxom Plug-in: static_file
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.
134 This allows you to integrate existing webpages to a Blosxom site.
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).
142 Version number is the date on which this version of the plug-in was created.
147 - Bugfix : change how we check if we should serve the required extension (checks the complete extension, forgot that qw does not interpolate)
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
155 - the default MIME type is configurable
159 Barijaona Ramaholimihaso <static_file@barijaona.com>
161 Original idea by Raffi Krikorian <r@bitwaste.com>, http://www.bitwaste.com/, author of the binary plugin.
165 Blosxom Home/Docs/Licensing: http://www.raelity.org/apps/blosxom/
167 Blosxom Plugin Docs: http://www.raelity.org/apps/blosxom/plugin.shtml
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.
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.
177 static_file Blosxom plugin
178 Copyright 2003-2007, Barijaona Ramaholimihaso
179 Copyright 2003, Raffi Krikorian
181 Permission is hereby granted, free of charge, to any person obtaining a
182 copy of this software and associated documentation files (the "Software"),
183 to deal in the Software without restriction, including without limitation
184 the rights to use, copy, modify, merge, publish, distribute, sublicense,
185 and/or sell copies of the Software, and to permit persons to whom the
186 Software is furnished to do so, subject to the following conditions:
188 The above copyright notice and this permission notice shall be included
189 in all copies or substantial portions of the Software.
191 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
192 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
193 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
194 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
195 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
196 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
197 OTHER DEALINGS IN THE SOFTWARE.