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

(-)a/Koha/UploadedFile.pm (+32 lines)
Lines 148-153 sub httpheaders { Link Here
148
    }
148
    }
149
}
149
}
150
150
151
sub url {
152
    my ($self) = @_;
153
    my $OPACBaseURL = C4::Context->preference('OPACBaseURL');
154
    $OPACBaseURL =~ s|/$||;
155
    return "$OPACBaseURL/cgi-bin/koha/opac-retrieve-file.pl?id=" . $self->hashvalue;
156
}
157
158
sub local_public_path {
159
    my ($self) = @_;
160
    my $upload_public_path = C4::Context->config('upload_public_path');
161
    return unless $upload_public_path;
162
    $upload_public_path =~ s|/$||;
163
    my $filepath = "$upload_public_path/" . $self->hashvalue;
164
    return $filepath;
165
}
166
167
sub has_local_public_path {
168
    my ($self) = @_;
169
    my $filepath = $self->local_public_path;
170
    return $filepath if -e $filepath;
171
}
172
173
sub direct_url {
174
    my ( $self ) = @_;
175
    # TODO It could start with '/' and we prefix with OPACBaseURL
176
    my $upload_public_path = C4::Context->config('upload_public_url');
177
    return unless $upload_public_path;
178
    $upload_public_path =~ s|/$||;
179
    my $direct_url = "$upload_public_path/" . $self->hashvalue;
180
    return $direct_url;
181
}
182
151
=head2 CLASS METHODS
183
=head2 CLASS METHODS
152
184
153
=head3 permanent_directory
185
=head3 permanent_directory
(-)a/Koha/Uploader.pm (-1 / +3 lines)
Lines 31-37 Koha::Uploader - Facilitate file uploads (temporary and permanent) Link Here
31
31
32
    # add an upload (see tools/upload-file.pl)
32
    # add an upload (see tools/upload-file.pl)
33
    # the public flag allows retrieval via OPAC
33
    # the public flag allows retrieval via OPAC
34
    my $upload = Koha::Uploader->new( public => 1, category => 'A' );
34
    my $upload = Koha::Uploader->new( public => 1, category => 'A', use_public_path => 1 );
35
    my $cgi = $upload->cgi;
35
    my $cgi = $upload->cgi;
36
    # Do something with $upload->count, $upload->result or $upload->err
36
    # Do something with $upload->count, $upload->result or $upload->err
37
37
Lines 205-210 sub _init { Link Here
205
        $self->{category} = $params->{category} || KOHA_UPLOAD;
205
        $self->{category} = $params->{category} || KOHA_UPLOAD;
206
    }
206
    }
207
207
208
    $self->{use_public_path} = $params->{use_public_path};
208
    $self->{files} = {};
209
    $self->{files} = {};
209
    $self->{uid} = C4::Context->userenv->{number} if C4::Context->userenv;
210
    $self->{uid} = C4::Context->userenv->{number} if C4::Context->userenv;
210
    $self->{public} = $params->{public}? 1: undef;
211
    $self->{public} = $params->{public}? 1: undef;
Lines 293-298 sub _register { Link Here
293
        public    => $self->{public},
294
        public    => $self->{public},
294
        permanent => $self->{temporary}? 0: 1,
295
        permanent => $self->{temporary}? 0: 1,
295
    })->store;
296
    })->store;
297
296
    $self->{files}->{$filename}->{id} = $rec->id if $rec;
298
    $self->{files}->{$filename}->{id} = $rec->id if $rec;
297
}
299
}
298
300
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/upload.tt (-11 / +23 lines)
Lines 72-79 Link Here
72
        <li>
72
        <li>
73
            [% IF plugin %]
73
            [% IF plugin %]
74
                <input type="hidden" id="public" name="public" value="1"/>
74
                <input type="hidden" id="public" name="public" value="1"/>
75
                [% IF can_use_public_path %]
76
                    <label id="use_public_path_cb" for="use_public_path">Use a public path:</label>
77
                    <input type="checkbox" id="use_public_path" name="use_public_path" />
78
                [% END %]
75
            [% ELSE %]
79
            [% ELSE %]
76
                <label id="public_cb">Allow public downloads:</label>
80
                <label id="public_cb" for="public">Allow public downloads:</label>
77
                <input type="checkbox" id="public" name="public" />
81
                <input type="checkbox" id="public" name="public" />
78
            [% END %]
