From 50be9048e67e4f47141bc70ef5fc1b526b8a325c Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 13 Mar 2019 00:20:06 -0300 Subject: [PATCH] Bug 22508: Add the ability to prefill 856$u with the direct URL of the file When uploading a file using the cataloguing plugin, the current value used to fill 856$u (or others) in is OPACBaseURL/cgi-bin/koha/opac-retrieve-file.pl?id=a-big-hash This patch add the ability to define a custom and direct URL to this file. To accomplish that we need 2 new entries in our config file: * upload_public_path * upload_public_url The first one will be the local path where the files will be stored, that will be publicly accessible. The second one is the root of the URL which links to ths public dir path. Test plan: 0. Define the 2 new config entries, like: /home/vagrant/kohaclone/koha-tmpl/public_path http://catalogue.kohadev.org/public_path Then restart memcached+plack to take into account the changes 1. Link the cataloguing plugin 'upload.pl' to 856$u 2. Edit a record, 856$u > click the plugin link 3. A new "Use a public path:" appears if the config is set, tick it and upload a file 4. Pick "Choose the direct url" 5. 856$u will be prefilled with http://catalogue.kohadev.org/public_path/$hash_value instead of the usual http://catalogue.kohadev.org/cgi-bin/koha/opac-retrieve-file.pl?id=$hash_value 6. Use the link and confirm that you can access the file. Note: This patch is not ready for inclusion yet and is published to get feedback. There are several things that will block its inclusion: * TODO: "Delete" button must delete the file * See TODO and FIXME in the patch * Tests and POD for new methods are missing * Variable naming is not perfect * In discussion - what about the upload of files from outside of the cataloguing plugin? * TODO: add check to the about page (?) * FIXME: in upload_public_path the files are not suffixed by the filename as it is for upload_path. We certainly want to be consistent here. Signed-off-by: Hugo Agud --- Koha/UploadedFile.pm | 32 ++++++++++++++++++++ Koha/Uploader.pm | 4 ++- .../intranet-tmpl/prog/en/modules/tools/upload.tt | 34 +++++++++++++++------- tools/upload-file.pl | 14 ++++++++- tools/upload.pl | 6 ++-- 5 files changed, 75 insertions(+), 15 deletions(-) diff --git a/Koha/UploadedFile.pm b/Koha/UploadedFile.pm index 20fc608c68..982cdc6ae3 100644 --- a/Koha/UploadedFile.pm +++ b/Koha/UploadedFile.pm @@ -148,6 +148,38 @@ sub httpheaders { } } +sub url { + my ($self) = @_; + my $OPACBaseURL = C4::Context->preference('OPACBaseURL'); + $OPACBaseURL =~ s|/$||; + return "$OPACBaseURL/cgi-bin/koha/opac-retrieve-file.pl?id=" . $self->hashvalue; +} + +sub local_public_path { + my ($self) = @_; + my $upload_public_path = C4::Context->config('upload_public_path'); + return unless $upload_public_path; + $upload_public_path =~ s|/$||; + my $filepath = "$upload_public_path/" . $self->hashvalue; + return $filepath; +} + +sub has_local_public_path { + my ($self) = @_; + my $filepath = $self->local_public_path; + return $filepath if -e $filepath; +} + +sub direct_url { + my ( $self ) = @_; + # TODO It could start with '/' and we prefix with OPACBaseURL + my $upload_public_path = C4::Context->config('upload_public_url'); + return unless $upload_public_path; + $upload_public_path =~ s|/$||; + my $direct_url = "$upload_public_path/" . $self->hashvalue; + return $direct_url; +} + =head2 CLASS METHODS =head3 permanent_directory diff --git a/Koha/Uploader.pm b/Koha/Uploader.pm index a4baa6a30b..d226e3be66 100644 --- a/Koha/Uploader.pm +++ b/Koha/Uploader.pm @@ -31,7 +31,7 @@ Koha::Uploader - Facilitate file uploads (temporary and permanent) # add an upload (see tools/upload-file.pl) # the public flag allows retrieval via OPAC - my $upload = Koha::Uploader->new( public => 1, category => 'A' ); + my $upload = Koha::Uploader->new( public => 1, category => 'A', use_public_path => 1 ); my $cgi = $upload->cgi; # Do something with $upload->count, $upload->result or $upload->err @@ -205,6 +205,7 @@ sub _init { $self->{category} = $params->{category} || KOHA_UPLOAD; } + $self->{use_public_path} = $params->{use_public_path}; $self->{files} = {}; $self->{uid} = C4::Context->userenv->{number} if C4::Context->userenv; $self->{public} = $params->{public}? 1: undef; @@ -293,6 +294,7 @@ sub _register { public => $self->{public}, permanent => $self->{temporary}? 0: 1, })->store; + $self->{files}->{$filename}->{id} = $rec->id if $rec; } diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/upload.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/upload.tt index 6bd010ad6f..f863f315a2 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/upload.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/upload.tt @@ -72,8 +72,12 @@
  • [% IF plugin %] + [% IF can_use_public_path %] + + + [% END %] [% ELSE %] - + [% END %]
  • @@ -175,7 +179,10 @@ [% END %] [% IF plugin %] - + + [% IF record.has_local_public_path %] + + [% END %] [% END %] [% IF record.owner == owner || CAN_user_tools_upload_manage %] @@ -270,12 +277,17 @@ $("#searchfile").hide(); $("#lastbreadcrumb").text( _("Add a new upload") ); - var cat, xtra=''; + var cat = xtra = use_public_path = ''; if( $("#uploadcategory").val() ) cat = encodeURIComponent( $("#uploadcategory").val() ); if( cat ) xtra= 'category=' + cat + '&'; + + if( $("#use_public_path").is(":checked") ) { + xtra = xtra + 'use_public_path=1&'; + } + [% IF plugin %] - xtra = xtra + 'public=1&temp=0'; + xtra = xtra + 'public=1&temp=0&'; [% ELSE %] if( !cat ) xtra = 'temp=1&'; if( $('#public').prop('checked') ) xtra = xtra + 'public=1'; @@ -372,12 +384,9 @@ $(window.opener.document).find('#[% index | html %]').val( '' ); [% END %] } - function Choose(hashval) { - var res = '[% Koha.Preference('OPACBaseURL') | html %]'; - res = res.replace( /\/$/, ''); - res = res + '/cgi-bin/koha/opac-retrieve-file.pl?id=' + hashval; + function Choose(url) { [% IF index %] - $(window.opener.document).find('#[% index | html %]').val( res ); + $(window.opener.document).find('#[% index | html %]').val( url ); [% END %] window.close(); } @@ -408,8 +417,11 @@ }); $(".choose_entry").on("click",function(e){ e.preventDefault(); - var record_hashvalue = $(this).data("record-hashvalue"); - Choose( record_hashvalue ); + Choose( $(this).data("record-url") ); + }); + $(".choose_entry_public_path").on("click",function(e){ + e.preventDefault(); + Choose( $(this).data("record-url") ); }); $(".download_entry").on("click",function(e){ e.preventDefault(); diff --git a/tools/upload-file.pl b/tools/upload-file.pl index 86ef2c5dc2..dcbe1afdbd 100755 --- a/tools/upload-file.pl +++ b/tools/upload-file.pl @@ -24,6 +24,7 @@ use CGI::Cookie; use Encode; use JSON; use URI::Escape; +use File::Copy qw( copy ); use C4::Context; use C4::Auth qw/check_cookie_auth haspermission/; @@ -53,6 +54,17 @@ if( !$upload || !$upload->cgi || !$upload->count ) { # not one upload succeeded send_reply( 'failed', undef, $upload? $upload->err: undef ); } else { + my $upload_public_path = C4::Context->config('upload_public_path'); + # FIXME The copy must be done in Koha::Uploader, but where? + # I (Joubu) did not find a place where the file was completely written + if ( $upload->{use_public_path} && $upload_public_path ) { + for my $file ( values %{ $upload->{files} } ) { + my $uploaded_file = Koha::UploadedFiles->find($file->{id}); + next unless $uploaded_file; + copy $uploaded_file->full_path, $uploaded_file->local_public_path + or warn sprintf("Copy %s to %s failed (%s)", $uploaded_file->filename, $upload_public_path, $!); + } + } # in case of multiple uploads, at least one got through send_reply( 'done', $upload->result, $upload->err ); } @@ -75,7 +87,7 @@ sub upload_pars { # this sub parses QUERY_STRING in order to build the $qstr = Encode::decode_utf8( uri_unescape( $qstr ) ); # category could include a utf8 character my $rv = {}; - foreach my $p ( qw[public category temp] ) { + foreach my $p ( qw[public category temp use_public_path] ) { if( $qstr =~ /(^|&)$p=(\w+)(&|$)/ ) { $rv->{$p} = $2; } diff --git a/tools/upload.pl b/tools/upload.pl index afb7c516f9..ace55ab784 100755 --- a/tools/upload.pl +++ b/tools/upload.pl @@ -54,8 +54,10 @@ $template->param( ); if ( $op eq 'new' ) { + my $can_use_public_path = C4::Context->config('upload_public_path'); $template->param( mode => 'new', + can_use_public_path => $can_use_public_path, ); output_html_with_http_headers $input, $cookie, $template->output; @@ -65,7 +67,7 @@ if ( $op eq 'new' ) { my @id = split /,/, $id; foreach my $recid (@id) { my $rec = Koha::UploadedFiles->find( $recid ); - push @$uploads, $rec->unblessed + push @$uploads, $rec # FIXME replace find with search and let the DB do that if $rec && ( $rec->public || !$plugin ); # Do not show private uploads in the plugin mode (:editor) } @@ -73,7 +75,7 @@ if ( $op eq 'new' ) { $uploads = Koha::UploadedFiles->search_term({ term => $term, $plugin? (): ( include_private => 1 ), - })->unblessed; + }); } $template->param( -- 2.11.0