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

(-)a/Koha/Schema/Result/UploadedFile.pm (-4 / +12 lines)
Lines 35-40 __PACKAGE__->table("uploaded_files"); Link Here
35
  is_nullable: 0
35
  is_nullable: 0
36
  size: 40
36
  size: 40
37
37
38
=head2 checksum
39
40
  data_type: 'tinyint'
41
  default_value: 1
42
  is_nullable: 1
43
38
=head2 filename
44
=head2 filename
39
45
40
  data_type: 'mediumtext'
46
  data_type: 'mediumtext'
Lines 43-49 __PACKAGE__->table("uploaded_files"); Link Here
43
=head2 dir
49
=head2 dir
44
50
45
  data_type: 'mediumtext'
51
  data_type: 'mediumtext'
46
  is_nullable: 0
52
  is_nullable: 1
47
53
48
=head2 filesize
54
=head2 filesize
49
55
Lines 84-93 __PACKAGE__->add_columns( Link Here
84
  { data_type => "integer", is_auto_increment => 1, is_nullable => 0 },
90
  { data_type => "integer", is_auto_increment => 1, is_nullable => 0 },
85
  "hashvalue",
91
  "hashvalue",
86
  { data_type => "char", is_nullable => 0, size => 40 },
92
  { data_type => "char", is_nullable => 0, size => 40 },
93
  "checksum",
94
  { data_type => "tinyint", default_value => 1, is_nullable => 1 },
87
  "filename",
95
  "filename",
88
  { data_type => "mediumtext", is_nullable => 0 },
96
  { data_type => "mediumtext", is_nullable => 0 },
89
  "dir",
97
  "dir",
90
  { data_type => "mediumtext", is_nullable => 0 },
98
  { data_type => "mediumtext", is_nullable => 1 },
91
  "filesize",
99
  "filesize",
92
  { data_type => "integer", is_nullable => 1 },
100
  { data_type => "integer", is_nullable => 1 },
93
  "dtcreated",
101
  "dtcreated",
Lines 120-127 __PACKAGE__->add_columns( Link Here
120
__PACKAGE__->set_primary_key("id");
128
__PACKAGE__->set_primary_key("id");
121
129
122
130
123
# Created by DBIx::Class::Schema::Loader v0.07042 @ 2018-02-16 17:54:54
131
# Created by DBIx::Class::Schema::Loader v0.07046 @ 2019-01-30 13:21:07
124
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:kJUbIULQMBo3t51HvWC8cg
132
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:4f2CTtOjsy/t/1/u9Am1hQ
125
133
126
134
127
# You can replace this text with custom code or comments, and it will be preserved on regeneration
135
# You can replace this text with custom code or comments, and it will be preserved on regeneration
(-)a/Koha/UploadedFile.pm (-2 / +3 lines)
Lines 101-112 Returns the fully qualified path name for an uploaded file. Link Here
101
101
102
sub full_path {
102
sub full_path {
103
    my ( $self ) = @_;
103
    my ( $self ) = @_;
104
    my $filename = $self->checksum ? $self->hashvalue. '_'. $self->filename: $self->filename;
104
    my $path = File::Spec->catfile(
105
    my $path = File::Spec->catfile(
105
        $self->permanent
106
        $self->permanent
106
            ? $self->permanent_directory
107
            ? $self->permanent_directory
107
            : C4::Context->temporary_directory,
108
            : C4::Context->temporary_directory,
108
        $self->dir,
109
        $self->dir .'/'. $self->uploadcategorycode,
109
        $self->hashvalue. '_'. $self->filename,
110
        $filename,
110
    );
111
    );
111
    return $path;
112
    return $path;
112
}
113
}
(-)a/Koha/Uploader.pm (-3 / +10 lines)
Lines 69-74 use Modern::Perl; Link Here
69
use CGI; # no utf8 flag, since it may interfere with binary uploads
69
use CGI; # no utf8 flag, since it may interfere with binary uploads
70
use Digest::MD5;
70
use Digest::MD5;
71
use Encode;
71
use Encode;
72
use File::Path;
72
use File::Spec;
73
use File::Spec;
73
use IO::File;
74
use IO::File;
74
use Time::HiRes;
75
use Time::HiRes;
Lines 197-202 sub _init { Link Here
197
198
198
    $params->{tmp} = $params->{temp} if !exists $params->{tmp};
199
    $params->{tmp} = $params->{temp} if !exists $params->{tmp};
199
    $self->{temporary} = $params->{tmp}? 1: 0; #default false
200
    $self->{temporary} = $params->{tmp}? 1: 0; #default false
201
    $params->{checksum} = 1 if !exists $params->{checksum};
202
    $self->{checksum} = $params->{checksum} ;
200
    if( $params->{tmp} ) {
203
    if( $params->{tmp} ) {
201
        my $db =  C4::Context->config('database');
204
        my $db =  C4::Context->config('database');
202
        $self->{category} = KOHA_UPLOAD;
205
        $self->{category} = KOHA_UPLOAD;
Lines 205-210 sub _init { Link Here
205
        $self->{category} = $params->{category} || KOHA_UPLOAD;
208
        $self->{category} = $params->{category} || KOHA_UPLOAD;
206
    }
209
    }
207
210
211
    $self->{dir} = $params->{dir} || "";
208
    $self->{files} = {};
212
    $self->{files} = {};
209
    $self->{uid} = C4::Context->userenv->{number} if C4::Context->userenv;
213
    $self->{uid} = C4::Context->userenv->{number} if C4::Context->userenv;
210
    $self->{public} = $params->{public}? 1: undef;
214
    $self->{public} = $params->{public}? 1: undef;
Lines 230-236 sub _create_file { Link Here
230
    } else {
234
    } else {
231
        my $dir = $self->_dir;
235
        my $dir = $self->_dir;
232
        my $hashval = $self->{files}->{$filename}->{hash};
236
        my $hashval = $self->{files}->{$filename}->{hash};
233
        my $fn = $hashval. '_'. $filename;
237
        my $fn = $self->{checksum} ? $hashval. '_'. $filename: $filename;
234
238
235
        # if the file exists and it is registered, then set error
239
        # if the file exists and it is registered, then set error
236
        # if it exists, but is not in the database, we will overwrite
240
        # if it exists, but is not in the database, we will overwrite
Lines 238-243 sub _create_file { Link Here
238
        Koha::UploadedFiles->search({
242
        Koha::UploadedFiles->search({
239
            hashvalue          => $hashval,
243
            hashvalue          => $hashval,
240
            uploadcategorycode => $self->{category},
244
            uploadcategorycode => $self->{category},
245
            dir                => $self->{dir},
241
        })->count ) {
246
        })->count ) {
242
            $self->{files}->{$filename}->{errcode} = ERR_EXISTS;
247
            $self->{files}->{$filename}->{errcode} = ERR_EXISTS;
243
            return;
248
            return;
Lines 257-264 sub _create_file { Link Here
257
sub _dir {
262
sub _dir {
258
    my ( $self ) = @_;
263
    my ( $self ) = @_;
259
    my $dir = $self->{temporary}? $self->{tmpdir}: $self->{rootdir};
264
    my $dir = $self->{temporary}? $self->{tmpdir}: $self->{rootdir};
265
    $dir = $self->{rootdir}.'/'. $self->{dir} if( $self->{dir} );
260
    $dir.= '/'. $self->{category};
266
    $dir.= '/'. $self->{category};
261
    mkdir $dir if !-d $dir;
267
    mkpath $dir if !-d $dir;
262
    return $dir;
268
    return $dir;
263
}
269
}
264
270
Lines 285-292 sub _register { Link Here
285
    my ( $self, $filename, $size ) = @_;
291
    my ( $self, $filename, $size ) = @_;
286
    my $rec = Koha::UploadedFile->new({
292
    my $rec = Koha::UploadedFile->new({
287
        hashvalue => $self->{files}->{$filename}->{hash},
293
        hashvalue => $self->{files}->{$filename}->{hash},
294
        checksum  => $self->{checksum},
288
        filename  => $filename,
295
        filename  => $filename,
289
        dir       => $self->{category},
296
        dir       => $self->{dir},
290
        filesize  => $size,
297
        filesize  => $size,
291
        owner     => $self->{uid},
298
        owner     => $self->{uid},
292
        uploadcategorycode => $self->{category},
299
        uploadcategorycode => $self->{category},
(-)a/installer/data/mysql/atomicupdate/bug_20208.perl (+14 lines)
Line 0 Link Here
1
$DBversion = '18.12.00.XXX'; # will be replaced by the RM
2
if( CheckVersion( $DBversion ) ) {
3
    unless ( column_exists('uploaded_files', 'checksum') ) {
4
        $dbh->do( q{
5
            ALTER TABLE uploaded_files ADD checksum tinyint(1) DEFAULT 1 AFTER hashvalue
6
        });
7
        $dbh->do( q{
8
            UPDATE uploaded_files SET checksum=1
9
        });
10
        $dbh->do("ALTER TABLE `uploaded_files` MODIFY COLUMN `dir` mediumtext DEFAULT NULL");
11
    }
12
    SetVersion( $DBversion );
13
    print "Upgrade to $DBversion done (Bug 20208 - Development - Peddie School - Custom file upload paths)\n";
14
}
(-)a/t/db_dependent/Upload.t (-2 / +119 lines)
Lines 2-8 Link Here
2
2
3
use Modern::Perl;
3
use Modern::Perl;
4
use File::Temp qw/ tempdir /;
4
use File::Temp qw/ tempdir /;
5
use Test::More tests => 13;
5
use Test::More tests => 18;
6
use Test::Warn;
6
use Test::Warn;
7
7
8
use Test::MockModule;
8
use Test::MockModule;
Lines 46-51 our $uploads = [ Link Here
46
        { name => 'file6', cat => undef, size => 6500 },
46
        { name => 'file6', cat => undef, size => 6500 },
47
        { name => 'file7', cat => undef, size => 6501 },
47
        { name => 'file7', cat => undef, size => 6501 },
48
    ],
48
    ],
49
    #cheksum tests
50
    [
51
        { name => 'file8', cat => 'A', checksum => 0, size => 2000 },
52
    ],
53
    [
54
        { name => 'file9', cat => undef, checksum => 0, size => 5000 }, # temp without checksum
55
    ],
56
    [
57
        { name => 'file3', cat => 'B', checksum => 0, size => 1000 }, #duplicate file without checksum
58
    ],
59
    #path tests
60
    [
61
        { name => 'file10', dir => 'A/B', cat => 'A', size => 2000 },
62
    ],
63
    [
64
        { name => 'file11', dir => 'A/B', cat => undef, size => 3000 },
65
    ],
49
];
66
];
50
67
51
# Redirect upload dir structure and mock C4::Context and CGI
68
# Redirect upload dir structure and mock C4::Context and CGI
Lines 327-332 subtest 'Testing download headers' => sub { Link Here
327
    is_deeply(\@pdf_head, \@pdf_expect,"Get inline pdf headers for pdf");
344
    is_deeply(\@pdf_head, \@pdf_expect,"Get inline pdf headers for pdf");
328
    is_deeply(\@not_head, \@not_expect,"Get download headers for non pdf");
345
    is_deeply(\@not_head, \@not_expect,"Get download headers for non pdf");
329
};
346
};
347
348
subtest 'Not add file checksum to the uploaded filename' => sub {
349
    plan tests => 7;
350
351
    my $upl = Koha::Uploader->new({
352
            category => $uploads->[$current_upload]->[0]->{cat},
353
            checksum => 0,
354
        }); #add file8
355
    my $cgi = $upl->cgi;
356
    my $res = $upl->result;
357
    is( $upl->err, undef, 'No errors reported' );
358
359
    my $rec = Koha::UploadedFiles->find( $res );
360
    is( $rec->filename, 'file8', 'Check file name' );
361
    is( $rec->checksum, 0, 'Checksum false' );
362
    my $path = $rec->full_path;
363
    is( -e $path, 1, 'File is found' );
364
    my $fh = $rec->file_handle;
365
    is( ref($fh) eq 'IO::File' && $fh->opened, 1, 'Get returns a file handle' );
366
    my $delete = $rec->delete;
367
    ok( $delete=~/^-?1$/, 'Delete successful' );
368
    isnt( -e $path, 1, 'File no longer found after delete' );
369
};
370
371
subtest 'Temporary upload without checksum' => sub {
372
    plan tests => 5;
373
374
    my $upl = Koha::Uploader->new({
375
            tmp => 1,
376
            checksum => 0,
377
        }); #add file9
378
    my $cgi = $upl->cgi;
379
    is( $upl->count, 1, 'Upload 9 includes one temporary file' );
380
    is( $upl->err, undef, 'No errors reported' );
381
382
    my $rec = Koha::UploadedFiles->find( $upl->result );
383
    is( $rec->checksum, 0, 'Checksum false' );
384
    is( $rec->uploadcategorycode =~ /_upload$/, 1, 'Check category temp file' );
385
    my $path = $rec->full_path;
386
    is( -e $path, 1, 'File is found' );
387
};
388
389
subtest 'Duplicate file without checksum' => sub {
390
    plan tests => 5;
391
392
    my $upl = Koha::Uploader->new({
393
            category => $uploads->[$current_upload]->[0]->{cat},
394
            checksum => 0,
395
        });
396
    my $cgi = $upl->cgi;
397
    is( $upl->count, 1, 'Upload 10 successful' );
398
    is( $upl->err, undef, 'No errors reported' );
399
400
    my $rec = Koha::UploadedFiles->find( $upl->result );
401
    is( $rec->filename, 'file3', 'Check file name' );
402
    is( $rec->checksum, 0, 'Checksum false' );
403
    my $e = $upl->err;
404
    isnt( $e->{file2}->{code}, Koha::Uploader::ERR_EXISTS, "Already exists error reported" );
405
};
406
407
subtest 'Add upload in dir' => sub {
408
    plan tests => 8;
409
410
    my $upl = Koha::Uploader->new({
411
            category => $uploads->[$current_upload]->[0]->{cat},
412
            dir => $uploads->[$current_upload]->[0]->{dir},
413
        });
414
    my $cgi = $upl->cgi;
415
    is( $upl->count, 1, 'Upload 10 successful' );
416
    isnt( $upl->result, undef, 'Result is undefined' );
417
    is( $upl->err, undef, 'No errors reported' );
418
419
    my $rec = Koha::UploadedFiles->find( $upl->result );
420
    is( $rec->dir, 'A/B', 'Check dir A/B' );
421
    my $path = $rec->full_path;
422
    is( -e $path, 1, 'File is found' );
423
    my $fh = $rec->file_handle;
424
    is( ref($fh) eq 'IO::File' && $fh->opened, 1, 'Get returns a file handle' );
425
    my $delete = $rec->delete;
426
    ok( $delete=~/^-?1$/, 'Delete successful' );
427
    isnt( -e $path, 1, 'File no longer found after delete' );
428
429
};
430
431
432
subtest 'Temporary upload in dir' => sub {
433
    plan tests => 4;
434
435
    my $upl = Koha::Uploader->new({
436
            tmp => 1,
437
            dir => $uploads->[$current_upload]->[0]->{dir},
438
        }); #add file9
439
    my $cgi = $upl->cgi;
440
    is( $upl->err, undef, 'No errors reported' );
441
442
    my $rec = Koha::UploadedFiles->find( $upl->result );
443
    is( $rec->dir, 'A/B', 'Check dir' );
444
    is( $rec->uploadcategorycode =~ /_upload$/, 1, 'Check category temp file' );
445
    my $path = $rec->full_path;
446
    is( -e $path, 1, 'File is found' );
447
};
330
# The end
448
# The end
331
$schema->storage->txn_rollback;
449
$schema->storage->txn_rollback;
332
450
333
- 

Return to bug 20208