use Modern::Perl;
use File::Spec;
-#use Koha::Database;
-
use parent qw(Koha::Object);
=head1 NAME
=head1 SYNOPSIS
-use Koha::UploadedFile;
+ use Koha::UploadedFile;
+
+ # store record in uploaded_files
+ my $upload = Koha::UploadedFile->new({ [columns and values] });
+
+ # get a file handle on an uploaded_file
+ my $fh = $upload->file_handle;
+
+ # get full path
+ my $path = $upload->full_path;
+
+ # delete uploaded file
+ $upload->delete;
=head1 DESCRIPTION
-Description
+Allows regular CRUD operations on uploaded_files via Koha::Object / DBIx.
+
+The delete method also takes care of deleting files. The full_path method
+returns a fully qualified path for an upload.
+
+Additional methods include: file_handle, httpheaders.
=head1 METHODS
It deletes not only the record, but also the actual file (unless you pass
the keep_file parameter).
-Returns filename on successful delete or undef.
+Returns number of deleted records (1 or 0E0), or -1 for unknown.
+Please keep in mind that a deleted record does not automatically imply a
+deleted file; a warning may have been raised.
+(TODO: Use exceptions.)
=cut
my $name = $self->filename;
my $file = $self->full_path;
- if( $params->{keep_file} ) {
- return $name if $self->SUPER::delete;
- } elsif( !-e $file ) { # we will just delete the record
+ my $retval = $self->SUPER::delete;
+ return $retval if $params->{keep_file};
+
+ if( ! -e $file ) {
warn "Removing record for $name within category ".
$self->uploadcategorycode. ", but file was missing.";
- return $name if $self->SUPER::delete;
- } elsif( unlink($file) ) {
- return $name if $self->SUPER::delete;
- } else {
+ } elsif( ! unlink($file) ) {
warn "Problem while deleting: $file";
}
- return; # something went wrong
+ return $retval;
}
=head3 full_path
sub full_path {
my ( $self ) = @_;
my $path = File::Spec->catfile(
- $self->permanent?
- $self->permanent_directory: $self->temporary_directory,
+ $self->permanent
+ ? $self->permanent_directory
+ : C4::Context->temporary_directory,
$self->dir,
$self->hashvalue. '_'. $self->filename,
);
=head3 httpheaders
- httpheaders returns http headers for a retrievable upload
- Will be extended by report 14282
+httpheaders returns http headers for a retrievable upload.
+
+Will be extended by report 14282
=cut
sub httpheaders {
my ( $self ) = @_;
- return (
- '-type' => 'application/octet-stream',
- '-attachment' => $self->filename,
- );
+ if( $self->filename =~ /\.pdf$/ ) {
+ return (
+ '-type' => 'application/pdf',
+ 'Content-Disposition' => 'inline; filename='.$self->filename,
+ );
+ } else {
+ return (
+ '-type' => 'application/octet-stream',
+ '-attachment' => $self->filename,
+ );
+ }
}
=head2 CLASS METHODS
=head3 permanent_directory
+Returns root directory for permanent storage
+
=cut
sub permanent_directory {
return C4::Context->config('upload_path');
}
-=head3 tmp_directory
-
-=cut
-
-sub temporary_directory {
- my ( $class ) = @_;
- return File::Spec->tmpdir;
-}
-
-=head3 getCategories
-
- getCategories returns a list of upload category codes and names
-
-=cut
-
-sub getCategories {
- my ( $class ) = @_;
- my $cats = C4::Koha::GetAuthorisedValues('UPLOAD');
- [ map {{ code => $_->{authorised_value}, name => $_->{lib} }} @$cats ];
-}
-
=head3 _type
Returns name of corresponding DBIC resultset