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

(-)a/Koha/Upload.pm (-42 / +5 lines)
Lines 46-54 Koha::Upload - Facilitate file uploads (temporary and permanent) Link Here
46
    while( <$fh> ) { print $_; }
46
    while( <$fh> ) { print $_; }
47
    $fh->close;
47
    $fh->close;
48
48
49
    # delete an upload
50
    my ( $fn ) = Koha::Upload->new->delete({ id => $id });
51
52
=head1 DESCRIPTION
49
=head1 DESCRIPTION
53
50
54
    This module is a refactored version of C4::UploadedFile but adds on top
51
    This module is a refactored version of C4::UploadedFile but adds on top
Lines 56-61 Koha::Upload - Facilitate file uploads (temporary and permanent) Link Here
56
    That report added module UploadedFiles.pm. This module contains the
53
    That report added module UploadedFiles.pm. This module contains the
57
    functionality of both.
54
    functionality of both.
58
55
56
    The module has been revised to use Koha::Object[s]; the delete method
57
    has been moved to Koha::UploadedFile[s].
58
59
=head1 METHODS
59
=head1 METHODS
60
60
61
=cut
61
=cut
Lines 192-210 sub get { Link Here
192
    return wantarray? @rv: $res;
192
    return wantarray? @rv: $res;
193
}
193
}
194
194
195
=head2 delete
196
197
    Returns array of deleted filenames or undef.
198
    Since it now only accepts id as parameter, you should not expect more
199
    than one filename.
