Show office documents as PDF (modern browsers usually have PDF support)

3col
Vitaliy Filippov 2015-04-13 14:29:28 +03:00
parent 947b75069a
commit 44b52654d4
2 changed files with 37 additions and 40 deletions

View File

@ -162,7 +162,6 @@ whether or not the attachment is obsolete
whether or not the attachment is private
=item B<bug>
the bug object to which the attachment is attached
@ -186,9 +185,9 @@ the content of the attachment
check if the attachment has office document content type
=item B<_get_converted_html>
=item B<convert_to>
return converted html from the content of the attachment
return converted html or pdf from the content of the attachment
=item B<datasize>
@ -285,44 +284,26 @@ sub isOfficeDocument
return 1 && $self->{mimetype} =~ m/(officedocument|msword|excel|html|opendocument)/;
}
sub _get_converted_html
sub convert_to
{
my $self = shift;
my ($format) = @_;
$format = $format eq 'pdf' ? 'pdf' : 'html';
my $file_path = $self->_get_local_filename();
my $file_cache_path = $self->_get_local_cache_filename();
my $file_cache_path = $self->_get_local_cache_filename().'.'.$format;
my $dir_cache_path = $self->_get_local_cache_dir();
my $converted_html;
# Read cached converted file
if (-e $file_cache_path)
{
if (open(AH, '<:encoding(UTF-8)', $file_cache_path))
{
local $/;
$converted_html = <AH>;
close(AH);
}
}
else
if (!-e $file_cache_path)
{
# Work with existing files
if (-e $file_path)
{
$ENV{HOME} = '/tmp/';
system("/usr/bin/libreoffice --invisible --convert-to html --outdir ".$dir_cache_path." ".$file_path." 1>&2");
if (-e $dir_cache_path."/attachment.html")
system("/usr/bin/libreoffice --invisible --convert-to $format --outdir $dir_cache_path $file_path 1>&2");
if (-e "$dir_cache_path/attachment.$format")
{
rename $dir_cache_path."/attachment.html",$file_cache_path;
}
if (-e $file_cache_path)
{
if (open(AH, '<:encoding(UTF-8)', $file_cache_path))
{
local $/;
$converted_html = <AH>;
close(AH);
}
rename "$dir_cache_path/attachment.$format", $file_cache_path;
}
}
else
@ -331,6 +312,15 @@ sub _get_converted_html
# FIXME: save data from DB to file and convert it to HTML.
}
}
# Read cached converted file
if (-e $file_cache_path && open(AH, $file_cache_path))
{
local $/ = undef;
$converted_html = <AH>;
close AH;
}
return $converted_html;
}

View File

@ -344,13 +344,27 @@ sub view
$filename =~ s/\\/\\\\/g; # escape backslashes
$filename =~ s/"/\\"/g; # escape quotes
if ($cgi->user_agent() =~ /MSIE/ && $cgi->user_agent() !~ /Opera/)
{
# Bug 57108 - russian filenames for MSIE
Encode::_utf8_off($filename);
Encode::from_to($filename, 'utf-8', 'cp1251');
}
# Bug 129398 - View office documents online
if (defined $action && $action eq 'online_view' && $attachment->isOfficeDocument())
{
Bugzilla->send_header();
my $html = $attachment->_get_converted_html();
$html =~ s/\n([^\n]*List_20_Paragraph.*?\{.*?)margin:100%;(.*?\}[^\n]*?)\n/\n$1$2\n/;
print $html;
# FIXME: Detect pdf support in MSIE and show PDF as pictures if not present
# Prevent recoding of binary data
disable_utf8();
use bytes;
my $pdf = $attachment->convert_to('pdf');
Bugzilla->send_header(
-type => 'application/pdf',
-content_disposition => "inline; filename=\"$filename\"",
-content_length => length $pdf,
);
print $pdf;
}
else
{
@ -360,13 +374,6 @@ sub view
&& $contenttype =~ /$disposition/is
? "inline" : "attachment";
if ($cgi->user_agent() =~ /MSIE/ && $cgi->user_agent() !~ /Opera/)
{
# Bug 57108 - russian filenames for MSIE
Encode::_utf8_off($filename);
Encode::from_to($filename, 'utf-8', 'cp1251');
}
# Don't send a charset header with attachments--they might not be UTF-8.
# However, we do allow people to explicitly specify a charset if they
# want.