use C4::Context;
use MARC::Field;
-use Koha::Upload;
+use Koha::UploadedFiles;
=head1 HTML5Media
if ( $HTML5Media{srcblock} =~ /\Qopac-retrieve-file.pl\E/ ) {
my ( undef, $id ) = split /id=/, $HTML5Media{srcblock};
next if !$id;
- my $public = ( ( caller )[1] =~ /opac/ ) ? { public => 1 }: {};
- my $upl = Koha::Upload->new( $public )->get({ hashvalue => $id });
- next if !$upl || $upl->{name} !~ /\./;
- $HTML5Media{extension} = ( $upl->{name} =~ m/([^.]+)$/ )[0];
+ my %public = ( ( caller )[1] =~ /opac/ ) ? ( public => 1 ): ();
+ my $upload = Koha::UploadedFiles->search({
+ hashvalue => $id, %public,
+ })->next;
+ next if !$upload || $upload->filename !~ /\./;
+ $HTML5Media{extension} = ( $upload->filename =~ m/([^.]+)$/ )[0];
}
# check remote files
else {
=head1 SYNOPSIS
use Koha::Upload;
+ use Koha::UploadedFiles;
# add an upload (see tools/upload-file.pl)
# the public flag allows retrieval via OPAC
# Do something with $upload->count, $upload->result or $upload->err
# get some upload records (in staff)
- # Note: use the public flag for OPAC
- my @uploads = Koha::Upload->new->get( term => $term );
- $template->param( uploads => \@uploads );
+ my @uploads1 = Koha::UploadedFiles->search({ filename => $name });
+ my @uploads2 = Koha::UploadedFiles->search_term({ term => $term });
# staff download
- my $rec = Koha::Upload->new->get({ id => $id, filehandle => 1 });
- my $fh = $rec->{fh};
- my @hdr = Koha::Upload->httpheaders( $rec->{name} );
+ my $rec = Koha::UploadedFiles->find( $id );
+ my $fh = $rec->file_handle;
+ my @hdr = Koha::Upload->httpheaders( $rec->filename );
print Encode::encode_utf8( $input->header( @hdr ) );
while( <$fh> ) { print $_; }
$fh->close;
functionality of both.
The module has been revised to use Koha::Object[s]; the delete method
- has been moved to Koha::UploadedFile[s].
+ has been moved to Koha::UploadedFile[s], as well as the get method.
-=head1 METHODS
+=head1 INSTANCE METHODS
=cut
return $err;
}
-=head2 get
-
- Returns arrayref of uploaded records (hash) or one uploaded record.
- You can pass id => $id or hashvalue => $hash or term => $term.
- Optional parameter filehandle => 1 returns you a filehandle too.
-
-=cut
-
-sub get {
- my ( $self, $params ) = @_;
- my $temp= $self->_lookup( $params );
- my ( @rv, $res);
- foreach my $r ( @$temp ) {
- undef $res;
- foreach( qw[id hashvalue filesize uploadcategorycode public permanent owner] ) {
- $res->{$_} = $r->{$_};
- }
- $res->{name} = $r->{filename};
- $res->{path} = $self->_full_fname($r);
- if( $res->{path} && -r $res->{path} ) {
- if( $params->{filehandle} ) {
- my $fh = IO::File->new( $res->{path}, "r" );
- $fh->binmode if $fh;
- $res->{fh} = $fh;
- }
- push @rv, $res;
- } else {
- $self->{files}->{ $r->{filename} }->{errcode}=5; #not readable
- }
- last if !wantarray;
- }
- return wantarray? @rv: $res;
-}
-
=head1 CLASS METHODS
=head2 getCategories
# if the file exists and it is registered, then set error
# if it exists, but is not in the database, we will overwrite
if( -e "$dir/$fn" &&
- Koha::UploadedFiles->search({
- hashvalue => $hashval,
- uploadcategorycode => $self->{category},
- })->count ) {
+ Koha::UploadedFiles->search({
+ hashvalue => $hashval,
+ uploadcategorycode => $self->{category},
+ })->count ) {
$self->{files}->{$filename}->{errcode} = 1; #already exists
return;
}
return $dir;
}
-sub _full_fname {
- my ( $self, $rec ) = @_;
- my $p;
- if( ref $rec ) {
- $p = File::Spec->catfile(
- $rec->{permanent}? $self->{rootdir}: $self->{tmpdir},
- $rec->{dir},
- $rec->{hashvalue}. '_'. $rec->{filename}
- );
- }
- return $p;
-}
-
sub _hook {
my ( $self, $filename, $buffer, $bytes_read, $data ) = @_;
$filename= Encode::decode_utf8( $filename ); # UTF8 chars in filename
$self->{files}->{$filename}->{id} = $rec->id if $rec;
}
-sub _lookup {
- my ( $self, $params ) = @_;
-
- my ( $cond, $attr, %pubhash );
- %pubhash = $self->{public}? ( public => 1 ): ();
- if( $params->{id} ) {
- return [] if $params->{id} !~ /^\d+(,\d+)*$/;
- $cond = { id => [ split ',', $params->{id} ], %pubhash };
- } elsif( $params->{hashvalue} ) {
- $cond = { hashvalue => $params->{hashvalue}, %pubhash };
- } elsif( $params->{term} ) {
- $cond =
- [ { filename => { like => '%'.$params->{term}.'%' }, %pubhash },
- { hashvalue => { like => '%'.$params->{term}.'%' }, %pubhash } ];
- } else {
- return [];
- }
- $attr = { order_by => { -asc => 'id' }};
-
- return Koha::UploadedFiles->search( $cond, $attr )->unblessed;
- # Does always return an arrayref (perhaps an empty one)
-}
-
sub _compute {
# Computes hash value when sub hook feeds the first block
# For temporary files, the id is made unique with time
=head3 delete
Delete uploaded file.
-It deletes not only the record, but also the actual file.
+It deletes not only the record, but also the actual file (unless you pass
+the keep_file parameter).
Returns filename on successful delete or undef.
=cut
sub delete {
- my ( $self ) = @_;
+ my ( $self, $params ) = @_;
my $name = $self->filename;
my $file = $self->full_path;
- if( !-e $file ) { # we will just delete the record
+ if( $params->{keep_file} ) {
+ return $name if $self->SUPER::delete;
+ } elsif( !-e $file ) { # we will just delete the record
warn "Removing record for $name within category ".
$self->uploadcategorycode. ", but file was missing.";
return $name if $self->SUPER::delete;
return $path;
}
+=head3 file_handle
+
+Returns a file handle for an uploaded file.
+
+=cut
+
+sub file_handle {
+ my ( $self ) = @_;
+ $self->{_file_handle} = IO::File->new( $self->full_path, "r" );
+ return if !$self->{_file_handle};
+ $self->{_file_handle}->binmode;
+ return $self->{_file_handle};
+}
+
=head2 CLASS METHODS
=head3 root_directory
Returns true if no errors occur.
Delete_errors returns the number of errors when deleting files.
+Parameter keep_file may be used to delete records, but keep files.
+
=cut
sub delete {
- my ( $self ) = @_;
+ my ( $self, $params ) = @_;
# We use the individual delete on each resultset record
my $err = 0;
while( my $row = $self->_resultset->next ) {
my $kohaobj = Koha::UploadedFile->_new_from_dbic( $row );
- $err++ if !$kohaobj->delete;
+ $err++ if !$kohaobj->delete( $params );
}
$self->{delete_errors} = $err;
return $err==0;
return $self->{delete_errors};
}
+=head3 search_term
+
+Search_term allows you to pass a term to search in filename and hashvalue.
+If you do not pass include_private, only public records are returned.
+
+Is only a wrapper around Koha::Objects search. Has similar return value.
+
+=cut
+
+sub search_term {
+ my ( $self, $params ) = @_;
+ my $term = $params->{term} // '';
+ my %public = ();
+ if( !$params->{include_private} ) {
+ %public = ( public => 1 );
+ }
+ return $self->search(
+ [ { filename => { like => '%'.$term.'%' }, %public },
+ { hashvalue => { like => '%'.$params->{term}.'%' }, %public } ],
+ { order_by => { -asc => 'id' }},
+ );
+}
+
=head2 CLASS METHODS
=head3 _type
<tbody>
[% FOREACH record IN uploads %]
<tr>
- <td>[% record.name %]</td>
+ <td>[% record.filename %]</td>
<td>[% record.filesize %]</td>
<td>[% record.hashvalue %]</td>
<td>[% record.uploadcategorycode %]</td>
use C4::Items;
use C4::Members;
use C4::Stats;
-use Koha::Upload;
+use Koha::UploadedFiles;
use Date::Calc qw( Add_Delta_Days Date_to_Days );
our $dbh = C4::Context->dbh();
if ($fileID) {
- my $upload = Koha::Upload->new->get({ id => $fileID, filehandle => 1 });
- my $fh = $upload->{fh};
- my @input_lines = <$fh>;
+ my $upload = Koha::UploadedFiles->find($fileID);
+ my $fh = $upload? $upload->file_handle: undef;
+ my @input_lines = $fh? <$fh>: ();
+ $fh->close if $fh;
my $header_line = shift @input_lines;
my $file_info = parse_header_line($header_line);
use C4::Members;
use C4::Stats;
use C4::BackgroundJob;
-use Koha::Upload;
+use Koha::UploadedFiles;
use Koha::Account;
use Koha::Patrons;
$template->param(transactions_loaded => 1);
$template->param(messages => $results->{results});
} elsif ($fileID) {
- my $upload = Koha::Upload->new->get({ id => $fileID, filehandle => 1 });
- my $fh = $upload->{fh};
- my $filename = $upload->{name};
- my @input_lines = <$fh>;
+ my $upload = Koha::UploadedFiles->find( $fileID );
+ my $fh = $upload? $upload->file_handle: undef;
+ my $filename = $upload? $upload->filename: undef;
+ my @input_lines = $fh? <$fh>: ();
+ $fh->close if $fh;
my $job = undef;
use C4::Context;
use C4::Output;
use Koha::Upload;
+use Koha::UploadedFiles;
my $input = CGI::->new;
my $hash = $input->param('id'); # historically called id (used in URLs?)
-my $upl = Koha::Upload->new({ public => 1 });
-my $rec = $upl->get({ hashvalue => $hash, filehandle => 1 });
-my $fh = $rec->{fh};
+my $rec = Koha::UploadedFiles->search({
+ hashvalue => $hash, public => 1,
+ # DO NOT REMOVE the public flag: this is an opac script !
+})->next;
+my $fh = $rec? $rec->file_handle: undef;
+
if( !$rec || !$fh ) {
my ( $template, $user, $cookie ) = get_template_and_user({
query => $input,
$template->param( hash => $hash );
output_html_with_http_headers $input, $cookie, $template->output;
} else {
- my @hdr = $upl->httpheaders( $rec->{name} );
+ my @hdr = Koha::Upload->httpheaders( $rec->filename );
print Encode::encode_utf8( $input->header( @hdr ) );
while( <$fh> ) {
print $_;
my $schema = Koha::Database->new->schema;
$schema->storage->txn_begin;
-my $dbh = C4::Context->dbh;
our $current_upload = 0;
our $uploads = [
{ name => 'file4', cat => undef, size => 5000 }, # temp duplicate
],
[
- { name => 'file5', cat => undef, size => 7000 }, # temp duplicate
+ { name => 'file5', cat => undef, size => 7000 },
],
];
# Start testing
subtest 'Test01' => sub {
- plan tests => 9;
+ plan tests => 11;
test01();
};
subtest 'Test02' => sub {
- plan tests => 4;
+ plan tests => 5;
test02();
};
subtest 'Test03' => sub {
test05();
};
subtest 'Test06' => sub {
- plan tests => 2;
+ plan tests => 3;
test06();
};
subtest 'Test07' => sub {
sub test01 {
# Delete existing records (for later tests)
- $dbh->do( "DELETE FROM uploaded_files" );
+ # Passing keep_file suppresses warnings
+ Koha::UploadedFiles->new->delete({ keep_file => 1 });
# Check mocked directories
is( Koha::UploadedFile->permanent_directory, $tempdir,
my $res= $upl->result;
is( $res =~ /^\d+,\d+$/, 1, 'Upload 1 includes two files' );
is( $upl->count, 2, 'Count returns 2 also' );
- foreach my $r ( $upl->get({ id => $res }) ) {
- if( $r->{name} eq 'file1' ) {
- is( $r->{uploadcategorycode}, 'A', 'Check category A' );
- is( $r->{filesize}, 6000, 'Check size of file1' );
- } elsif( $r->{name} eq 'file2' ) {
- is( $r->{filesize}, 8000, 'Check size of file2' );
- is( $r->{public}, undef, 'Check public undefined' );
- }
- }
is( $upl->err, undef, 'No errors reported' );
+
+ my $rs = Koha::UploadedFiles->search({
+ id => [ split ',', $res ]
+ }, { order_by => { -asc => 'filename' }});
+ my $rec = $rs->next;
+ is( $rec->filename, 'file1', 'Check file name' );
+ is( $rec->uploadcategorycode, 'A', 'Check category A' );
+ is( $rec->filesize, 6000, 'Check size of file1' );
+ $rec = $rs->next;
+ is( $rec->filename, 'file2', 'Check file name 2' );
+ is( $rec->filesize, 8000, 'Check size of file2' );
+ is( $rec->public, undef, 'Check public undefined' );
}
sub test02 {
my $cgi= $upl->cgi;
is( $upl->count, 1, 'Upload 2 includes one file' );
my $res= $upl->result;
- my $r = $upl->get({ id => $res, filehandle => 1 });
- is( $r->{uploadcategorycode}, 'B', 'Check category B' );
- is( $r->{public}, 1, 'Check public == 1' );
- is( ref($r->{fh}) eq 'IO::File' && $r->{fh}->opened, 1, 'Get returns a file handle' );
+ my $rec = Koha::UploadedFiles->find( $res );
+ is( $rec->uploadcategorycode, 'B', 'Check category B' );
+ is( $rec->public, 1, 'Check public == 1' );
+ my $fh = $rec->file_handle;
+ is( ref($fh) eq 'IO::File' && $fh->opened, 1, 'Get returns a file handle' );
+
+ my $orgname = $rec->filename;
+ $rec->filename( 'doesprobablynotexist' )->store;
+ is( $rec->file_handle, undef, 'Sabotage with file handle' );
+ $rec->filename( $orgname )->store;
}
sub test03 {
my $upl = Koha::Upload->new({ tmp => 1 }); #temporary
my $cgi= $upl->cgi;
is( $upl->count, 1, 'Upload 3 includes one temporary file' );
- my $r = $upl->get({ id => $upl->result });
- is( $r->{uploadcategorycode} =~ /_upload$/, 1, 'Check category temp file' );
+ my $rec = Koha::UploadedFiles->find( $upl->result );
+ is( $rec->uploadcategorycode =~ /_upload$/, 1, 'Check category temp file' );
}
sub test04 { # Fail on a file already there
my $cgi= $upl->cgi;
is( $upl->count, 1, 'Upload 5 adds duplicate temporary file' );
my $id = $upl->result;
- my $r = $upl->get({ id => $id });
+ my $path = Koha::UploadedFiles->find( $id )->full_path;
# testing delete via UploadedFiles (plural)
my $delete = Koha::UploadedFiles->search({ id => $id })->delete;
is( $delete, 1, 'Delete successful' );
- isnt( -e $r->{path}, 1, 'File no longer found after delete' );
- is( scalar $upl->get({ id => $id }), undef, 'Record also gone' );
+ isnt( -e $path, 1, 'File no longer found after delete' );
+ is( Koha::UploadedFiles->find( $id ), undef, 'Record also gone' );
# testing delete via UploadedFile (singular)
# Note that find returns a Koha::Object
$upl = Koha::Upload->new({ tmp => 1 });
$upl->cgi;
- $id = $upl->result;
- my $kohaobj = Koha::UploadedFiles->find( $id );
+ my $kohaobj = Koha::UploadedFiles->find( $upl->result );
my $name = $kohaobj->filename;
- my $path = $kohaobj->full_path;
+ $path = $kohaobj->full_path;
$delete = $kohaobj->delete;
is( $delete, $name, 'Delete successful' );
isnt( -e $path, 1, 'File no longer found after delete' );
}
-sub test06 { #some extra tests for get
- my $upl = Koha::Upload->new({ public => 1 });
- my @rec = $upl->get({ term => 'file' });
- is( @rec, 1, 'Get returns only one public result (file3)' );
- $upl = Koha::Upload->new; # public == 0
- @rec = $upl->get({ term => 'file' });
- is( @rec, 4, 'Get returns now four results' );
+sub test06 { #search_term with[out] private flag
+ my @recs = Koha::UploadedFiles->search_term({ term => 'file' });
+ is( @recs, 1, 'Returns only one public result' );
+ is( $recs[0]->filename, 'file3', 'Should be file3' );
+
+ is( Koha::UploadedFiles->search_term({
+ term => 'file', include_private => 1,
+ })->count, 4, 'Returns now four results' );
}
sub test07 { #simple test for httpheaders and getCategories
my $upload01 = $builder->build({ source => 'UploadedFile' });
my $found = Koha::UploadedFiles->find( $upload01->{id} );
is( $found->id, $upload01->{id}, 'Koha::Object returns id' );
- $found->delete;
+ $found->delete({ keep_file => 1 }); #note that it does not exist
$found = Koha::UploadedFiles->search(
{ id => $upload01->{id} },
);
use C4::Biblio;
use C4::ImportBatch;
use C4::Matcher;
-use Koha::Upload;
+use Koha::UploadedFiles;
use C4::BackgroundJob;
use C4::MarcModificationTemplates;
use Koha::Plugins;
my $results = $job->results();
$template->param(map { $_ => $results->{$_} } keys %{ $results });
} elsif ($fileID) {
- my $upload = Koha::Upload->new->get({ id => $fileID });
- my ( $file, $filename ) = ( $upload->{path}, $upload->{name} );
+ my $upload = Koha::UploadedFiles->find( $fileID );
+ my $file = $upload->full_path;
+ my $filename = $upload->filename;
+
my ( $errors, $marcrecords );
if( $format eq 'MARCXML' ) {
( $errors, $marcrecords ) = C4::ImportBatch::RecordsFromMARCXMLFile( $file, $encoding);
use C4::Auth;
use C4::Output;
use C4::Images;
-use Koha::Upload;
+use Koha::UploadedFiles;
use C4::Log;
my $debug = 1;
my $filetype = $input->param('filetype');
my $biblionumber = $input->param('biblionumber');
-my $uploadfilename = $input->param('uploadfile');
+#my $uploadfilename = $input->param('uploadfile'); # obsolete?
my $replace = !C4::Context->preference("AllowMultipleCovers")
|| $input->param('replace');
my $op = $input->param('op');
my $total = 0;
if ($fileID) {
- my $upload = Koha::Upload->new->get({ id => $fileID, filehandle => 1 });
+ my $upload = Koha::UploadedFiles->find( $fileID );
if ( $filetype eq 'image' ) {
- my $fh = $upload->{fh};
+ my $fh = $upload->file_handle;
my $srcimage = GD::Image->new($fh);
+ $fh->close if $fh;
if ( defined $srcimage ) {
my $dberror = PutImage( $biblionumber, $srcimage, $replace );
if ($dberror) {
undef $srcimage;
}
else {
- my $filename = $upload->{path};
+ my $filename = $upload->full_path;
my $dirname = File::Temp::tempdir( CLEANUP => 1 );
unless ( system( "unzip", $filename, '-d', $dirname ) == 0 ) {
$error = 'UZIPFAIL';
use C4::Auth;
use C4::Output;
use Koha::Upload;
+use Koha::UploadedFiles;
my $input = CGI::->new;
my $op = $input->param('op') // 'new';
plugin => $plugin,
);
-my $upar = $plugin ? { public => 1 } : {};
if ( $op eq 'new' ) {
$template->param(
mode => 'new',
output_html_with_http_headers $input, $cookie, $template->output;
} elsif ( $op eq 'search' ) {
- my $h = $id ? { id => $id } : { term => $term };
- my @uploads = Koha::Upload->new($upar)->get($h);
+ my $uploads;
+ if( $id ) {
+ my $rec = Koha::UploadedFiles->search({
+ id => $id,
+ $plugin? ( public => 1 ) : (),
+ })->next;
+ push @$uploads, $rec->unblessed if $rec;
+ } else {
+ $uploads = Koha::UploadedFiles->search_term({
+ term => $term,
+ $plugin? (): ( include_private => 1 ),
+ })->unblessed;
+ }
+
$template->param(
mode => 'report',
msg => $msg,
- uploads => \@uploads,
+ uploads => $uploads,
);
output_html_with_http_headers $input, $cookie, $template->output;
output_html_with_http_headers $input, $cookie, $template->output;
} elsif ( $op eq 'download' ) {
- my $upl = Koha::Upload->new($upar);
- my $rec = $upl->get( { id => $id, filehandle => 1 } );
- my $fh = $rec->{fh};
+ my $rec = Koha::UploadedFiles->search({
+ id => $id,
+ $plugin? ( public => 1 ) : (),
+ })->next;
+ my $fh = $rec? $rec->file_handle: undef;
if ( !$rec || !$fh ) {
$template->param(
mode => 'new',
msg => JSON::to_json( { $id => 5 } ),
- uploadcategories => $upl->getCategories,
+ uploadcategories => Koha::Upload->getCategories,
);
output_html_with_http_headers $input, $cookie, $template->output;
} else {
- my @hdr = $upl->httpheaders( $rec->{name} );
+ my @hdr = Koha::Upload->httpheaders( $rec->filename );
print Encode::encode_utf8( $input->header(@hdr) );
while (<$fh>) {
print $_;