Bug 17501: Move Koha::Upload::delete to Koha::UploadedFile[s]
[koha.git] / Koha / UploadedFile.pm
blob01d0580a87d3042c51d3643463129707ad09483b
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 Koha::Database;
25 use parent qw(Koha::Object);
27 =head1 NAME
29 Koha::UploadedFile - Koha::Object class for single uploaded file
31 =head1 SYNOPSIS
33 use Koha::UploadedFile;
35 =head1 DESCRIPTION
37 Description
39 =head1 METHODS
41 =head2 INSTANCE METHODS
43 =head3 delete
45 Delete uploaded file.
46 It deletes not only the record, but also the actual file.
48 Returns filename on successful delete or undef.
50 =cut
52 sub delete {
53 my ( $self ) = @_;
55 my $name = $self->filename;
56 my $file = $self->full_path;
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";
67 return; # something went wrong
70 =head3 full_path
72 Returns the fully qualified path name for an uploaded file.
74 =cut
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,
84 return $path;
87 =head2 CLASS METHODS
89 =head3 root_directory
91 =cut
93 sub permanent_directory {
94 my ( $class ) = @_;
95 return C4::Context->config('upload_path');
98 =head3 tmp_directory
100 =cut
102 sub temporary_directory {
103 my ( $class ) = @_;
104 return File::Spec->tmpdir;
107 =head3 _type
109 Returns name of corresponding DBIC resultset
111 =cut
113 sub _type {
114 return 'UploadedFile';
117 =head1 AUTHOR
119 Marcel de Rooy (Rijksmuseum)
121 Koha Development Team
123 =cut