Bug 17501: Move Koha::Upload::delete to Koha::UploadedFile[s]
[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.
47
48 Returns filename on successful delete or undef.
49
50 =cut
51
52 sub delete {
53     my ( $self ) = @_;
54
55     my $name = $self->filename;
56     my $file = $self->full_path;
57
58     if( !-e $file ) { # we will just delete the record
59         warn "Removing record for $name within category ".
60             $self->uploadcategorycode. ", but file was missing.";
61         return $name if $self->SUPER::delete;
62     } elsif( unlink($file) ) {
63         return $name if $self->SUPER::delete;
64     } else {
65         warn "Problem while deleting: $file";
66     }
67     return; # something went wrong
68 }
69
70 =head3 full_path
71
72 Returns the fully qualified path name for an uploaded file.
73
74 =cut
75
76 sub full_path {
77     my ( $self ) = @_;
78     my $path = File::Spec->catfile(
79         $self->permanent?
80             $self->permanent_directory: $self->temporary_directory,
81         $self->dir,
82         $self->hashvalue. '_'. $self->filename,
83     );
84     return $path;
85 }
86
87 =head2 CLASS METHODS
88
89 =head3 root_directory
90
91 =cut
92
93 sub permanent_directory {
94     my ( $class ) = @_;
95     return C4::Context->config('upload_path');
96 }
97
98 =head3 tmp_directory
99
100 =cut
101
102 sub temporary_directory {
103     my ( $class ) = @_;
104     return File::Spec->tmpdir;
105 }
106
107 =head3 _type
108
109 Returns name of corresponding DBIC resultset
110
111 =cut
112
113 sub _type {
114     return 'UploadedFile';
115 }
116
117 =head1 AUTHOR
118
119 Marcel de Rooy (Rijksmuseum)
120
121 Koha Development Team
122
123 =cut
124
125 1;