Bug 17501: Remove Koha::Upload::get from Koha::Upload
[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 Koha::Database;
24
25 use parent qw(Koha::Object);
26
27 =head1 NAME
28
29 Koha::UploadedFile - Koha::Object class for single uploaded file
30
31 =head1 SYNOPSIS
32
33 use Koha::UploadedFile;
34
35 =head1 DESCRIPTION
36
37 Description
38
39 =head1 METHODS
40
41 =head2 INSTANCE METHODS
42
43 =head3 delete
44
45 Delete uploaded file.
46 It deletes not only the record, but also the actual file (unless you pass
47 the keep_file parameter).
48
49 Returns filename on successful delete or undef.
50
51 =cut
52
53 sub delete {
54     my ( $self, $params ) = @_;
55
56     my $name = $self->filename;
57     my $file = $self->full_path;
58
59     if( $params->{keep_file} ) {
60         return $name if $self->SUPER::delete;
61     } elsif( !-e $file ) { # we will just delete the record
62         warn "Removing record for $name within category ".
63             $self->uploadcategorycode. ", but file was missing.";
64         return $name if $self->SUPER::delete;
65     } elsif( unlink($file) ) {
66         return $name if $self->SUPER::delete;
67     } else {
68         warn "Problem while deleting: $file";
69     }
70     return; # something went wrong
71 }
72
73 =head3 full_path
74
75 Returns the fully qualified path name for an uploaded file.
76
77 =cut
78
79 sub full_path {
80     my ( $self ) = @_;
81     my $path = File::Spec->catfile(
82         $self->permanent?
83             $self->permanent_directory: $self->temporary_directory,
84         $self->dir,
85         $self->hashvalue. '_'. $self->filename,
86     );
87     return $path;
88 }
89
90 =head3 file_handle
91
92 Returns a file handle for an uploaded file.
93
94 =cut
95
96 sub file_handle {
97     my ( $self ) = @_;
98     $self->{_file_handle} = IO::File->new( $self->full_path, "r" );
99     return if !$self->{_file_handle};
100     $self->{_file_handle}->binmode;
101     return $self->{_file_handle};
102 }
103
104 =head2 CLASS METHODS
105
106 =head3 root_directory
107
108 =cut
109
110 sub permanent_directory {
111     my ( $class ) = @_;
112     return C4::Context->config('upload_path');
113 }
114
115 =head3 tmp_directory
116
117 =cut
118
119 sub temporary_directory {
120     my ( $class ) = @_;
121     return File::Spec->tmpdir;
122 }
123
124 =head3 _type
125
126 Returns name of corresponding DBIC resultset
127
128 =cut
129
130 sub _type {
131     return 'UploadedFile';
132 }
133
134 =head1 AUTHOR
135
136 Marcel de Rooy (Rijksmuseum)
137
138 Koha Development Team
139
140 =cut
141
142 1;