Bug 19410: (follow-up) Fix typo in POD
[koha.git] / Koha / UploadedFile.pm
1 package Koha::UploadedFile;
2
3 # Copyright Rijksmuseum 2016
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
15 #
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.
19
20 use Modern::Perl;
21 use File::Spec;
22
23 use parent qw(Koha::Object);
24
25 =head1 NAME
26
27 Koha::UploadedFile - Koha::Object class for single uploaded file
28
29 =head1 SYNOPSIS
30
31     use Koha::UploadedFile;
32
33     # store record in uploaded_files
34     my $upload = Koha::UploadedFile->new({ [columns and values] });
35
36     # get a file handle on an uploaded_file
37     my $fh = $upload->file_handle;
38
39     # get full path
40     my $path = $upload->full_path;
41
42     # delete uploaded file
43     $upload->delete;
44
45 =head1 DESCRIPTION
46
47 Allows regular CRUD operations on uploaded_files via Koha::Object / DBIx.
48
49 The delete method also takes care of deleting files. The full_path method
50 returns a fully qualified path for an upload.
51
52 Additional methods include: file_handle, httpheaders.
53
54 =head1 METHODS
55
56 =head2 INSTANCE METHODS
57
58 =head3 delete
59
60 Delete uploaded file.
61 It deletes not only the record, but also the actual file (unless you pass
62 the keep_file parameter).
63
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.)
68
69 =cut
70
71 sub delete {
72     my ( $self, $params ) = @_;
73
74     my $name = $self->filename;
75     my $file = $self->full_path;
76
77     my $retval = $self->SUPER::delete;
78     if( !defined($retval) ) { # undef is Unknown (-1)
79         $retval = -1;
80     } elsif( $retval eq '0' ) { # 0 => 0E0
81         $retval = "0E0";
82     } elsif( $retval !~ /^(0E0|1)$/ ) { # Unknown too
83         $retval = -1;
84     }
85     return $retval if $params->{keep_file};
86
87     if( ! -e $file ) {
88         warn "Removing record for $name within category ".
89             $self->uploadcategorycode. ", but file was missing.";
90     } elsif( ! unlink($file) ) {
91         warn "Problem while deleting: $file";
92     }
93     return $retval;
94 }
95
96 =head3 full_path
97
98 Returns the fully qualified path name for an uploaded file.
99
100 =cut
101
102 sub full_path {
103     my ( $self ) = @_;
104     my $path = File::Spec->catfile(
105         $self->permanent?
106             $self->permanent_directory: $self->temporary_directory,
107         $self->dir,
108         $self->hashvalue. '_'. $self->filename,
109     );
110     return $path;
111 }
112
113 =head3 file_handle
114
115 Returns a file handle for an uploaded file.
116
117 =cut
118
119 sub file_handle {
120     my ( $self ) = @_;
121     $self->{_file_handle} = IO::File->new( $self->full_path, "r" );
122     return if !$self->{_file_handle};
123     $self->{_file_handle}->binmode;
124     return $self->{_file_handle};
125 }
126
127 =head3 httpheaders
128
129 httpheaders returns http headers for a retrievable upload.
130
131 Will be extended by report 14282
132
133 =cut
134
135 sub httpheaders {
136     my ( $self ) = @_;
137     return (
138         '-type'       => 'application/octet-stream',
139         '-attachment' => $self->filename,
140     );
141 }
142
143 =head2 CLASS METHODS
144
145 =head3 permanent_directory
146
147 Returns root directory for permanent storage
148
149 =cut
150
151 sub permanent_directory {
152     my ( $class ) = @_;
153     return C4::Context->config('upload_path');
154 }
155
156 =head3 tmp_directory
157
158 Returns root directory for temporary storage
159
160 =cut
161
162 sub temporary_directory {
163     my ( $class ) = @_;
164     return File::Spec->tmpdir;
165 }
166
167 =head3 _type
168
169 Returns name of corresponding DBIC resultset
170
171 =cut
172
173 sub _type {
174     return 'UploadedFile';
175 }
176
177 =head1 AUTHOR
178
179 Marcel de Rooy (Rijksmuseum)
180
181 Koha Development Team
182
183 =cut
184
185 1;