Bug 17501: Move getCategories and httpheaders from Upload.pm
[koha.git] / Koha / UploadedFile.pm
blob085037d9b28bd4623b45de6a1ad74362bce7f688
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 (unless you pass
47 the keep_file parameter).
49 Returns filename on successful delete or undef.
51 =cut
53 sub delete {
54 my ( $self, $params ) = @_;
56 my $name = $self->filename;
57 my $file = $self->full_path;
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";
70 return; # something went wrong
73 =head3 full_path
75 Returns the fully qualified path name for an uploaded file.
77 =cut
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,
87 return $path;
90 =head3 file_handle
92 Returns a file handle for an uploaded file.
94 =cut
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};
104 =head3 httpheaders
106 httpheaders returns http headers for a retrievable upload
107 Will be extended by report 14282
109 =cut
111 sub httpheaders {
112 my ( $self ) = @_;
113 return (
114 '-type' => 'application/octet-stream',
115 '-attachment' => $self->filename,
119 =head2 CLASS METHODS
121 =head3 permanent_directory
123 =cut
125 sub permanent_directory {
126 my ( $class ) = @_;
127 return C4::Context->config('upload_path');
130 =head3 tmp_directory
132 =cut
134 sub temporary_directory {
135 my ( $class ) = @_;
136 return File::Spec->tmpdir;
139 =head3 getCategories
141 getCategories returns a list of upload category codes and names
143 =cut
145 sub getCategories {
146 my ( $class ) = @_;
147 my $cats = C4::Koha::GetAuthorisedValues('UPLOAD');
148 [ map {{ code => $_->{authorised_value}, name => $_->{lib} }} @$cats ];
151 =head3 _type
153 Returns name of corresponding DBIC resultset
155 =cut
157 sub _type {
158 return 'UploadedFile';
161 =head1 AUTHOR
163 Marcel de Rooy (Rijksmuseum)
165 Koha Development Team
167 =cut