View | Details | Raw Unified | Return to bug 17501
Collapse All | Expand All

(-)a/Koha/Upload.pm (-33 / +6 lines)
Lines 26-31 Koha::Upload - Facilitate file uploads (temporary and permanent) Link Here
26
=head1 SYNOPSIS
26
=head1 SYNOPSIS
27
27
28
    use Koha::Upload;
28
    use Koha::Upload;
29
    use Koha::UploadedFile;
29
    use Koha::UploadedFiles;
30
    use Koha::UploadedFiles;
30
31
31
    # add an upload (see tools/upload-file.pl)
32
    # add an upload (see tools/upload-file.pl)
Lines 34-48 Koha::Upload - Facilitate file uploads (temporary and permanent) Link Here
34
    my $cgi = $upload->cgi;
35
    my $cgi = $upload->cgi;
35
    # Do something with $upload->count, $upload->result or $upload->err
36
    # Do something with $upload->count, $upload->result or $upload->err
36
37
37
    # get some upload records (in staff)
38
    # get some upload records (in staff) via Koha::UploadedFiles
38
    my @uploads1 = Koha::UploadedFiles->search({ filename => $name });
39
    my @uploads1 = Koha::UploadedFiles->search({ filename => $name });
39
    my @uploads2 = Koha::UploadedFiles->search_term({ term => $term });
40
    my @uploads2 = Koha::UploadedFiles->search_term({ term => $term });
40
41
41
    # staff download
42
    # staff download (via Koha::UploadedFile[s])
42
    my $rec = Koha::UploadedFiles->find( $id );
43
    my $rec = Koha::UploadedFiles->find( $id );
43
    my $fh = $rec->file_handle;
44
    my $fh = $rec->file_handle;
44
    my @hdr = Koha::Upload->httpheaders( $rec->filename );
45
    print Encode::encode_utf8( $input->header( $rec->httpheaders ) );
45
    print Encode::encode_utf8( $input->header( @hdr ) );
46
    while( <$fh> ) { print $_; }
46
    while( <$fh> ) { print $_; }
47
    $fh->close;
47
    $fh->close;
48
48
Lines 56-63 Koha::Upload - Facilitate file uploads (temporary and permanent) Link Here
56
    The module has been revised to use Koha::Object[s]; the delete method
56
    The module has been revised to use Koha::Object[s]; the delete method
57
    has been moved to Koha::UploadedFile[s], as well as the get method.
57
    has been moved to Koha::UploadedFile[s], as well as the get method.
58
58
59
=head1 INSTANCE METHODS
60
61
=cut
59
=cut
62
60
63
use constant KOHA_UPLOAD => 'koha_upload';
61
use constant KOHA_UPLOAD => 'koha_upload';
Lines 80-85 use Koha::UploadedFiles; Link Here
80
78
81
__PACKAGE__->mk_ro_accessors( qw|| );
79
__PACKAGE__->mk_ro_accessors( qw|| );
82
80
81
=head1 INSTANCE METHODS
82
83
=head2 new
83
=head2 new
84
84
85
    Returns new object based on Class::Accessor.
85
    Returns new object based on Class::Accessor.
Lines 160-192 sub err { Link Here
160
160
161
=head1 CLASS METHODS
161
=head1 CLASS METHODS
162
162
163
=head2 getCategories
164
165
    getCategories returns a list of upload category codes and names
166
167
=cut
168
169
sub getCategories {
170
    my ( $class ) = @_;
171
    my $cats = C4::Koha::GetAuthorisedValues('UPLOAD');
172
    [ map {{ code => $_->{authorised_value}, name => $_->{lib} }} @$cats ];
173
}
174
175
=head2 httpheaders
176
177
    httpheaders returns http headers for a retrievable upload
178
    Will be extended by report 14282
179
180
=cut
181
182
sub httpheaders {
183
    my ( $class, $name ) = @_;
184
    return (
185
        '-type'       => 'application/octet-stream',
186
        '-attachment' => $name,
187
    );
188
}
189
190
=head2 allows_add_by
163
=head2 allows_add_by
191
164
192
    allows_add_by checks if $userid has permission to add uploaded files
165
    allows_add_by checks if $userid has permission to add uploaded files
(-)a/Koha/UploadedFile.pm (-1 / +28 lines)
Lines 101-109 sub file_handle { Link Here
101
    return $self->{_file_handle};
101
    return $self->{_file_handle};
102
}
102
}
103
103
104
=head3 httpheaders
105
106
    httpheaders returns http headers for a retrievable upload
107
    Will be extended by report 14282
108
109
=cut
110
111
sub httpheaders {
112
    my ( $self ) = @_;
113
    return (
114
        '-type'       => 'application/octet-stream',
115
        '-attachment' => $self->filename,
116
    );
117
}
118
104
=head2 CLASS METHODS
119
=head2 CLASS METHODS
105
120
106
=head3 root_directory
121
=head3 permanent_directory
107
122
108
=cut
123
=cut
109
124
Lines 121-126 sub temporary_directory { Link Here
121
    return File::Spec->tmpdir;
136
    return File::Spec->tmpdir;
122
}
137
}
123
138
139
=head3 getCategories
140
141
    getCategories returns a list of upload category codes and names