200
201
=cut
202
203
sub delete {
204
    my ( $self, $params ) = @_;
205
    return $self->_delete( $params->{id} );
206
}
207
208
=head1 CLASS METHODS
195
=head1 CLASS METHODS
209
196
210
=head2 getCategories
197
=head2 getCategories
Lines 262-269 sub allows_add_by { Link Here
262
sub _init {
249
sub _init {
263
    my ( $self, $params ) = @_;
250
    my ( $self, $params ) = @_;
264
251
265
    $self->{rootdir} = C4::Context->config('upload_path');
252
    $self->{rootdir} = Koha::UploadedFile->permanent_directory;
266
    $self->{tmpdir} = File::Spec->tmpdir;
253
    $self->{tmpdir} = Koha::UploadedFile->temporary_directory;
267
254
268
    $params->{tmp} = $params->{temp} if !exists $params->{tmp};
255
    $params->{tmp} = $params->{temp} if !exists $params->{tmp};
269
    $self->{temporary} = $params->{tmp}? 1: 0; #default false
256
    $self->{temporary} = $params->{tmp}? 1: 0; #default false
Lines 402-431 sub _lookup { Link Here
402
    # Does always return an arrayref (perhaps an empty one)
389
    # Does always return an arrayref (perhaps an empty one)
403
}
390
}
404
391
405
sub _delete {
406
    my ( $self, $id ) = @_;
407
    my $rec = Koha::UploadedFiles->find($id) || return;
408
    my $filename = $rec->filename;
409
    my $file = $self->_full_fname({
410
        permanent => $rec->permanent,
411
        dir       => $rec->dir,
412
        hashvalue => $rec->hashvalue,
413
        filename  => $filename,
414
    });
415
416
    if( !-e $file ) { # we will just delete the record
417
        # TODO Should we add a trace here for the missing file?
418
        $rec->delete;
419
        return $filename;
420
    } elsif( unlink($file) ) {
421
        $rec->delete;
422
        return $filename;
423
    }
424
    $self->{files}->{ $rec->{filename} }->{errcode} = 7;
425
    #NOTE: errcode=6 is used to report successful delete (see template)
426
    return;
427
}
428
429
sub _compute {
392
sub _compute {
430
# Computes hash value when sub hook feeds the first block
393
# Computes hash value when sub hook feeds the first block
431
# For temporary files, the id is made unique with time
394
# For temporary files, the id is made unique with time
(-)a/Koha/UploadedFile.pm (-3 / +55 lines)
Lines 18-23 package Koha::UploadedFile; Link Here
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
use File::Spec;
21
22
22
#use Koha::Database;
23
#use Koha::Database;
23
24
Lines 41-57 Description Link Here
41
42
42
=head3 delete
43
=head3 delete
43
44
44
Delete uploaded file (to be extended)
45
Delete uploaded file.
46
It deletes not only the record, but also the actual file.
47
48
Returns filename on successful delete or undef.
45
49
46
=cut
50
=cut
47
51
48
sub delete {
52
sub delete {
49
    my ( $self, $params ) = @_;
53
    my ( $self ) = @_;
50
    $self->SUPER::delete( $params );
54
55
    my $name = $self->filename;
56
    my $file = $self->full_path;
57
58
    if( !-e $file ) { # we will just delete the record
59
        warn "Removing record for $name within category ".
60
            $self->uploadcategorycode. ", but file was missing.";
61
        return $name if $self->SUPER::delete;
62
    } elsif( unlink($file) ) {
63
        return $name if $self->SUPER::delete;
64
    } else {
65
        warn "Problem while deleting: $file";
66
    }
67
    return; # something went wrong
68
}
69
70
=head3 full_path
71
72
Returns the fully qualified path name for an uploaded file.
73
74
=cut
75
76
sub full_path {
77
    my ( $self ) = @_;
78
    my $path = File::Spec->catfile(
79
        $self->permanent?
80
            $self->permanent_directory: $self->temporary_directory,
81
        $self->dir,
82
        $self->hashvalue. '_'. $self->filename,
83
    );
84
    return $path;
51
}
85
}
52
86
53
=head2 CLASS METHODS
87
=head2 CLASS METHODS
54
88
89
=head3 root_directory
90
91
=cut
92
93
sub permanent_directory {
94
    my ( $class ) = @_;
95
    return C4::Context->config('upload_path');
96
}
97
98
=head3 tmp_directory
99
100
=cut
101
102
sub temporary_directory {
103
    my ( $class ) = @_;
104
    return File::Spec->tmpdir;
105
}
106
55
=head3 _type
107
=head3 _type
56
108
57
Returns name of corresponding DBIC resultset
109
Returns name of corresponding DBIC resultset
(-)a/Koha/UploadedFiles.pm (-4 / +18 lines)
Lines 40-54 Description Link Here
40
40
41
=head2 INSTANCE METHODS
41
=head2 INSTANCE METHODS
42
42
43
=head3 delete
43
=head3 delete, delete_errors
44
44
45
Delete uploaded files
45
Delete uploaded files.
46
Returns true if no errors occur.
47
Delete_errors returns the number of errors when deleting files.
46
48
47
=cut
49
=cut
48
50
49
sub delete {
51
sub delete {
50
    my ( $self, $params ) = @_;
52
    my ( $self ) = @_;
51
    $self->SUPER::delete( $params );
53
    # We use the individual delete on each resultset record
54
    my $err = 0;
55
    while( my $row = $self->_resultset->next ) {
56
        my $kohaobj = Koha::UploadedFile->_new_from_dbic( $row );
57
        $err++ if !$kohaobj->delete;
58
    }
59
    $self->{delete_errors} = $err;
60
    return $err==0;
61
}
62
63
sub delete_errors {
64
    my ( $self ) = @_;
65
    return $self->{delete_errors};
52
}
66
}
53
67
54
=head2 CLASS METHODS
68
=head2 CLASS METHODS
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/upload.tt (-1 / +1 lines)
Lines 201-207 Link Here
201
        _("No temporary directory found."),
201
        _("No temporary directory found."),
202
        _("File could not be read."),
202
        _("File could not be read."),
203
        _("File has been deleted."),
203
        _("File has been deleted."),
204
        _("File could not be deleted."),
204
        _("File or upload record could not be deleted."),
205
    ];
205
    ];
206
//]]>
206
//]]>
207
</script>
207
</script>
(-)a/t/db_dependent/Upload.t (-6 / +28 lines)
Lines 36-41 our $uploads = [ Link Here
36
    [
36
    [
37
        { name => 'file4', cat => undef, size => 5000 }, # temp duplicate
37
        { name => 'file4', cat => undef, size => 5000 }, # temp duplicate
38
    ],
38
    ],
39
    [
40
        { name => 'file5', cat => undef, size => 7000 }, # temp duplicate
41
    ],
39
];
42
];
40
43
41
# Redirect upload dir structure and mock File::Spec and CGI
44
# Redirect upload dir structure and mock File::Spec and CGI
Lines 48-54 $cgimod->mock( 'new' => \&newCGI ); Link Here
48
51
49
# Start testing
52
# Start testing
50
subtest 'Test01' => sub {
53
subtest 'Test01' => sub {
51
    plan tests => 7;
54
    plan tests => 9;
52
    test01();
55
    test01();
53
};
56
};
54
subtest 'Test02' => sub {
57
subtest 'Test02' => sub {
Lines 64-70 subtest 'Test04' => sub { Link Here
64
    test04();
67
    test04();
65
};
68
};
66
subtest 'Test05' => sub {
69
subtest 'Test05' => sub {
67
    plan tests => 5;
70
    plan tests => 6;
68
    test05();
71
    test05();
69
};
72
};
70
subtest 'Test06' => sub {
73
subtest 'Test06' => sub {
Lines 85-90 sub test01 { Link Here
85
    # Delete existing records (for later tests)
88
    # Delete existing records (for later tests)
86
    $dbh->do( "DELETE FROM uploaded_files" );
89
    $dbh->do( "DELETE FROM uploaded_files" );
87
90
91
    # Check mocked directories
92
    is( Koha::UploadedFile->permanent_directory, $tempdir,
93
        'Check permanent directory' );
94
    is( Koha::UploadedFile->temporary_directory, $tempdir,
95
        'Check temporary directory' );
96
88
    my $upl = Koha::Upload->new({
97
    my $upl = Koha::Upload->new({
89
        category => $uploads->[$current_upload]->[0]->{cat},
98
        category => $uploads->[$current_upload]->[0]->{cat},
90
    });
99
    });
Lines 143-153 sub test05 { # add temporary file with same name and contents, delete it Link Here
143
    is( $upl->count, 1, 'Upload 5 adds duplicate temporary file' );
152
    is( $upl->count, 1, 'Upload 5 adds duplicate temporary file' );
144
    my $id = $upl->result;
153
    my $id = $upl->result;
145
    my $r = $upl->get({ id => $id });
154
    my $r = $upl->get({ id => $id });
146
    my @d = $upl->delete({ id => $id });
155
147
    is( $d[0], $r->{name}, 'Delete successful' );
156
    # testing delete via UploadedFiles (plural)
148
    is( -e $r->{path}? 1: 0, 0, 'File no longer found after delete' );
157
    my $delete = Koha::UploadedFiles->search({ id => $id })->delete;
158
    is( $delete, 1, 'Delete successful' );
159
    isnt( -e $r->{path}, 1, 'File no longer found after delete' );
149
    is( scalar $upl->get({ id => $id }), undef, 'Record also gone' );
160
    is( scalar $upl->get({ id => $id }), undef, 'Record also gone' );
150
    is( $upl->delete({ id => $id }), undef, 'Repeated delete failed' );
161
162
    # testing delete via UploadedFile (singular)
163
    # Note that find returns a Koha::Object
164
    $upl = Koha::Upload->new({ tmp => 1 });
165
    $upl->cgi;
166
    $id = $upl->result;
167
    my $kohaobj = Koha::UploadedFiles->find( $id );
168
    my $name = $kohaobj->filename;
169
    my $path = $kohaobj->full_path;
170
    $delete = $kohaobj->delete;
171
    is( $delete, $name, 'Delete successful' );
172
    isnt( -e $path, 1, 'File no longer found after delete' );
151
}
173
}
152
174
153
sub test06 { #some extra tests for get
175
sub test06 { #some extra tests for get
(-)a/tools/upload.pl (-9 / +11 lines)
Lines 68-84 if ( $op eq 'new' ) { Link Here
68
68
69
} elsif ( $op eq 'delete' ) {
69
} elsif ( $op eq 'delete' ) {
70
    # delete only takes the id parameter
70
    # delete only takes the id parameter
71
    my $upl = Koha::Upload->new($upar);
71
    my $upload = $plugin?
72
    my ($fn) = $upl->delete( { id => $id } );
72
         Koha::UploadedFiles->search({ public => 1, id => $id })->next:
73
    my $e = $upl->err;
73
         Koha::UploadedFiles->find($id);
74
    my $msg =
74
    my $fn = $upload? $upload->delete: undef;
75
        $fn ? JSON::to_json( { $fn => 6 } )
75
    #TODO Improve error handling
76
      : $e  ? JSON::to_json($e)
76
    my $msg = $fn?
77
      :       undef;
77
        JSON::to_json({ $fn => 6 }):
78
        JSON::to_json({
79
            $upload? $upload->filename: ( $id? "id $id": '[No id]' ), 7,
80
        });
78
    $template->param(
81
    $template->param(
79
        mode             => 'deleted',
82
        mode             => 'deleted',
80
        msg              => $msg,
83
        msg              => $msg,
81
        uploadcategories => $upl->getCategories,
84
        uploadcategories => Koha::Upload->getCategories,
82
    );
85
    );
83
    output_html_with_http_headers $input, $cookie, $template->output;
86
    output_html_with_http_headers $input, $cookie, $template->output;
84
87
85
- 

Return to bug 17501