/
AddCoversheet.pm
260 lines (201 loc) · 6.91 KB
/
AddCoversheet.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package EPrints::Plugin::Convert::AddCoversheet;
=pod
=head1 NAME
EPrints::Plugin::Convert::AddCoversheet - Prepend front and back coversheet sheets
=cut
use strict;
use warnings;
use encoding 'utf-8';
use File::Copy;
use Cwd;
#use Encode qw(encode);
use EPrints::Plugin::Convert;
our @ISA = qw/ EPrints::Plugin::Convert /;
our (%FORMATS, @ORDERED, %FORMATS_PREF);
@ORDERED = %FORMATS = qw(
pdf application/pdf
);
# formats pref maps mime type to file suffix. Last suffix
# in the list is used.
for(my $i = 0; $i < @ORDERED; $i+=2)
{
$FORMATS_PREF{$ORDERED[$i+1]} = $ORDERED[$i];
}
our $EXTENSIONS_RE = join '|', keys %FORMATS;
sub new
{
my( $class, %opts ) = @_;
my $self = $class->SUPER::new( %opts );
$self->{name} = "Coversheet Pages";
$self->{visible} = "";
unless( EPrints::Utils::require_if_exists('EPrints::OpenOfficeService') )
{
$self->{disable} = 1;
return $self;
}
my $oosrv = EPrints::OpenOfficeService->new();
unless( defined $oosrv && $oosrv->is_running() )
{
$self->{disable} = 1;
return $self;
}
$self->{visible} = "all";
if( defined $self->{session} )
{
# EPrints Services/sf2 - allow tags to be locally defined
$self->{tags} = $self->{session}->config( 'coversheet', 'tags' );
}
return $self;
}
sub can_convert
{
my ( $plugin, $doc ) = @_;
# need Ghostscript and python
return unless $plugin->get_repository->can_execute( "python" );
return unless -e $plugin->get_repository->config( "executables", "uno_converter");
my %types;
# Get the main file name
my $fn = $doc->get_main() or return ();
if( $fn =~ /\.($EXTENSIONS_RE)$/oi )
{
$types{"coverpage"} = { plugin => $plugin, };
}
return %types;
}
sub prepare_pages
{
my ($self, $doc, $pages) = @_;
my $eprint = $doc->get_eprint;
my $temp_dir = File::Temp->newdir( "ep-coversheetXXXX", TMPDIR => 1 );
my $session = $self->{session};
foreach my $coversheet_page (keys %{$pages})
{
my $filetype = $pages->{$coversheet_page}->{type};
my $file_path = $pages->{$coversheet_page}->{path};
next if( $filetype eq 'none' );
if ($filetype eq 'odt')
{
if ( EPrints::Utils::require_if_exists( "OpenOffice::OODoc" ) && $self->oo_is_running())
{
copy($file_path, "$temp_dir/$coversheet_page.odt");
EPrints::Utils::chown_for_eprints( "$temp_dir/$coversheet_page.odt" );
my $oodoc = OpenOffice::OODoc::odfDocument(file => "$temp_dir/$coversheet_page.odt");
$self->{tags} = $self->{session}->config( 'coversheet', 'tags' );
foreach my $tag (keys %{$self->{tags}})
{
eval
{
my @list = $oodoc->selectElementsByContent( '##'.$tag.'##', \&{$self->{tags}->{$tag}}, $eprint, $doc, $oodoc );
};
if( $@ )
{
$session->log( "[Convert::AddCoversheet] OpenOffice::OODoc failed to insert tag '$tag' on the coversheet: '$@'" );
next;
}
}
my $cwd = getcwd; #a quirk of $oodoc->save is that it saves a temp file to the working directory.
chdir $temp_dir;
$oodoc->save();
chdir $cwd;
#end of search and replace
#convert to pdf
system(
$session->config( 'executables', 'python' ),
$session->config( 'executables', 'uno_converter' ),
"$temp_dir/$coversheet_page.odt",
"$temp_dir/$coversheet_page.pdf",
);
#end of convert to pdf
unlink "$temp_dir/$coversheet_page.odt";
}
unless( -e "$temp_dir/$coversheet_page.pdf" )
{
$session->log("[Convert::AddCoversheet] Failed to add coversheet to document '".$doc->get_id."'\n");
}
}
elsif ($filetype eq 'pdf')
{
copy($file_path, $temp_dir . "/$coversheet_page.pdf");
}
else
{
$session->log("[Convert::AddCoversheet] Cannot handle coversheet of format '$filetype'\n");
}
}
return $temp_dir;
}
sub export
{
my ( $plugin, $target_dir, $doc, $type) = @_;
my $pages = $plugin->{_pages};
return unless( defined $pages );
my $repository = $plugin->get_repository;
# need Ghostscript and python
return unless $repository->can_execute( "python" );
return unless $repository->can_execute( "uno_converter" );
my $temp_dir = $plugin->prepare_pages($doc, $pages);
my $frontfile_path = $temp_dir . '/frontfile.pdf';
my $backfile_path = $temp_dir . '/backfile.pdf';
if ( ($pages->{frontfile}->{path} && ! -e $frontfile_path) || ($pages->{backfile}->{path} && ! -e $backfile_path) )
{
$repository->log( "[Convert::AddCoversheet] Unexpected absence of coversheet files." );
return;
}
unless( -d $target_dir )
{
EPrints::Platform::mkdir( $target_dir);
}
my $output_file = EPrints::Platform::join_path( $target_dir, $doc->get_main );
#my $output_file = $target_dir . '/' . $doc->get_main;
if( -e $output_file )
{
# remove old covered file
unlink( $output_file );
}
# EPrints Services/tmb 2011-08-26 get properly escaped filename via File dataobj
#my $doc_path = $doc->local_path."/".$doc->get_main;
my $doc_path = $doc->get_stored_file( $doc->get_main )->get_local_copy();
my @input_files;
push @input_files, $frontfile_path if( -e $frontfile_path );
push @input_files, $doc_path;
push @input_files, $backfile_path if( -e $backfile_path );
my $temp_output_dir = File::Temp->newdir( "ep-coversheet-finishedXXXX", TMPDIR => 1 );
my $temp_output_file = $temp_dir.'/temp.pdf';
# EPrints Services/pjw Modification to use Ghostscript rather than pdftk
my $gs_cmd = $plugin->get_repository->get_conf( "gs_pdf_stich_cmd" );
# add the output file
$gs_cmd .= $temp_output_file;
# add the input files
foreach my $input_file (@input_files)
{
$gs_cmd .= " '$input_file'";
}
my $sys_call_status = system($gs_cmd);
# check it worked
if (0 == $sys_call_status)
{
copy($temp_output_file, $output_file);
}
else
{
my $eprint = $doc->get_eprint;
# $repository->mail_administrator( 'Plugin/Screen/Coversheet:email_subject',
# 'Plugin/Screen/Coversheet:email_body',
# eprintid => $eprint->render_value("eprintid"),
# docid => $doc->render_value("docid") );
$repository->log("[Convert::AddCoversheet] Ghostscript could not create '$output_file'. Check the PDF is not password-protected.");
return;
}
EPrints::Utils::chown_for_eprints( $output_file );
# return the filename without the abs. path
return( $doc->get_main );
}
#will check to see if openoffice is running.
sub oo_is_running
{
my( $self ) = @_;
my $oosrv = EPrints::OpenOfficeService->new();
return 0 unless( defined $oosrv && $oosrv->is_running() );
return 1;
}
1;