142
143
=cut
144
145
sub getCategories {
146
    my ( $class ) = @_;
147
    my $cats = C4::Koha::GetAuthorisedValues('UPLOAD');
148
    [ map {{ code => $_->{authorised_value}, name => $_->{lib} }} @$cats ];
149
}
150
124
=head3 _type
151
=head3 _type
125
152
126
Returns name of corresponding DBIC resultset
153
Returns name of corresponding DBIC resultset
(-)a/opac/opac-retrieve-file.pl (-3 / +1 lines)
Lines 24-30 use Encode; Link Here
24
use C4::Auth;
24
use C4::Auth;
25
use C4::Context;
25
use C4::Context;
26
use C4::Output;
26
use C4::Output;
27
use Koha::Upload;
28
use Koha::UploadedFiles;
27
use Koha::UploadedFiles;
29
28
30
my $input = CGI::->new;
29
my $input = CGI::->new;
Lines 46-53 if( !$rec || !$fh ) { Link Here
46
    $template->param( hash => $hash );
45
    $template->param( hash => $hash );
47
    output_html_with_http_headers $input, $cookie, $template->output;
46
    output_html_with_http_headers $input, $cookie, $template->output;
48
} else {
47
} else {
49
    my @hdr = Koha::Upload->httpheaders( $rec->filename );
48
    print Encode::encode_utf8( $input->header( $rec->httpheaders ) );
50
    print Encode::encode_utf8( $input->header( @hdr ) );
51
    while( <$fh> ) {
49
    while( <$fh> ) {
52
        print $_;
50
        print $_;
53
    }
51
    }
(-)a/t/db_dependent/Upload.t (-2 / +4 lines)
Lines 11-16 use t::lib::TestBuilder; Link Here
11
use C4::Context;
11
use C4::Context;
12
use Koha::Database;
12
use Koha::Database;
13
use Koha::Upload;
13
use Koha::Upload;
14
use Koha::UploadedFile;
14
use Koha::UploadedFiles;
15
use Koha::UploadedFiles;
15
16
16
my $schema  = Koha::Database->new->schema;
17
my $schema  = Koha::Database->new->schema;
Lines 191-201 sub test06 { #search_term with[out] private flag Link Here
191
}
192
}
192
193
193
sub test07 { #simple test for httpheaders and getCategories
194
sub test07 { #simple test for httpheaders and getCategories
194
    my @hdrs = Koha::Upload->httpheaders('does_not_matter_yet');
195
    my $rec = Koha::UploadedFiles->search_term({ term => 'file' })->next;
196
    my @hdrs = $rec->httpheaders;
195
    is( @hdrs == 4 && $hdrs[1] =~ /application\/octet-stream/, 1, 'Simple test for httpheaders');
197
    is( @hdrs == 4 && $hdrs[1] =~ /application\/octet-stream/, 1, 'Simple test for httpheaders');
196
    my $builder = t::lib::TestBuilder->new;
198
    my $builder = t::lib::TestBuilder->new;
197
    $builder->build({ source => 'AuthorisedValue', value => { category => 'UPLOAD', authorised_value => 'HAVE_AT_LEAST_ONE', lib => 'Hi there' } });
199
    $builder->build({ source => 'AuthorisedValue', value => { category => 'UPLOAD', authorised_value => 'HAVE_AT_LEAST_ONE', lib => 'Hi there' } });
198
    my $cat = Koha::Upload->getCategories;
200
    my $cat = Koha::UploadedFile->getCategories;
199
    is( @$cat >= 1, 1, 'getCategories returned at least one category' );
201
    is( @$cat >= 1, 1, 'getCategories returned at least one category' );
200
}
202
}
201
203
(-)a/tools/upload.pl (-7 / +5 lines)
Lines 23-29 use JSON; Link Here
23
23
24
use C4::Auth;
24
use C4::Auth;
25
use C4::Output;
25
use C4::Output;
26
use Koha::Upload;
26
use Koha::UploadedFile;
27
use Koha::UploadedFiles;
27
use Koha::UploadedFiles;
28
28
29
my $input  = CGI::->new;
29
my $input  = CGI::->new;
Lines 52-58 $template->param( Link Here
52
if ( $op eq 'new' ) {
52
if ( $op eq 'new' ) {
53
    $template->param(
53
    $template->param(
54
        mode             => 'new',
54
        mode             => 'new',
55
        uploadcategories => Koha::Upload->getCategories,
55
        uploadcategories => Koha::UploadedFile->getCategories,
56
    );
56
    );
57
    output_html_with_http_headers $input, $cookie, $template->output;
57
    output_html_with_http_headers $input, $cookie, $template->output;
58
58
Lines 93-99 if ( $op eq 'new' ) { Link Here
93
    $template->param(
93
    $template->param(
94
        mode             => 'deleted',
94
        mode             => 'deleted',
95
        msg              => $msg,
95
        msg              => $msg,
96
        uploadcategories => Koha::Upload->getCategories,
96
        uploadcategories => Koha::UploadedFile->getCategories,
97
    );
97
    );
98
    output_html_with_http_headers $input, $cookie, $template->output;
98
    output_html_with_http_headers $input, $cookie, $template->output;
99
99
Lines 107-118 if ( $op eq 'new' ) { Link Here
107
        $template->param(
107
        $template->param(
108
            mode             => 'new',
108
            mode             => 'new',
109
            msg              => JSON::to_json( { $id => 5 } ),
109
            msg              => JSON::to_json( { $id => 5 } ),
110
            uploadcategories => Koha::Upload->getCategories,
110
            uploadcategories => Koha::UploadedFile->getCategories,
111
        );
111
        );
112
        output_html_with_http_headers $input, $cookie, $template->output;
112
        output_html_with_http_headers $input, $cookie, $template->output;
113
    } else {
113
    } else {
114
        my @hdr = Koha::Upload->httpheaders( $rec->filename );
114
        print Encode::encode_utf8( $input->header( $rec->httpheaders ) );
115
        print Encode::encode_utf8( $input->header(@hdr) );
116
        while (<$fh>) {
115
        while (<$fh>) {
117
            print $_;
116
            print $_;
118
        }
117
        }
119
- 

Return to bug 17501