1 package Koha
::UploadedFile
;
3 # Copyright Rijksmuseum 2016
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
23 use parent
qw(Koha::Object);
27 Koha::UploadedFile - Koha::Object class for single uploaded file
31 use Koha::UploadedFile;
33 # store record in uploaded_files
34 my $upload = Koha::UploadedFile->new({ [columns and values] });
36 # get a file handle on an uploaded_file
37 my $fh = $upload->file_handle;
40 my $path = $upload->full_path;
42 # delete uploaded file
47 Allows regular CRUD operations on uploaded_files via Koha::Object / DBIx.
49 The delete method also takes care of deleting files. The full_path method
50 returns a fully qualified path for an upload.
52 Additional methods include: file_handle, httpheaders.
56 =head2 INSTANCE METHODS
61 It deletes not only the record, but also the actual file (unless you pass
62 the keep_file parameter).
64 Returns number of deleted records (1 or 0E0), or -1 for unknown.
65 Please keep in mind that a deleted record does not automatically imply a
66 deleted file; a warning may have been raised.
67 (TODO: Use exceptions.)
72 my ( $self, $params ) = @_;
74 my $name = $self->filename;
75 my $file = $self->full_path;
77 my $retval = $self->SUPER::delete;
78 return $retval if $params->{keep_file
};
81 warn "Removing record for $name within category ".
82 $self->uploadcategorycode. ", but file was missing.";
83 } elsif( ! unlink($file) ) {
84 warn "Problem while deleting: $file";
91 Returns the fully qualified path name for an uploaded file.
97 my $path = File
::Spec
->catfile(
99 ?
$self->permanent_directory
100 : C4
::Context
->temporary_directory,
102 $self->hashvalue. '_'. $self->filename,
109 Returns a file handle for an uploaded file.
115 $self->{_file_handle
} = IO
::File
->new( $self->full_path, "r" );
116 return if !$self->{_file_handle
};
117 $self->{_file_handle
}->binmode;
118 return $self->{_file_handle
};
123 httpheaders returns http headers for a retrievable upload.
125 Will be extended by report 14282
131 if( $self->filename =~ /\.pdf$/ ) {
133 '-type' => 'application/pdf',
134 'Content-Disposition' => 'inline; filename='.$self->filename,
138 '-type' => 'application/octet-stream',
139 '-attachment' => $self->filename,
146 =head3 permanent_directory
148 Returns root directory for permanent storage
152 sub permanent_directory
{
154 return C4
::Context
->config('upload_path');
159 Returns name of corresponding DBIC resultset
164 return 'UploadedFile';
169 Marcel de Rooy (Rijksmuseum)
171 Koha Development Team