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

(-)a/Koha/UploadedFile.pm (-20 / +7 lines)
Lines 71-88 sub delete { Link Here
71
    my $name = $self->filename;
71
    my $name = $self->filename;
72
    my $file = $self->full_path;
72
    my $file = $self->full_path;
73
73
74
    if( $params->{keep_file} ) {
74
    my $retval = $self->SUPER::delete;
75
        return $name if $self->SUPER::delete;
75
    return $retval if $params->{keep_file};
76
    } elsif( !-e $file ) { # we will just delete the record
76
77
    if( ! -e $file ) {
77
        warn "Removing record for $name within category ".
78
        warn "Removing record for $name within category ".
78
            $self->uploadcategorycode. ", but file was missing.";
79
            $self->uploadcategorycode. ", but file was missing.";
79
        return $name if $self->SUPER::delete;
80
    } elsif( ! unlink($file) ) {
80
    } elsif( unlink($file) ) {
81
        $retval = 0;
81
        return $name if $self->SUPER::delete;
82
    } else {
83
        warn "Problem while deleting: $file";
82
        warn "Problem while deleting: $file";
84
    }
83
    }
85
    return; # something went wrong
84
    return $retval;
86
}
85
}
87
86
88
=head3 full_path
87
=head3 full_path
Lines 156-173 sub temporary_directory { Link Here
156
    return File::Spec->tmpdir;
155
    return File::Spec->tmpdir;
157
}
156
}
158
157
159
=head3 getCategories
160
161
getCategories returns a list of upload category codes and names
162
163
=cut
164
165
sub getCategories {
166
    my ( $class ) = @_;
167
    my $cats = C4::Koha::GetAuthorisedValues('UPLOAD');
168
    [ map {{ code => $_->{authorised_value}, name => $_->{lib} }} @$cats ];
169
}
170
171
=head3 _type
158
=head3 _type
172
159
173
Returns name of corresponding DBIC resultset
160
Returns name of corresponding DBIC resultset
(-)a/Koha/UploadedFiles.pm (-10 / +16 lines)
Lines 19-24 package Koha::UploadedFiles; Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use C4::Koha;
22
use Koha::UploadedFile;
23
use Koha::UploadedFile;
23
24
24
use parent qw(Koha::Objects);
25
use parent qw(Koha::Objects);
Lines 38-44 Koha::UploadedFiles - Koha::Objects class for uploaded files Link Here
38
    my @uploads = Koha::UploadedFiles->search_term({ term => '.mrc' });
39
    my @uploads = Koha::UploadedFiles->search_term({ term => '.mrc' });
39
40
40
    # delete all uploads
41
    # delete all uploads
41
    Koha::UploadedFiles->new->delete;
42
    Koha::UploadedFiles->delete;
42
43
43
=head1 DESCRIPTION
44
=head1 DESCRIPTION
44
45
Lines 51-61 provides a wrapper around search to look for a term in multiple columns. Link Here
51
52
52
=head2 INSTANCE METHODS
53
=head2 INSTANCE METHODS
53
54
54
=head3 delete, delete_errors
55
=head3 delete
55
56
56
Delete uploaded files.
57
Delete uploaded files.
57
Returns true if no errors occur.
58
Returns true if no errors occur. (So, false may mean partial success.)
58
Delete_errors returns the number of errors when deleting files.
59
59
60
Parameter keep_file may be used to delete records, but keep files.
60
Parameter keep_file may be used to delete records, but keep files.
61
61
Lines 69-83 sub delete { Link Here
69
        my $kohaobj = Koha::UploadedFile->_new_from_dbic( $row );
69
        my $kohaobj = Koha::UploadedFile->_new_from_dbic( $row );
70
        $err++ if !$kohaobj->delete( $params );
70
        $err++ if !$kohaobj->delete( $params );
71
    }
71
    }
72
    $self->{delete_errors} = $err;
73
    return $err==0;
72
    return $err==0;
