1 rizwank 1.1 # TWiki Collaboration Platform, http://TWiki.org/
2 #
3 # Copyright (C) 1999-2004 Peter Thoeny, peter@thoeny.com
4 #
5 # For licensing info read license.txt file in the TWiki root.
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details, published at
15 # http://www.gnu.org/copyleft/gpl.html
16 =begin twiki
17
18 ---+ TWiki::UI::Viewfile
19
20 UI functions for file viewing
21
22 rizwank 1.1 =cut
23
24 package TWiki::UI::Viewfile;
25
26 use strict;
27 use TWiki;
28 use TWiki::Store;
29 use TWiki::UI;
30
31 =pod
32
33 ---++ view( $web, $topic, $query )
34 Command handler for viewfile. View a file in the browser.
35 Some parameters are passed in CGI query:
36 | =filename= | |
37 | =rev= | |
38
39 =cut
40
41 sub view {
42 my ( $webName, $topic, $query ) = @_;
43 rizwank 1.1
44 my $fileName = $query->param( 'filename' );
45 my $rev = $query->param( 'rev' ) || "";
46
47 return unless TWiki::UI::webExists( $webName, $topic );
48
49 my $topRev = TWiki::Store::getRevisionNumber( $webName, $topic, $fileName );
50
51 if( ( $rev ) && ( $rev ne $topRev ) ) {
52 my $fileContent = TWiki::Store::readAttachmentVersion( $webName, $topic, $fileName, $rev );
53 if( $fileContent ) {
54 my $mimeType = _suffixToMimeType( $fileName );
55 print $query->header( -type => $mimeType,
56 -Content_Disposition => "inline;filename=$fileName");
57 print "$fileContent";
58 return;
59 } else {
60 # If no file content we'll try and show pub content, should there be a warning FIXME
61 }
62 }
63
64 rizwank 1.1 # this should actually kick off a document conversion
65 # (.doc, .xls... to .html) and show the html file.
66 # Convert only if html file does not yet exist
67 # for now, show the original document:
68
69 my $pubUrlPath = &TWiki::getPubUrlPath();
70 my $host = $TWiki::urlHost;
71 TWiki::UI::redirect( "$host$pubUrlPath/$webName/$topic/$fileName" );
72 }
73
74 sub _suffixToMimeType {
75 my( $theFilename ) = @_;
76
77 my $mimeType = 'text/plain';
78 if( $theFilename =~ /\.(.+)$/ ) {
79 my $suffix = $1;
80 my @types = grep{ s/^\s*([^\s]+).*?\s$suffix\s.*$/$1/i }
81 map{ "$_ " }
82 split( /[\n\r]/, &TWiki::Store::readFile( $TWiki::mimeTypesFilename ) );
83 $mimeType = $types[0] if( @types );
84 }
85 rizwank 1.1 return $mimeType;
86 }
87
88 1;
|