13ec2eaadbceee1f16ae575212b93b2782bc459a
[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
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.
11 #
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.
16 #
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>.
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     return $retval if $params->{keep_file};
79
80     if( ! -e $file ) {
81         if ( $self->permanent ) {
82             warn "Removing record for $name within category ".
83                 $self->uploadcategorycode. ", but file was missing.";
84         }
85     } elsif( ! unlink($file) ) {
86         warn "Problem while deleting: $file";
87     }
88     return $retval;
89 }
90
91 =head3 full_path
92
93 Returns the fully qualified path name for an uploaded file.
94
95 =cut
96
97 sub full_path {
98     my ( $self ) = @_;
99     my $path = File::Spec->catfile(
100         $self->permanent
101             ? $self->permanent_directory
102             : C4::Context->temporary_directory,
103         $self->dir,
104         $self->hashvalue. '_'. $self->filename,
105     );
106     return $path;
107 }
108
109 =head3 file_handle
110
111 Returns a file handle for an uploaded file.
112
113 =cut
114
115 sub file_handle {
116     my ( $self ) = @_;
117     $self->{_file_handle} = IO::File->new( $self->full_path, "r" );
118     return if !$self->{_file_handle};
119     $self->{_file_handle}->binmode;
120     return $self->{_file_handle};
121 }
122
123 =head3 httpheaders
124
125 httpheaders returns http headers for a retrievable upload.
126
127 Will be extended by report 14282
128
129 =cut
130
131 sub httpheaders {
132     my ( $self ) = @_;
133     if( $self->filename =~ /\.pdf$/ ) {
134         return (
135             '-type'       => 'application/pdf',
136             'Content-Disposition' => 'inline; filename="'.$self->filename.'"',
137         );
138     } else {
139         return (
140             '-type'       => 'application/octet-stream',
141             '-attachment' => $self->filename,
142         );
143     }
144 }
145
146 =head2 CLASS METHODS
147
148 =head3 permanent_directory
149
150 Returns root directory for permanent storage
151
152 =cut
153
154 sub permanent_directory {
155     my ( $class ) = @_;
156     return C4::Context->config('upload_path');
157 }
158
159 =head3 _type
160
161 Returns name of corresponding DBIC resultset
162
163 =cut
164
165 sub _type {
166     return 'UploadedFile';
167 }
168
169 =head1 AUTHOR
170
171 Marcel de Rooy (Rijksmuseum)
172
173 Koha Development Team
174
175 =cut
176
177 1;