82
            [% END %]
79
        </li>
83
        </li>
Lines 175-181 Link Here
175
        [% END %]
179
        [% END %]
176
        <td class="actions">
180
        <td class="actions">
177
            [% IF plugin %]
181
            [% IF plugin %]
178
                <button class="btn btn-default btn-xs choose_entry" data-record-hashvalue="[% record.hashvalue | html %]"><i class="fa fa-plus"></i> Choose</button>
182
                <button class="btn btn-default btn-xs choose_entry" data-record-url="[% record.url | html %]"><i class="fa fa-plus"></i> Choose</button>
183
                [% IF record.has_local_public_path %]
184
                    <button class="btn btn-default btn-xs choose_entry_public_path" data-record-url="[% record.direct_url | html %]"><i class="fa fa-plus"></i> Choose the direct url</button>
185
                [% END %]
179
            [% END %]
186
            [% END %]
180
            <button class="btn btn-default btn-xs download_entry" data-record-id="[% record.id | html %]"><i class="fa fa-download"></i> Download</button>
187
            <button class="btn btn-default btn-xs download_entry" data-record-id="[% record.id | html %]"><i class="fa fa-download"></i> Download</button>
181
            [% IF record.owner == owner || CAN_user_tools_upload_manage %]
188
            [% IF record.owner == owner || CAN_user_tools_upload_manage %]
Lines 270-281 Link Here
270
            $("#searchfile").hide();
277
            $("#searchfile").hide();
271
            $("#lastbreadcrumb").text( _("Add a new upload") );
278
            $("#lastbreadcrumb").text( _("Add a new upload") );
272
279
273
            var cat, xtra='';
280
            var cat = xtra = use_public_path = '';
274
            if( $("#uploadcategory").val() )
281
            if( $("#uploadcategory").val() )
275
                cat = encodeURIComponent( $("#uploadcategory").val() );
282
                cat = encodeURIComponent( $("#uploadcategory").val() );
276
            if( cat ) xtra= 'category=' + cat + '&';
283
            if( cat ) xtra= 'category=' + cat + '&';
284
285
            if( $("#use_public_path").is(":checked") ) {
286
                xtra = xtra + 'use_public_path=1&';
287
            }
288
277
            [% IF plugin %]
289
            [% IF plugin %]
278
                xtra = xtra + 'public=1&temp=0';
290
                xtra = xtra + 'public=1&temp=0&';
279
            [% ELSE %]
291
            [% ELSE %]
280
                if( !cat ) xtra = 'temp=1&';
292
                if( !cat ) xtra = 'temp=1&';
281
                if( $('#public').prop('checked') ) xtra = xtra + 'public=1';
293
                if( $('#public').prop('checked') ) xtra = xtra + 'public=1';
Lines 372-383 Link Here
372
                $(window.opener.document).find('#[% index | html %]').val( '' );
384
                $(window.opener.document).find('#[% index | html %]').val( '' );
373
            [% END %]
385
            [% END %]
374
        }
386
        }
375
        function Choose(hashval) {
387
        function Choose(url) {
376
            var res = '[% Koha.Preference('OPACBaseURL') | html %]';
377
            res = res.replace( /\/$/, '');
378
            res = res + '/cgi-bin/koha/opac-retrieve-file.pl?id=' + hashval;
379
            [% IF index %]
388
            [% IF index %]
380
                $(window.opener.document).find('#[% index | html %]').val( res );
389
                $(window.opener.document).find('#[% index | html %]').val( url );
381
            [% END %]
390
            [% END %]
382
            window.close();
391
            window.close();
383
        }
392
        }
Lines 408-415 Link Here
408
            });
417
            });
409
            $(".choose_entry").on("click",function(e){
418
            $(".choose_entry").on("click",function(e){
410
                e.preventDefault();
419
                e.preventDefault();
411
                var record_hashvalue = $(this).data("record-hashvalue");
420
                Choose( $(this).data("record-url") );
412
                Choose( record_hashvalue );
421
            });
422
            $(".choose_entry_public_path").on("click",function(e){
423
                e.preventDefault();
424
                Choose( $(this).data("record-url") );
413
            });
425
            });
