View | Details | Raw Unified | Return to bug 17501
Collapse All | Expand All

(-)a/C4/HTML5Media.pm (-5 / +7 lines)
Lines 22-28 use warnings; Link Here
22
22
23
use C4::Context;
23
use C4::Context;
24
use MARC::Field;
24
use MARC::Field;
25
use Koha::Upload;
25
use Koha::UploadedFiles;
26
26
27
=head1 HTML5Media
27
=head1 HTML5Media
28
28
Lines 134-143 sub gethtml5media { Link Here
134
        if ( $HTML5Media{srcblock} =~ /\Qopac-retrieve-file.pl\E/ ) {
134
        if ( $HTML5Media{srcblock} =~ /\Qopac-retrieve-file.pl\E/ ) {
135
            my ( undef, $id ) = split /id=/, $HTML5Media{srcblock};
135
            my ( undef, $id ) = split /id=/, $HTML5Media{srcblock};
136
            next if !$id;
136
            next if !$id;
137
            my $public = ( ( caller )[1] =~ /opac/ ) ? { public => 1 }: {};
137
            my %public = ( ( caller )[1] =~ /opac/ ) ? ( public => 1 ): ();
138
            my $upl = Koha::Upload->new( $public )->get({ hashvalue => $id });
138
            my $upload = Koha::UploadedFiles->search({
139
            next if !$upl || $upl->{name} !~ /\./;
139
                hashvalue => $id, %public,
140
            $HTML5Media{extension} = ( $upl->{name} =~ m/([^.]+)$/ )[0];
140
            })->next;
141
            next if !$upload || $upload->filename !~ /\./;
142
            $HTML5Media{extension} = ( $upload->filename =~ m/([^.]+)$/ )[0];
141
        }
143
        }
142
        # check remote files
144
        # check remote files
143
        else {
145
        else {
(-)a/Koha/Upload.pm (-82 / +12 lines)
Lines 26-31 Koha::Upload - Facilitate file uploads (temporary and permanent) Link Here
26
=head1 SYNOPSIS
26
=head1 SYNOPSIS
27
27
28
    use Koha::Upload;
28
    use Koha::Upload;
29
    use Koha::UploadedFiles;
29
30
30
    # add an upload (see tools/upload-file.pl)
31
    # add an upload (see tools/upload-file.pl)
31
    # the public flag allows retrieval via OPAC
32
    # the public flag allows retrieval via OPAC
Lines 34-47 Koha::Upload - Facilitate file uploads (temporary and permanent) Link Here
34
    # Do something with $upload->count, $upload->result or $upload->err
35
    # Do something with $upload->count, $upload->result or $upload->err
35
36
36
    # get some upload records (in staff)
37
    # get some upload records (in staff)
37
    # Note: use the public flag for OPAC
38
    my @uploads1 = Koha::UploadedFiles->search({ filename => $name });
38
    my @uploads = Koha::Upload->new->get( term => $term );
39
    my @uploads2 = Koha::UploadedFiles->search_term({ term => $term });
39
    $template->param( uploads => \@uploads );
40
40
41
    # staff download
41
    # staff download
42
    my $rec = Koha::Upload->new->get({ id => $id, filehandle => 1 });
42
    my $rec = Koha::UploadedFiles->find( $id );
43
    my $fh = $rec->{fh};
43
    my $fh = $rec->file_handle;
44
    my @hdr = Koha::Upload->httpheaders( $rec->{name} );
44
    my @hdr = Koha::Upload->httpheaders( $rec->filename );
45
    print Encode::encode_utf8( $input->header( @hdr ) );
45
    print Encode::encode_utf8( $input->header( @hdr ) );
46
    while( <$fh> ) { print $_; }
46
    while( <$fh> ) { print $_; }
47
    $fh->close;
47
    $fh->close;
Lines 54-62 Koha::Upload - Facilitate file uploads (temporary and permanent) Link Here
54
    functionality of both.
54
    functionality of both.
55
55
56
    The module has been revised to use Koha::Object[s]; the delete method
56
    The module has been revised to use Koha::Object[s]; the delete method
57
    has been moved to Koha::UploadedFile[s].
57
    has been moved to Koha::UploadedFile[s], as well as the get method.
58
58
59
=head1 METHODS
59
=head1 INSTANCE METHODS
60
60
61
=cut
61
=cut
62
62
Lines 158-197 sub err { Link Here
158
    return $err;
158
    return $err;
159
}
159
}
160
160
161
=head2 get
162
163
    Returns arrayref of uploaded records (hash) or one uploaded record.
164
    You can pass id => $id or hashvalue => $hash or term => $term.
165
    Optional parameter filehandle => 1 returns you a filehandle too.
166
167
=cut
168
169
sub get {
170
    my ( $self, $params ) = @_;
171
    my $temp= $self->_lookup( $params );
172
    my ( @rv, $res);
173
    foreach my $r ( @$temp ) {
174
        undef $res;
175
        foreach( qw[id hashvalue filesize uploadcategorycode public permanent owner] ) {
176
            $res->{$_} = $r->{$_};
177
        }
178
        $res->{name} = $r->{filename};
179
        $res->{path} = $self->_full_fname($r);
180
        if( $res->{path} && -r $res->{path} ) {
181
            if( $params->{filehandle} ) {
182
                my $fh = IO::File->new( $res->{path}, "r" );
183
                $fh->binmode if $fh;
184
                $res->{fh} = $fh;
185
            }
186
            push @rv, $res;
187
        } else {
188
            $self->{files}->{ $r->{filename} }->{errcode}=5; #not readable
189
        }
190
        last if !wantarray;
191
    }
192
    return wantarray? @rv: $res;
193
}
194
195
=head1 CLASS METHODS
161
=head1 CLASS METHODS
196
162
197
=head2 getCategories
163
=head2 getCategories
Lines 292-301 sub _create_file { Link Here
292
        # if the file exists and it is registered, then set error
258
        # if the file exists and it is registered, then set error
293
        # if it exists, but is not in the database, we will overwrite
259
        # if it exists, but is not in the database, we will overwrite
294
        if( -e "$dir/$fn" &&
260
        if( -e "$dir/$fn" &&
295
            Koha::UploadedFiles->search({
261
        Koha::UploadedFiles->search({
296
                hashvalue          => $hashval,
262
            hashvalue          => $hashval,
297
                uploadcategorycode => $self->{category},
263
            uploadcategorycode => $self->{category},
298
            })->count ) {
264
        })->count ) {
299
            $self->{files}->{$filename}->{errcode} = 1; #already exists
265
            $self->{files}->{$filename}->{errcode} = 1; #already exists
300
            return;
266
            return;
301
        }
267
        }
Lines 319-337 sub _dir { Link Here
319
    return $dir;
285
    return $dir;
320
}
286
}
321
287
322
sub _full_fname {
323
    my ( $self, $rec ) = @_;
324
    my $p;
325
    if( ref $rec ) {
326
        $p = File::Spec->catfile(
327
            $rec->{permanent}? $self->{rootdir}: $self->{tmpdir},
328
            $rec->{dir},
329
            $rec->{hashvalue}. '_'. $rec->{filename}
330
        );
331
    }
332
    return $p;
333
}
334
335
sub _hook {
288
sub _hook {
336
    my ( $self, $filename, $buffer, $bytes_read, $data ) = @_;
289
    my ( $self, $filename, $buffer, $bytes_read, $data ) = @_;
337
    $filename= Encode::decode_utf8( $filename ); # UTF8 chars in filename
290
    $filename= Encode::decode_utf8( $filename ); # UTF8 chars in filename
Lines 366-394 sub _register { Link Here
366
    $self->{files}->{$filename}->{id} = $rec->id if $rec;
319
    $self->{files}->{$filename}->{id} = $rec->id if $rec;
367
}
320
}
368
321
369
sub _lookup {
370
    my ( $self, $params ) = @_;
371
372
    my ( $cond, $attr, %pubhash );
373
    %pubhash = $self->{public}? ( public => 1 ): ();
374
    if( $params->{id} ) {
375
        return [] if $params->{id} !~ /^\d+(,\d+)*$/;
376
        $cond = { id => [ split ',', $params->{id} ], %pubhash };
377
    } elsif( $params->{hashvalue} ) {
378
        $cond = { hashvalue => $params->{hashvalue}, %pubhash };
379
    } elsif( $params->{term} ) {
380
        $cond =
381
            [ { filename => { like => '%'.$params->{term}.'%' }, %pubhash },
382
              { hashvalue => { like => '%'.$params->{term}.'%' }, %pubhash } ];
383
    } else {
384
        return [];
385
    }
386
    $attr = { order_by => { -asc => 'id' }};
387
388
    return Koha::UploadedFiles->search( $cond, $attr )->unblessed;
389
    # Does always return an arrayref (perhaps an empty one)
390
}
391
392
sub _compute {
322
sub _compute {
393
# Computes hash value when sub hook feeds the first block
323
# Computes hash value when sub hook feeds the first block
394
# For temporary files, the id is made unique with time
324
# For temporary files, the id is made unique with time
(-)a/Koha/UploadedFile.pm (-3 / +20 lines)
Lines 43-61 Description Link Here
43
=head3 delete
43
=head3 delete
44
44
45
Delete uploaded file.
45
Delete uploaded file.
46
It deletes not only the record, but also the actual file.
46
It deletes not only the record, but also the actual file (unless you pass
47
the keep_file parameter).
47
48
48
Returns filename on successful delete or undef.
49
Returns filename on successful delete or undef.
49
50
50
=cut
51
=cut
51
52
52
sub delete {
53
sub delete {
53
    my ( $self ) = @_;
54
    my ( $self, $params ) = @_;
54
55
55
    my $name = $self->filename;
56
    my $name = $self->filename;
56
    my $file = $self->full_path;
57
    my $file = $self->full_path;
57
58
58
    if( !-e $file ) { # we will just delete the record
59
    if( $params->{keep_file} ) {
60
        return $name if $self->SUPER::delete;
61
    } elsif( !-e $file ) { # we will just delete the record
59
        warn "Removing record for $name within category ".
62
        warn "Removing record for $name within category ".
60
            $self->uploadcategorycode. ", but file was missing.";
63
            $self->uploadcategorycode. ", but file was missing.";
61
        return $name if $self->SUPER::delete;
64
        return $name if $self->SUPER::delete;
Lines 84-89 sub full_path { Link Here
84
    return $path;
87
    return $path;
85
}
88
}
86
89
90
=head3 file_handle
91
92
Returns a file handle for an uploaded file.
93
94
=cut
95
96
sub file_handle {
97
    my ( $self ) = @_;
98
    $self->{_file_handle} = IO::File->new( $self->full_path, "r" );
99
    return if !$self->{_file_handle};
100
    $self->{_file_handle}->binmode;
101
    return $self->{_file_handle};
102
}
103
87
=head2 CLASS METHODS
104
=head2 CLASS METHODS
88
105
89
=head3 root_directory
106
=head3 root_directory
(-)a/Koha/UploadedFiles.pm (-2 / +27 lines)
Lines 46-60 Delete uploaded files. Link Here
46
Returns true if no errors occur.
46
Returns true if no errors occur.
47
Delete_errors returns the number of errors when deleting files.
47
Delete_errors returns the number of errors when deleting files.
48
48
49
Parameter keep_file may be used to delete records, but keep files.
50
49
=cut
51
=cut
50
52
51
sub delete {
53
sub delete {
52
    my ( $self ) = @_;
54
    my ( $self, $params ) = @_;
53
    # We use the individual delete on each resultset record
55
    # We use the individual delete on each resultset record
54
    my $err = 0;
56
    my $err = 0;
55
    while( my $row = $self->_resultset->next ) {
57
    while( my $row = $self->_resultset->next ) {
56
        my $kohaobj = Koha::UploadedFile->_new_from_dbic( $row );
58
        my $kohaobj = Koha::UploadedFile->_new_from_dbic( $row );
57
        $err++ if !$kohaobj->delete;
59
        $err++ if !$kohaobj->delete( $params );
58
    }
60
    }
59
    $self->{delete_errors} = $err;
61
    $self->{delete_errors} = $err;
60
    return $err==0;
62
    return $err==0;
Lines 65-70 sub delete_errors { Link Here
65
    return $self->{delete_errors};
67
    return $self->{delete_errors};
66
}
68
}
67
69
70
=head3 search_term
71
72
Search_term allows you to pass a term to search in filename and hashvalue.
73
If you do not pass include_private, only public records are returned.
74
75
Is only a wrapper around Koha::Objects search. Has similar return value.
76
77
=cut
78
79
sub search_term {
80
    my ( $self, $params ) = @_;
81
    my $term = $params->{term} // '';
82
    my %public = ();
83
    if( !$params->{include_private} ) {
84
        %public = ( public => 1 );
85
    }
86
    return $self->search(
87
        [ { filename => { like => '%'.$term.'%' }, %public },
88
          { hashvalue => { like => '%'.$params->{term}.'%' }, %public } ],
89
        { order_by => { -asc => 'id' }},
90
    );
91
}
92
68
=head2 CLASS METHODS
93
=head2 CLASS METHODS
69
94
70
=head3 _type
95
=head3 _type
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/upload.tt (-1 / +1 lines)
Lines 162-168 Link Here
162
    <tbody>
162
    <tbody>
163
    [% FOREACH record IN uploads %]
163
    [% FOREACH record IN uploads %]
164
    <tr>
164
    <tr>
165
        <td>[% record.name %]</td>
165
        <td>[% record.filename %]</td>
166
        <td>[% record.filesize %]</td>
166
        <td>[% record.filesize %]</td>
167
        <td>[% record.hashvalue %]</td>
167
        <td>[% record.hashvalue %]</td>
168
        <td>[% record.uploadcategorycode %]</td>
168
        <td>[% record.uploadcategorycode %]</td>
(-)a/offline_circ/enqueue_koc.pl (-4 / +5 lines)
Lines 32-38 use C4::Circulation; Link Here
32
use C4::Items;
32
use C4::Items;
33
use C4::Members;
33
use C4::Members;
34
use C4::Stats;
34
use C4::Stats;
35
use Koha::Upload;
35
use Koha::UploadedFiles;
36
36
37
use Date::Calc qw( Add_Delta_Days Date_to_Days );
37
use Date::Calc qw( Add_Delta_Days Date_to_Days );
38
38
Lines 60-68 my $sessionID = $cookies{'CGISESSID'}->value; Link Here
60
our $dbh = C4::Context->dbh();
60
our $dbh = C4::Context->dbh();
61
61
62
if ($fileID) {
62
if ($fileID) {
63
    my $upload = Koha::Upload->new->get({ id => $fileID, filehandle => 1 });
63
    my $upload = Koha::UploadedFiles->find($fileID);
64
    my $fh = $upload->{fh};
64
    my $fh = $upload? $upload->file_handle: undef;
65
    my @input_lines = <$fh>;
65
    my @input_lines = $fh? <$fh>: ();
66
    $fh->close if $fh;
66
67
67
    my $header_line = shift @input_lines;
68
    my $header_line = shift @input_lines;
68
    my $file_info   = parse_header_line($header_line);
69
    my $file_info   = parse_header_line($header_line);
(-)a/offline_circ/process_koc.pl (-5 / +6 lines)
Lines 35-41 use C4::Items; Link Here
35
use C4::Members;
35
use C4::Members;
36
use C4::Stats;
36
use C4::Stats;
37
use C4::BackgroundJob;
37
use C4::BackgroundJob;
38
use Koha::Upload;
38
use Koha::UploadedFiles;
39
use Koha::Account;
39
use Koha::Account;
40
use Koha::Patrons;
40
use Koha::Patrons;
41
41
Lines 73-82 if ($completedJobID) { Link Here
73
    $template->param(transactions_loaded => 1);
73
    $template->param(transactions_loaded => 1);
74
    $template->param(messages => $results->{results});
74
    $template->param(messages => $results->{results});
75
} elsif ($fileID) {
75
} elsif ($fileID) {
76
    my $upload = Koha::Upload->new->get({ id => $fileID, filehandle => 1 });
76
    my $upload = Koha::UploadedFiles->find( $fileID );
77
    my $fh = $upload->{fh};
77
    my $fh = $upload? $upload->file_handle: undef;
78
    my $filename = $upload->{name};
78
    my $filename = $upload? $upload->filename: undef;
79
    my @input_lines = <$fh>;
79
    my @input_lines = $fh? <$fh>: ();
80
    $fh->close if $fh;
80
81
81
    my $job = undef;
82
    my $job = undef;
82
83
(-)a/opac/opac-retrieve-file.pl (-4 / +8 lines)
Lines 25-37 use C4::Auth; Link Here
25
use C4::Context;
25
use C4::Context;
26
use C4::Output;
26
use C4::Output;
27
use Koha::Upload;
27
use Koha::Upload;
28
use Koha::UploadedFiles;
28
29
29
my $input = CGI::->new;
30
my $input = CGI::->new;
30
my $hash = $input->param('id'); # historically called id (used in URLs?)
31
my $hash = $input->param('id'); # historically called id (used in URLs?)
31
32
32
my $upl = Koha::Upload->new({ public => 1 });
33
my $rec = Koha::UploadedFiles->search({
33
my $rec = $upl->get({ hashvalue => $hash, filehandle => 1 });
34
    hashvalue => $hash, public => 1,
34
my $fh = $rec->{fh};
35
    # DO NOT REMOVE the public flag: this is an opac script !
36
})->next;
37
my $fh = $rec? $rec->file_handle: undef;
38
35
if( !$rec || !$fh ) {
39
if( !$rec || !$fh ) {
36
    my ( $template, $user, $cookie ) = get_template_and_user({
40
    my ( $template, $user, $cookie ) = get_template_and_user({
37
        query           => $input,
41
        query           => $input,
Lines 42-48 if( !$rec || !$fh ) { Link Here
42
    $template->param( hash => $hash );
46
    $template->param( hash => $hash );
43
    output_html_with_http_headers $input, $cookie, $template->output;
47
    output_html_with_http_headers $input, $cookie, $template->output;
44
} else {
48
} else {
45
    my @hdr = $upl->httpheaders( $rec->{name} );
49
    my @hdr = Koha::Upload->httpheaders( $rec->filename );
46
    print Encode::encode_utf8( $input->header( @hdr ) );
50
    print Encode::encode_utf8( $input->header( @hdr ) );
47
    while( <$fh> ) {
51
    while( <$fh> ) {
48
        print $_;
52
        print $_;
(-)a/t/db_dependent/Upload.t (-35 / +44 lines)
Lines 15-21 use Koha::UploadedFiles; Link Here
15
15
16
my $schema  = Koha::Database->new->schema;
16
my $schema  = Koha::Database->new->schema;
17
$schema->storage->txn_begin;
17
$schema->storage->txn_begin;
18
my $dbh = C4::Context->dbh;
19
18
20
our $current_upload = 0;
19
our $current_upload = 0;
21
our $uploads = [
20
our $uploads = [
Lines 37-43 our $uploads = [ Link Here
37
        { name => 'file4', cat => undef, size => 5000 }, # temp duplicate
36
        { name => 'file4', cat => undef, size => 5000 }, # temp duplicate
38
    ],
37
    ],
39
    [
38
    [
40
        { name => 'file5', cat => undef, size => 7000 }, # temp duplicate
39
        { name => 'file5', cat => undef, size => 7000 },
41
    ],
40
    ],
42
];
41
];
43
42
Lines 51-61 $cgimod->mock( 'new' => \&newCGI ); Link Here
51
50
52
# Start testing
51
# Start testing
53
subtest 'Test01' => sub {
52
subtest 'Test01' => sub {
54
    plan tests => 9;
53
    plan tests => 11;
55
    test01();
54
    test01();
56
};
55
};
57
subtest 'Test02' => sub {
56
subtest 'Test02' => sub {
58
    plan tests => 4;
57
    plan tests => 5;
59
    test02();
58
    test02();
60
};
59
};
61
subtest 'Test03' => sub {
60
subtest 'Test03' => sub {
Lines 71-77 subtest 'Test05' => sub { Link Here
71
    test05();
70
    test05();
72
};
71
};
73
subtest 'Test06' => sub {
72
subtest 'Test06' => sub {
74
    plan tests => 2;
73
    plan tests => 3;
75
    test06();
74
    test06();
76
};
75
};
77
subtest 'Test07' => sub {
76
subtest 'Test07' => sub {
Lines 86-92 $schema->storage->txn_rollback; Link Here
86
85
87
sub test01 {
86
sub test01 {
88
    # Delete existing records (for later tests)
87
    # Delete existing records (for later tests)
89
    $dbh->do( "DELETE FROM uploaded_files" );
88
    # Passing keep_file suppresses warnings
89
    Koha::UploadedFiles->new->delete({ keep_file => 1 });
90
90
91
    # Check mocked directories
91
    # Check mocked directories
92
    is( Koha::UploadedFile->permanent_directory, $tempdir,
92
    is( Koha::UploadedFile->permanent_directory, $tempdir,
Lines 101-116 sub test01 { Link Here
101
    my $res= $upl->result;
101
    my $res= $upl->result;
102
    is( $res =~ /^\d+,\d+$/, 1, 'Upload 1 includes two files' );
102
    is( $res =~ /^\d+,\d+$/, 1, 'Upload 1 includes two files' );
103
    is( $upl->count, 2, 'Count returns 2 also' );
103
    is( $upl->count, 2, 'Count returns 2 also' );
104
    foreach my $r ( $upl->get({ id => $res }) ) {
105
        if( $r->{name} eq 'file1' ) {
106
            is( $r->{uploadcategorycode}, 'A', 'Check category A' );
107
            is( $r->{filesize}, 6000, 'Check size of file1' );
108
        } elsif( $r->{name} eq 'file2' ) {
109
            is( $r->{filesize}, 8000, 'Check size of file2' );
110
            is( $r->{public}, undef, 'Check public undefined' );
111
        }
112
    }
113
    is( $upl->err, undef, 'No errors reported' );
104
    is( $upl->err, undef, 'No errors reported' );
105
106
    my $rs = Koha::UploadedFiles->search({
107
        id => [ split ',', $res ]
108
    }, { order_by => { -asc => 'filename' }});
109
    my $rec = $rs->next;
110
    is( $rec->filename, 'file1', 'Check file name' );
111
    is( $rec->uploadcategorycode, 'A', 'Check category A' );
112
    is( $rec->filesize, 6000, 'Check size of file1' );
113
    $rec = $rs->next;
114
    is( $rec->filename, 'file2', 'Check file name 2' );
115
    is( $rec->filesize, 8000, 'Check size of file2' );
116
    is( $rec->public, undef, 'Check public undefined' );
114
}
117
}
115
118
116
sub test02 {
119
sub test02 {
Lines 121-138 sub test02 { Link Here
121
    my $cgi= $upl->cgi;
124
    my $cgi= $upl->cgi;
122
    is( $upl->count, 1, 'Upload 2 includes one file' );
125
    is( $upl->count, 1, 'Upload 2 includes one file' );
123
    my $res= $upl->result;
126
    my $res= $upl->result;
124
    my $r = $upl->get({ id => $res, filehandle => 1 });
127
    my $rec = Koha::UploadedFiles->find( $res );
125
    is( $r->{uploadcategorycode}, 'B', 'Check category B' );
128
    is( $rec->uploadcategorycode, 'B', 'Check category B' );
126
    is( $r->{public}, 1, 'Check public == 1' );
129
    is( $rec->public, 1, 'Check public == 1' );
127
    is( ref($r->{fh}) eq 'IO::File' && $r->{fh}->opened, 1, 'Get returns a file handle' );
130
    my $fh = $rec->file_handle;
131
    is( ref($fh) eq 'IO::File' && $fh->opened, 1, 'Get returns a file handle' );
132
133
    my $orgname = $rec->filename;
134
    $rec->filename( 'doesprobablynotexist' )->store;
135
    is( $rec->file_handle, undef, 'Sabotage with file handle' );
136
    $rec->filename( $orgname )->store;
128
}
137
}
129
138
130
sub test03 {
139
sub test03 {
131
    my $upl = Koha::Upload->new({ tmp => 1 }); #temporary
140
    my $upl = Koha::Upload->new({ tmp => 1 }); #temporary
132
    my $cgi= $upl->cgi;
141
    my $cgi= $upl->cgi;
133
    is( $upl->count, 1, 'Upload 3 includes one temporary file' );
142
    is( $upl->count, 1, 'Upload 3 includes one temporary file' );
134
    my $r = $upl->get({ id => $upl->result });
143
    my $rec = Koha::UploadedFiles->find( $upl->result );
135
    is( $r->{uploadcategorycode} =~ /_upload$/, 1, 'Check category temp file' );
144
    is( $rec->uploadcategorycode =~ /_upload$/, 1, 'Check category temp file' );
136
}
145
}
137
146
138
sub test04 { # Fail on a file already there
147
sub test04 { # Fail on a file already there
Lines 151-184 sub test05 { # add temporary file with same name and contents, delete it Link Here
151
    my $cgi= $upl->cgi;
160
    my $cgi= $upl->cgi;
152
    is( $upl->count, 1, 'Upload 5 adds duplicate temporary file' );
161
    is( $upl->count, 1, 'Upload 5 adds duplicate temporary file' );
153
    my $id = $upl->result;
162
    my $id = $upl->result;
154
    my $r = $upl->get({ id => $id });
163
    my $path = Koha::UploadedFiles->find( $id )->full_path;
155
164
156
    # testing delete via UploadedFiles (plural)
165
    # testing delete via UploadedFiles (plural)
157
    my $delete = Koha::UploadedFiles->search({ id => $id })->delete;
166
    my $delete = Koha::UploadedFiles->search({ id => $id })->delete;
158
    is( $delete, 1, 'Delete successful' );
167
    is( $delete, 1, 'Delete successful' );
159
    isnt( -e $r->{path}, 1, 'File no longer found after delete' );
168
    isnt( -e $path, 1, 'File no longer found after delete' );
160
    is( scalar $upl->get({ id => $id }), undef, 'Record also gone' );
169
    is( Koha::UploadedFiles->find( $id ), undef, 'Record also gone' );
161
170
162
    # testing delete via UploadedFile (singular)
171
    # testing delete via UploadedFile (singular)
163
    # Note that find returns a Koha::Object
172
    # Note that find returns a Koha::Object
164
    $upl = Koha::Upload->new({ tmp => 1 });
173
    $upl = Koha::Upload->new({ tmp => 1 });
165
    $upl->cgi;
174
    $upl->cgi;
166
    $id = $upl->result;
175
    my $kohaobj = Koha::UploadedFiles->find( $upl->result );
167
    my $kohaobj = Koha::UploadedFiles->find( $id );
168
    my $name = $kohaobj->filename;
176
    my $name = $kohaobj->filename;
169
    my $path = $kohaobj->full_path;
177
    $path = $kohaobj->full_path;
170
    $delete = $kohaobj->delete;
178
    $delete = $kohaobj->delete;
171
    is( $delete, $name, 'Delete successful' );
179
    is( $delete, $name, 'Delete successful' );
172
    isnt( -e $path, 1, 'File no longer found after delete' );
180
    isnt( -e $path, 1, 'File no longer found after delete' );
173
}
181
}
174
182
175
sub test06 { #some extra tests for get
183
sub test06 { #search_term with[out] private flag
176
    my $upl = Koha::Upload->new({ public => 1 });
184
    my @recs = Koha::UploadedFiles->search_term({ term => 'file' });
177
    my @rec = $upl->get({ term => 'file' });
185
    is( @recs, 1, 'Returns only one public result' );
178
    is( @rec, 1, 'Get returns only one public result (file3)' );
186
    is( $recs[0]->filename, 'file3', 'Should be file3' );
179
    $upl = Koha::Upload->new; # public == 0
187
180
    @rec = $upl->get({ term => 'file' });
188
    is( Koha::UploadedFiles->search_term({
181
    is( @rec, 4, 'Get returns now four results' );
189
        term => 'file', include_private => 1,
190
    })->count, 4, 'Returns now four results' );
182
}
191
}
183
192
184
sub test07 { #simple test for httpheaders and getCategories
193
sub test07 { #simple test for httpheaders and getCategories
Lines 237-243 subtest 'Some basic CRUD testing' => sub { Link Here
237
    my $upload01 = $builder->build({ source => 'UploadedFile' });
246
    my $upload01 = $builder->build({ source => 'UploadedFile' });
238
    my $found = Koha::UploadedFiles->find( $upload01->{id} );
247
    my $found = Koha::UploadedFiles->find( $upload01->{id} );
239
    is( $found->id, $upload01->{id}, 'Koha::Object returns id' );
248
    is( $found->id, $upload01->{id}, 'Koha::Object returns id' );
240
    $found->delete;
249
    $found->delete({ keep_file => 1 }); #note that it does not exist
241
    $found = Koha::UploadedFiles->search(
250
    $found = Koha::UploadedFiles->search(
242
        { id => $upload01->{id} },
251
        { id => $upload01->{id} },
243
    );
252
    );
(-)a/tools/stage-marc-import.pl (-3 / +5 lines)
Lines 39-45 use C4::Output; Link Here
39
use C4::Biblio;
39
use C4::Biblio;
40
use C4::ImportBatch;
40
use C4::ImportBatch;
41
use C4::Matcher;
41
use C4::Matcher;
42
use Koha::Upload;
42
use Koha::UploadedFiles;
43
use C4::BackgroundJob;
43
use C4::BackgroundJob;
44
use C4::MarcModificationTemplates;
44
use C4::MarcModificationTemplates;
45
use Koha::Plugins;
45
use Koha::Plugins;
Lines 85-92 if ($completedJobID) { Link Here
85
    my $results = $job->results();
85
    my $results = $job->results();
86
    $template->param(map { $_ => $results->{$_} } keys %{ $results });
86
    $template->param(map { $_ => $results->{$_} } keys %{ $results });
87
} elsif ($fileID) {
87
} elsif ($fileID) {
88
    my $upload = Koha::Upload->new->get({ id => $fileID });
88
    my $upload = Koha::UploadedFiles->find( $fileID );
89
    my ( $file, $filename ) = ( $upload->{path}, $upload->{name} );
89
    my $file = $upload->full_path;
90
    my $filename = $upload->filename;
91
90
    my ( $errors, $marcrecords );
92
    my ( $errors, $marcrecords );
91
    if( $format eq 'MARCXML' ) {
93
    if( $format eq 'MARCXML' ) {
92
        ( $errors, $marcrecords ) = C4::ImportBatch::RecordsFromMARCXMLFile( $file, $encoding);
94
        ( $errors, $marcrecords ) = C4::ImportBatch::RecordsFromMARCXMLFile( $file, $encoding);
(-)a/tools/upload-cover-image.pl (-5 / +6 lines)
Lines 47-53 use C4::Context; Link Here
47
use C4::Auth;
47
use C4::Auth;
48
use C4::Output;
48
use C4::Output;
49
use C4::Images;
49
use C4::Images;
50
use Koha::Upload;
50
use Koha::UploadedFiles;
51
use C4::Log;
51
use C4::Log;
52
52
53
my $debug = 1;
53
my $debug = 1;
Lines 68-74 my ( $template, $loggedinuser, $cookie ) = get_template_and_user( Link Here
68
68
69
my $filetype       = $input->param('filetype');
69
my $filetype       = $input->param('filetype');
70
my $biblionumber   = $input->param('biblionumber');
70
my $biblionumber   = $input->param('biblionumber');
71
my $uploadfilename = $input->param('uploadfile');
71
#my $uploadfilename = $input->param('uploadfile'); # obsolete?
72
my $replace        = !C4::Context->preference("AllowMultipleCovers")
72
my $replace        = !C4::Context->preference("AllowMultipleCovers")
73
  || $input->param('replace');
73
  || $input->param('replace');
74
my $op        = $input->param('op');
74
my $op        = $input->param('op');
Lines 83-92 $template->{VARS}->{'biblionumber'} = $biblionumber; Link Here
83
my $total = 0;
83
my $total = 0;
84
84
85
if ($fileID) {
85
if ($fileID) {
86
    my $upload = Koha::Upload->new->get({ id => $fileID, filehandle => 1 });
86
    my $upload = Koha::UploadedFiles->find( $fileID );
87
    if ( $filetype eq 'image' ) {
87
    if ( $filetype eq 'image' ) {
88
        my $fh       = $upload->{fh};
88
        my $fh       = $upload->file_handle;
89
        my $srcimage = GD::Image->new($fh);
89
        my $srcimage = GD::Image->new($fh);
90
        $fh->close if $fh;
90
        if ( defined $srcimage ) {
91
        if ( defined $srcimage ) {
91
            my $dberror = PutImage( $biblionumber, $srcimage, $replace );
92
            my $dberror = PutImage( $biblionumber, $srcimage, $replace );
92
            if ($dberror) {
93
            if ($dberror) {
Lines 102-108 if ($fileID) { Link Here
102
        undef $srcimage;
103
        undef $srcimage;
103
    }
104
    }
104
    else {
105
    else {
105
        my $filename = $upload->{path};
106
        my $filename = $upload->full_path;
106
        my $dirname = File::Temp::tempdir( CLEANUP => 1 );
107
        my $dirname = File::Temp::tempdir( CLEANUP => 1 );
107
        unless ( system( "unzip", $filename, '-d', $dirname ) == 0 ) {
108
        unless ( system( "unzip", $filename, '-d', $dirname ) == 0 ) {
108
            $error = 'UZIPFAIL';
109
            $error = 'UZIPFAIL';
(-)a/tools/upload.pl (-10 / +23 lines)
Lines 24-29 use JSON; Link Here
24
use C4::Auth;
24
use C4::Auth;
25
use C4::Output;
25
use C4::Output;
26
use Koha::Upload;
26
use Koha::Upload;
27
use Koha::UploadedFiles;
27
28
28
my $input  = CGI::->new;
29
my $input  = CGI::->new;
29
my $op     = $input->param('op') // 'new';
30
my $op     = $input->param('op') // 'new';
Lines 48-54 $template->param( Link Here
48
    plugin     => $plugin,
49
    plugin     => $plugin,
49
);
50
);
50
51
51
my $upar = $plugin ? { public => 1 } : {};
52
if ( $op eq 'new' ) {
52
if ( $op eq 'new' ) {
53
    $template->param(
53
    $template->param(
54
        mode             => 'new',
54
        mode             => 'new',
Lines 57-68 if ( $op eq 'new' ) { Link Here
57
    output_html_with_http_headers $input, $cookie, $template->output;
57
    output_html_with_http_headers $input, $cookie, $template->output;
58
58
59
} elsif ( $op eq 'search' ) {
59
} elsif ( $op eq 'search' ) {
60
    my $h = $id ? { id => $id } : { term => $term };
60
    my $uploads;
61
    my @uploads = Koha::Upload->new($upar)->get($h);
61
    if( $id ) {
62
        my $rec = Koha::UploadedFiles->search({
63
            id => $id,
64
            $plugin? ( public => 1 ) : (),
65
        })->next;
66
        push @$uploads, $rec->unblessed if $rec;
67
    } else {
68
        $uploads = Koha::UploadedFiles->search_term({
69
            term => $term,
70
            $plugin? (): ( include_private => 1 ),
71
        })->unblessed;
72
    }
73
62
    $template->param(
74
    $template->param(
63
        mode    => 'report',
75
        mode    => 'report',
64
        msg     => $msg,
76
        msg     => $msg,
65
        uploads => \@uploads,
77
        uploads => $uploads,
66
    );
78
    );
67
    output_html_with_http_headers $input, $cookie, $template->output;
79
    output_html_with_http_headers $input, $cookie, $template->output;
68
80
Lines 86-103 if ( $op eq 'new' ) { Link Here
86
    output_html_with_http_headers $input, $cookie, $template->output;
98
    output_html_with_http_headers $input, $cookie, $template->output;
87
99
88
} elsif ( $op eq 'download' ) {
100
} elsif ( $op eq 'download' ) {
89
    my $upl = Koha::Upload->new($upar);
101
    my $rec = Koha::UploadedFiles->search({
90
    my $rec = $upl->get( { id => $id, filehandle => 1 } );
102
        id => $id,
91
    my $fh  = $rec->{fh};
103
        $plugin? ( public => 1 ) : (),
104
    })->next;
105
    my $fh  = $rec? $rec->file_handle:  undef;
92
    if ( !$rec || !$fh ) {
106
    if ( !$rec || !$fh ) {
93
        $template->param(
107
        $template->param(
94
            mode             => 'new',
108
            mode             => 'new',
95
            msg              => JSON::to_json( { $id => 5 } ),
109
            msg              => JSON::to_json( { $id => 5 } ),
96
            uploadcategories => $upl->getCategories,
110
            uploadcategories => Koha::Upload->getCategories,
97
        );
111
        );
98
        output_html_with_http_headers $input, $cookie, $template->output;
112
        output_html_with_http_headers $input, $cookie, $template->output;
99
    } else {
113
    } else {
100
        my @hdr = $upl->httpheaders( $rec->{name} );
114
        my @hdr = Koha::Upload->httpheaders( $rec->filename );
101
        print Encode::encode_utf8( $input->header(@hdr) );
115
        print Encode::encode_utf8( $input->header(@hdr) );
102
        while (<$fh>) {
116
        while (<$fh>) {
103
            print $_;
117
            print $_;
104
- 

Return to bug 17501