Bug 17927: (QA followup) Fix boolean types
[koha.git] / Koha / UploadedFile.pm
blob160c2d349f6cb8d3bda87a67f647019fcd23e760
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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 use Modern::Perl;
21 use File::Spec;
23 use parent qw(Koha::Object);
25 =head1 NAME
27 Koha::UploadedFile - Koha::Object class for single uploaded file
29 =head1 SYNOPSIS
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;
39 # get full path
40 my $path = $upload->full_path;
42 # delete uploaded file
43 $upload->delete;
45 =head1 DESCRIPTION
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.
54 =head1 METHODS
56 =head2 INSTANCE METHODS
58 =head3 delete
60 Delete uploaded file.
61 It deletes not only the record, but also the actual file (unless you pass
62 the keep_file parameter).
64 Returns filename on successful delete or undef.
66 =cut
68 sub delete {
69 my ( $self, $params ) = @_;
71 my $name = $self->filename;
72 my $file = $self->full_path;
74 my $retval = $self->SUPER::delete;
75 return $retval if $params->{keep_file};
77 if( ! -e $file ) {
78 warn "Removing record for $name within category ".
79 $self->uploadcategorycode. ", but file was missing.";
80 } elsif( ! unlink($file) ) {
81 $retval = 0;
82 warn "Problem while deleting: $file";
84 return $retval;
87 =head3 full_path
89 Returns the fully qualified path name for an uploaded file.
91 =cut
93 sub full_path {
94 my ( $self ) = @_;
95 my $path = File::Spec->catfile(
96 $self->permanent?
97 $self->permanent_directory: $self->temporary_directory,
98 $self->dir,
99 $self->hashvalue. '_'. $self->filename,
101 return $path;
104 =head3 file_handle
106 Returns a file handle for an uploaded file.
108 =cut
110 sub file_handle {
111 my ( $self ) = @_;
112 $self->{_file_handle} = IO::File->new( $self->full_path, "r" );
113 return if !$self->{_file_handle};
114 $self->{_file_handle}->binmode;
115 return $self->{_file_handle};
118 =head3 httpheaders
120 httpheaders returns http headers for a retrievable upload.
122 Will be extended by report 14282
124 =cut
126 sub httpheaders {
127 my ( $self ) = @_;
128 return (
129 '-type' => 'application/octet-stream',
130 '-attachment' => $self->filename,
134 =head2 CLASS METHODS
136 =head3 permanent_directory
138 Returns root directory for permanent storage
140 =cut
142 sub permanent_directory {
143 my ( $class ) = @_;
144 return C4::Context->config('upload_path');
147 =head3 tmp_directory
149 Returns root directory for temporary storage
151 =cut
153 sub temporary_directory {
154 my ( $class ) = @_;
155 return File::Spec->tmpdir;
158 =head3 _type
160 Returns name of corresponding DBIC resultset
162 =cut
164 sub _type {
165 return 'UploadedFile';
168 =head1 AUTHOR
170 Marcel de Rooy (Rijksmuseum)
172 Koha Development Team
174 =cut