74
}
73
}
75
74
76
sub delete_errors {
77
    my ( $self ) = @_;
78
    return $self->{delete_errors};
79
}
80
81
=head3 search_term
75
=head3 search_term
82
76
83
Search_term allows you to pass a term to search in filename and hashvalue.
77
Search_term allows you to pass a term to search in filename and hashvalue.
Lines 103-108 sub search_term { Link Here
103
97
104
=head2 CLASS METHODS
98
=head2 CLASS METHODS
105
99
100
=head3 getCategories
101
102
getCategories returns a list of upload category codes and names
103
104
=cut
105
106
sub getCategories {
107
    my ( $class ) = @_;
108
    my $cats = C4::Koha::GetAuthorisedValues('UPLOAD');
109
    [ map {{ code => $_->{authorised_value}, name => $_->{lib} }} @$cats ];
110
}
111
106
=head3 _type
112
=head3 _type
107
113
108
Returns name of corresponding DBIC resultset
114
Returns name of corresponding DBIC resultset
(-)a/t/db_dependent/Upload.t (-4 / +3 lines)
Lines 59-65 subtest 'Make a fresh start' => sub { Link Here
59
    # Passing keep_file suppresses warnings (and does not delete files)
59
    # Passing keep_file suppresses warnings (and does not delete files)
60
    # Note that your files are not in danger, since we redirected
60
    # Note that your files are not in danger, since we redirected
61
    # all files to a new empty temp folder
61
    # all files to a new empty temp folder
62
    Koha::UploadedFiles->new->delete({ keep_file => 1 });
62
    Koha::UploadedFiles->delete({ keep_file => 1 });
63
    is( Koha::UploadedFiles->count, 0, 'No records left' );
63
    is( Koha::UploadedFiles->count, 0, 'No records left' );
64
};
64
};
65
65
Lines 164-173 subtest 'Test delete via UploadedFile as well as UploadedFiles' => sub { Link Here
164
    $upl = Koha::Uploader->new({ tmp => 1 });
164
    $upl = Koha::Uploader->new({ tmp => 1 });
165
    $upl->cgi;
165
    $upl->cgi;
166
    my $kohaobj = Koha::UploadedFiles->find( $upl->result );
166
    my $kohaobj = Koha::UploadedFiles->find( $upl->result );
167
    my $name = $kohaobj->filename;
168
    $path = $kohaobj->full_path;
167
    $path = $kohaobj->full_path;
169
    $delete = $kohaobj->delete;
168
    $delete = $kohaobj->delete;
170
    is( $delete, $name, 'Delete successful' );
169
    is( $delete, 1, 'Delete successful' );
171
    isnt( -e $path, 1, 'File no longer found after delete' );
170
    isnt( -e $path, 1, 'File no longer found after delete' );
172
171
173
    # add another record with TestBuilder, so file does not exist
172
    # add another record with TestBuilder, so file does not exist
Lines 198-204 subtest 'Simple tests for httpheaders and getCategories' => sub { Link Here
198
    my @hdrs = $rec->httpheaders;
197
    my @hdrs = $rec->httpheaders;
199
    is( @hdrs == 4 && $hdrs[1] =~ /application\/octet-stream/, 1, 'Simple test for httpheaders');
198
    is( @hdrs == 4 && $hdrs[1] =~ /application\/octet-stream/, 1, 'Simple test for httpheaders');
200
    $builder->build({ source => 'AuthorisedValue', value => { category => 'UPLOAD', authorised_value => 'HAVE_AT_LEAST_ONE', lib => 'Hi there' } });
199
    $builder->build({ source => 'AuthorisedValue', value => { category => 'UPLOAD', authorised_value => 'HAVE_AT_LEAST_ONE', lib => 'Hi there' } });
201
    my $cat = Koha::UploadedFile->getCategories;
200
    my $cat = Koha::UploadedFiles->getCategories;
202
    is( @$cat >= 1, 1, 'getCategories returned at least one category' );
201
    is( @$cat >= 1, 1, 'getCategories returned at least one category' );
203
};
202
};
204
203
(-)a/tools/upload.pl (-22 / +14 lines)
Lines 23-29 use JSON; Link Here
23
23
24
use C4::Auth;
24
use C4::Auth;
25
use C4::Output;
25
use C4::Output;
26
use Koha::UploadedFile;
27
use Koha::UploadedFiles;
26
use Koha::UploadedFiles;
28
27
29
my $input  = CGI::->new;
28
my $input  = CGI::->new;
Lines 47-68 $template->param( Link Here
47
    index      => $index,
46
    index      => $index,
48
    owner      => $loggedinuser,
47
    owner      => $loggedinuser,
49
    plugin     => $plugin,
48
    plugin     => $plugin,
49
    uploadcategories => Koha::UploadedFiles->getCategories,
50
);
50
);
51
51
52
if ( $op eq 'new' ) {
52
if ( $op eq 'new' ) {
53
    $template->param(
53
    $template->param(
54
        mode             => 'new',
54
        mode             => 'new',
55
        uploadcategories => Koha::UploadedFile->getCategories,
56
    );
55
    );
57
    output_html_with_http_headers $input, $cookie, $template->output;
56
    output_html_with_http_headers $input, $cookie, $template->output;
58
57
59
} elsif ( $op eq 'search' ) {
58
} elsif ( $op eq 'search' ) {
60
    my $uploads;
59
    my $uploads;
61
    if( $id ) {
60
    if( $id ) {
62
        my $rec = Koha::UploadedFiles->search({
61
        my $rec = Koha::UploadedFiles->find( $id );
63
            id => $id,
62
        undef $rec if $rec && $plugin && !$rec->public;
64
            $plugin? ( public => 1 ) : (),
65
        })->next;
66
        push @$uploads, $rec->unblessed if $rec;
63
        push @$uploads, $rec->unblessed if $rec;
67
    } else {
64
    } else {
68
        $uploads = Koha::UploadedFiles->search_term({
65
        $uploads = Koha::UploadedFiles->search_term({
Lines 80-113 if ( $op eq 'new' ) { Link Here
80
77
81
} elsif ( $op eq 'delete' ) {
78
} elsif ( $op eq 'delete' ) {
82
    # delete only takes the id parameter
79
    # delete only takes the id parameter
83
    my $upload = $plugin?
80
    my $rec = Koha::UploadedFiles->find($id);
84
         Koha::UploadedFiles->search({ public => 1, id => $id })->next:
81
    undef $rec if $rec && $plugin && !$rec->public;
85
         Koha::UploadedFiles->find($id);
82
    my $fn = $rec ? $rec->filename : '';
86
    my $fn = $upload? $upload->delete: undef;
83
    my $delete = $rec ? $rec->delete : undef;
87
    #TODO Improve error handling
84
    #TODO Improve error handling
88
    my $msg = $fn?
85
    my $msg = $delete
89
        JSON::to_json({ $fn => 6 }):
86
        ? JSON::to_json({ $fn => 6 })
90
        JSON::to_json({
87
        : $id
91
            $upload? $upload->filename: ( $id? "id $id": '[No id]' ), 7,
88
        ? JSON::to_json({ $fn || $id, 7 })
92
        });
89
        : '';
93
    $template->param(
90
    $template->param(
94
        mode             => 'deleted',
91
        mode             => 'deleted',
95
        msg              => $msg,
92
        msg              => $msg,
96
        uploadcategories => Koha::UploadedFile->getCategories,
97
    );
93
    );
98
    output_html_with_http_headers $input, $cookie, $template->output;
94
    output_html_with_http_headers $input, $cookie, $template->output;
99
95
100
} elsif ( $op eq 'download' ) {
96
} elsif ( $op eq 'download' ) {
101
    my $rec = Koha::UploadedFiles->search({
97
    my $rec = Koha::UploadedFiles->find( $id );
102
        id => $id,
98
    undef $rec if $rec && $plugin && !$rec->public;
103
        $plugin? ( public => 1 ) : (),
104
    })->next;
105
    my $fh  = $rec? $rec->file_handle:  undef;
99
    my $fh  = $rec? $rec->file_handle:  undef;
106
    if ( !$rec || !$fh ) {
100
    if ( !$rec || !$fh ) {
107
        $template->param(
101
        $template->param(
108
            mode             => 'new',
102
            mode             => 'new',
109
            msg              => JSON::to_json( { $id => 5 } ),
103
            msg              => JSON::to_json( { $id => 5 } ),
110
            uploadcategories => Koha::UploadedFile->getCategories,
111
        );
104
        );
112
        output_html_with_http_headers $input, $cookie, $template->output;
105
        output_html_with_http_headers $input, $cookie, $template->output;
113
    } else {
106
    } else {
114
- 

Return to bug 17501