#!/usr/bin/perl

#use utf8;
use strict;
use CGI;
use MIME::Types;
use Opals::Context;


my $cgi = CGI->new;
my $input = $cgi->Vars();
my $fileId = $input->{'fileId'};
my $db_name = Opals::Context->config('db_name');
my $imex    = Opals::Context->config('imex');
my $filePath="$imex/uploadDoc/$db_name/$fileId";



if(-f $filePath){
    my $types = MIME::Types->new;
    my $mime = $types->mimeTypeOf($filePath);

    print "Content-type:$mime \n";
    print "Content-Disposition:attachment;filename=$fileId\n\n";
    open IN,"<$filePath";
    while (<IN>){
        print $_;
    }
    close IN;

}
else{
    print "Status: 404 Not Found\n";
    print "Content-Type: text/html\n\n";
    print "<title>404 Not Found</title>\n";
    print "<h1>404 Not Found </h1>\n";
}

exit;