414
            $(".download_entry").on("click",function(e){
426
            $(".download_entry").on("click",function(e){
415
                e.preventDefault();
427
                e.preventDefault();
(-)a/tools/upload-file.pl (-1 / +13 lines)
Lines 24-29 use CGI::Cookie; Link Here
24
use Encode;
24
use Encode;
25
use JSON;
25
use JSON;
26
use URI::Escape;
26
use URI::Escape;
27
use File::Copy qw( copy );
27
28
28
use C4::Context;
29
use C4::Context;
29
use C4::Auth qw/check_cookie_auth haspermission/;
30
use C4::Auth qw/check_cookie_auth haspermission/;
Lines 53-58 if( !$upload || !$upload->cgi || !$upload->count ) { Link Here
53
    # not one upload succeeded
54
    # not one upload succeeded
54
    send_reply( 'failed', undef, $upload? $upload->err: undef );
55
    send_reply( 'failed', undef, $upload? $upload->err: undef );
55
} else {
56
} else {
57
    my $upload_public_path = C4::Context->config('upload_public_path');
58
    # FIXME The copy must be done in Koha::Uploader, but where?
59
    # I (Joubu) did not find a place where the file was completely written
60
    if ( $upload->{use_public_path} && $upload_public_path ) {
61
        for my $file ( values %{ $upload->{files} } ) {
62
            my $uploaded_file = Koha::UploadedFiles->find($file->{id});
63
            next unless $uploaded_file;
64
            copy $uploaded_file->full_path, $uploaded_file->local_public_path
65
              or warn sprintf("Copy %s to %s failed (%s)", $uploaded_file->filename, $upload_public_path, $!);
66
        }
67
    }
56
    # in case of multiple uploads, at least one got through
68
    # in case of multiple uploads, at least one got through
57
    send_reply( 'done', $upload->result, $upload->err );
69
    send_reply( 'done', $upload->result, $upload->err );
58
}
70
}
Lines 75-81 sub upload_pars { # this sub parses QUERY_STRING in order to build the Link Here
75
    $qstr = Encode::decode_utf8( uri_unescape( $qstr ) );
87
    $qstr = Encode::decode_utf8( uri_unescape( $qstr ) );
76
    # category could include a utf8 character
88
    # category could include a utf8 character
77
    my $rv = {};
89
    my $rv = {};
78
    foreach my $p ( qw[public category temp] ) {
90
    foreach my $p ( qw[public category temp use_public_path] ) {
79
        if( $qstr =~ /(^|&)$p=(\w+)(&|$)/ ) {
91
        if( $qstr =~ /(^|&)$p=(\w+)(&|$)/ ) {
80
            $rv->{$p} = $2;
92
            $rv->{$p} = $2;
81
        }
93
        }
(-)a/tools/upload.pl (-3 / +4 lines)
Lines 54-61 $template->param( Link Here
54
);
54
);
55
55
56
if ( $op eq 'new' ) {
56
if ( $op eq 'new' ) {
57
    my $can_use_public_path = C4::Context->config('upload_public_path');
57
    $template->param(
58
    $template->param(
58
        mode             => 'new',
59
        mode             => 'new',
60
        can_use_public_path => $can_use_public_path,
59
    );
61
    );
60
    output_html_with_http_headers $input, $cookie, $template->output;
62
    output_html_with_http_headers $input, $cookie, $template->output;
61
63
Lines 65-71 if ( $op eq 'new' ) { Link Here
65
        my @id = split /,/, $id;
67
        my @id = split /,/, $id;
66
        foreach my $recid (@id) {
68
        foreach my $recid (@id) {
67
            my $rec = Koha::UploadedFiles->find( $recid );
69
            my $rec = Koha::UploadedFiles->find( $recid );
68
            push @$uploads, $rec->unblessed
70
            push @$uploads, $rec # FIXME replace find with search and let the DB do that
69
                if $rec && ( $rec->public || !$plugin );
71
                if $rec && ( $rec->public || !$plugin );
70
                # Do not show private uploads in the plugin mode (:editor)
72
                # Do not show private uploads in the plugin mode (:editor)
71
        }
73
        }
Lines 73-79 if ( $op eq 'new' ) { Link Here
73
        $uploads = Koha::UploadedFiles->search_term({
75
        $uploads = Koha::UploadedFiles->search_term({
74
            term => $term,
76
            term => $term,
75
            $plugin? (): ( include_private => 1 ),
77
            $plugin? (): ( include_private => 1 ),
76
        })->unblessed;
78
        });
77
    }
79
    }
78
80
79
    $template->param(
81
    $template->param(
80
- 

Return to bug 22508