#!/usr/bin/perl
#-----------------------------------------------------
#  The BeeWiki plugin parameters
#-----------------------------------------------------
# $Id: bee_get_local,v 1.3 2006/10/11 09:16:37 michald Exp $

use strict;
package TWiki::Plugins::BeeWikiPlugin;    

use CGI qw( :standard );

# =========================
use vars qw(
  	$PASSWDFILENAME $LOCALOSITOROOT $LOCALOSITOHTTPD $LOCALTWIKIABSPATH $MINREFRESHTIME $BASESTYLE
		$pluginName
	);

$pluginName = 'BeeWikiPlugin';	# Name of this Plugin
use lib '../lib';

#Export constant from BeeWikiConst
require "TWiki/Plugins/BeeWikiPlugin/BeeWikiConst.cfg";
import BeeWikiConst qw(:constant);

# =========================

my $q = new CGI;
my $f = $q->param("file");
my $srv = $q->param("srv");

die "ERROR: server is not specified" unless $srv;
die "ERROR: file is not specified" unless $f;

# =========================

if ($f =~ /^(.*)\.(jpg|jpeg|png|gif)$/) {
	my $type = ($2 eq 'jpg') ? "image/jpeg" : "image/$2";
	loadFile("$LOCALOSITOROOT/$srv/$f", $type);
}

# =========================

sub loadFile {
	my $file = shift;
	my $type = shift;

#print "$file, $type\n";

	return errMsg("can't access '$file'") unless -r $file && -f _;

	print $q->header("image/$type");

	undef local $/;
	open B, $file ||  return errMsg("can't open '$file'");
	print <B>;
	close B;

}

# =========================

sub errMsg {
	my $msg = shift;

	print $q->header("text/plain");
	print "ERROR: $msg";

	return
}

# =========================

1;

__END__

=head1 NAME

The BEE Project test environment

=head1 COPYRIGHT

I<Copyright (C) 2002-2005, Insight Strategy, a. s.> All rights reserved.

This program is free software; you can redistribute it and/or modify
it under the terms of the I<GNU General Public License> version 2 as published 
by the Free Software Foundation.
 
This program can be distributed or placed on a volume of a storage or
distribution medium together with files and/or programs covered by different
Licenses or distributed under different specific conditions. Only files with
reference to I<GNU General Public License> are covered by this kind of the
License. In addition, this program can use third party plug-ins or can make use
of meta-data files covered by specific different (possibly commercial)
Licenses. Please read their own  copyright notices and license conditions
for your reference.

This program is distributed in the hope that it will be useful,
but B<WITHOUT ANY WARRANTY>; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
I<GNU General Public License> in F<./COPYING> file for more details.

=cut
