@@ -89,6 +89,7 @@ use constant DB_COLUMNS => qw(
89
89
mimetype
90
90
modification_time
91
91
submitter_id
92
+ attach_size
92
93
) ;
93
94
94
95
use constant REQUIRED_FIELD_MAP => {
@@ -359,45 +360,14 @@ sub data {
359
360
360
361
=item C<datasize >
361
362
362
- the length (in characters ) of the attachment content
363
+ the length (in bytes ) of the attachment content
363
364
364
365
=back
365
366
366
367
=cut
367
368
368
- # datasize is a property of the data itself, and it's unclear whether we should
369
- # expose it at all, since you can easily derive it from the data itself: in TT,
370
- # attachment.data.size; in Perl, length($attachment->{data}). But perhaps
371
- # it makes sense for performance reasons, since accessing the data forces it
372
- # to get retrieved from the database/filesystem and loaded into memory,
373
- # while datasize avoids loading the attachment into memory, calling SQL's
374
- # LENGTH() function or stat()ing the file instead. I've left it in for now.
375
-
376
369
sub datasize {
377
- my $self = shift ;
378
- return $self -> {datasize } if defined $self -> {datasize };
379
-
380
- # If we have already retrieved the data, return its size.
381
- return length ($self -> {data }) if exists $self -> {data };
382
-
383
- $self -> {datasize } =
384
- Bugzilla-> dbh-> selectrow_array(" SELECT LENGTH(thedata)
385
- FROM attach_data
386
- WHERE id = ?" ,
387
- undef , $self -> id) || 0;
388
-
389
- # If there's no attachment data in the database, either the attachment
390
- # is stored in a local file, and so retrieve its size from the file,
391
- # or the attachment has been deleted.
392
- unless ($self -> {datasize }) {
393
- if (open (AH, ' <' , $self -> _get_local_filename())) {
394
- binmode AH;
395
- $self -> {datasize } = (stat (AH))[7];
396
- close (AH);
397
- }
398
- }
399
-
400
- return $self -> {datasize };
370
+ return $_ [0]-> {attach_size };
401
371
}
402
372
403
373
=over
@@ -566,18 +536,18 @@ sub _check_data {
566
536
my ($invocant , $params ) = @_ ;
567
537
568
538
my $data = $params -> {data };
569
- $params -> {filesize } = ref $data ? -s $data : length ($data );
539
+ $params -> {attach_size } = ref $data ? -s $data : length ($data );
570
540
571
541
Bugzilla::Hook::process(' attachment_process_data' , { data => \$data ,
572
542
attributes => $params });
573
543
574
- $params -> {filesize } || ThrowUserError(' zero_length_file' );
544
+ $params -> {attach_size } || ThrowUserError(' zero_length_file' );
575
545
# Make sure the attachment does not exceed the maximum permitted size.
576
546
my $max_size = max(Bugzilla-> params-> {' maxlocalattachment' } * 1048576,
577
547
Bugzilla-> params-> {' maxattachmentsize' } * 1024);
578
548
579
- if ($params -> {filesize } > $max_size ) {
580
- my $vars = { filesize => sprintf (" %.0f" , $params -> {filesize }/1024) };
549
+ if ($params -> {attach_size } > $max_size ) {
550
+ my $vars = { filesize => sprintf (" %.0f" , $params -> {attach_size }/1024) };
581
551
ThrowUserError(' file_too_large' , $vars );
582
552
}
583
553
return $data ;
@@ -703,16 +673,6 @@ sub get_attachments_by_bug {
703
673
foreach my $attachment (@$attachments ) {
704
674
$attachment -> {attacher } = $user_map {$attachment -> {submitter_id }};
705
675
}
706
-
707
- # Preload datasizes.
708
- my $sizes =
709
- $dbh -> selectall_hashref(' SELECT attach_id, LENGTH(thedata) AS datasize
710
- FROM attachments LEFT JOIN attach_data ON attach_id = id
711
- WHERE bug_id = ?' ,
712
- ' attach_id' , undef , $bug -> id);
713
-
714
- # Force the size of attachments not in the DB to be recalculated.
715
- $_ -> {datasize } = $sizes -> {$_ -> id}-> {datasize } || undef foreach @$attachments ;
716
676
}
717
677
return $attachments ;
718
678
}
@@ -842,13 +802,12 @@ sub create {
842
802
my $bug = delete $params -> {bug };
843
803
$params -> {bug_id } = $bug -> id;
844
804
my $data = delete $params -> {data };
845
- my $size = delete $params -> {filesize };
846
805
847
806
my $attachment = $class -> insert_create_data($params );
848
807
my $attachid = $attachment -> id;
849
808
850
809
# The file is too large to be stored in the DB, so we store it locally.
851
- if ($size > Bugzilla-> params-> {' maxattachmentsize' } * 1024) {
810
+ if ($params -> { attach_size } > Bugzilla-> params-> {' maxattachmentsize' } * 1024) {
852
811
my $attachdir = bz_locations()-> {' attachdir' };
853
812
my $hash = ($attachid % 100) + 100;
854
813
$hash =~ s / .*(\d\d )$/ group.$1 / ;
@@ -966,8 +925,8 @@ sub remove_from_db {
966
925
$dbh -> do(' DELETE FROM flags WHERE ' . $dbh -> sql_in(' id' , $flag_ids ))
967
926
if @$flag_ids ;
968
927
$dbh -> do(' DELETE FROM attach_data WHERE id = ?' , undef , $self -> id);
969
- $dbh -> do(' UPDATE attachments SET mimetype = ?, ispatch = ?, isobsolete = ?
970
- WHERE attach_id = ?' , undef , (' text/plain' , 0, 1, $self -> id));
928
+ $dbh -> do(' UPDATE attachments SET mimetype = ?, ispatch = ?, isobsolete = ?, attach_size = ?
929
+ WHERE attach_id = ?' , undef , (' text/plain' , 0, 1, 0, $self -> id));
971
930
$dbh -> bz_commit_transaction();
972
931
973
932
# As we don't call SUPER->remove_from_db we need to manually clear
0 commit comments