Browse Source
This patch makes the following changes to achieve that: [1] Use Upload.pm in stage-marc-import.pl, upload-cover-image.pl, offline_circ/process_koc.pl and enqueue_koc.pl. [2] A new file-upload.js replaces file-upload.inc in the associated template. We now use ajax to get progress figures instead of launching perl script upload-file-progress. The js changes now also allow for aborting a file upload. [3] Adds a tools/upload script and template. It allows to upload multiple files at once. [4] Makes upload-file return error messages in JSON. For a multiple upload, we could have some files with errors and others without errors. The upload is now marked as Failed only if there was no upload at all. [5] The upload plugin is converted to use tools/upload with plugin param. Deleting an upload is now presented via the search results form. NOTE: In editing the process_koc.tt I noticed that the form enqueuefile was hidden and no longer used (with associated code in process_koc.pl). When a file has been uploaded, I display the form again (with the Apply directly button). The code still works. NOTE: We fix an error in upload-file from one of the patches of bug 6874. The userid of the Koha admin user is passed to haspermission, but we should pick the userid from the session. NOTE: Bug 14686 will add a specific permission for tools/upload.pl, and will add the tools/upload script to the Tools menu. For now, you need edit_catalogue to start upload.pl and you will additionally need a permission like upload_local_cover_images to successfully upload a new file. Test plan: [1] Upload a marc file in stage-marc-import. (This is temp storage.) [2] Check new entry in table uploaded_files. Look for the file in your temporary directory (/tmp ?), subfolder koha_upload. Bonus: Remove permissions on this subfolder. Retry, check error and restore permissions again. [3] Upload another (larger) file and abort the upload. Check table and directory again. You should have a partial file, but no record. [4] Verify that Stage for import still works as expected. [5] Test Upload local cover image. (Enable OPACLocalCoverImages.) You can test an individual image or a zip file including images and a file called datalink.txt (with lines biblionumber,filename). [6] Test uploading a offline circulation file: Enable AllowOfflineCirculation, and create a koc file (plain text): Line1: Version=1.0\tA=1\tB=2 Line2: 2015-08-06 08:00:00 345\treturn\t[barcode] Note: Replace tabs and barcode. The number of tabs is essential! Checkout the item with your barcode. Go to Offline circulation file upload. Upload and click Apply directly. Checkout again. Upload again, click Add to offline circulation queue. [7] Upload three files via tools/upload.pl with a category and marked as public. Check the results in the table. Verify that you can download the file in OPAC without being logged in. [8] Pick one new file and one of the files of step 7. Upload them in the same category. One upload should succeed. Check for reported error. [9] Connect upload.pl to field 856$u. Goto Cataloguing editor. In an empty 856$u, click the tag editor. Upload a file and click Choose. Save the record. Open the record in the OPAC and click the link. Copy this link to your clipboard for next step. [A] Go back to editor. Click the tag editor on the same 856 field. Choose for Delete. Open the link in your clipboard again. Error message? [B] Check the process of upload, search, download and delete of an upload with some diacritical characters in the filename. (Bonus points for adding special chars in the category code.) Note: You can add categories via authorized values, UPLOAD key. Signed-off-by: Mirko Tietgen <mirko@abunchofthings.net> Signed-off-by: Julian Maurice <julian.maurice@biblibre.com> Bug 14321: [QA Follow-up] Perltidy upload.pl Run perltidy -pro=xt/perltidyrc on tools/upload.pl. No other changes. Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl> Signed-off-by: Julian Maurice <julian.maurice@biblibre.com> Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org> Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>3.22.x
14 changed files with 711 additions and 220 deletions
@ -0,0 +1,32 @@ |
|||
function AjaxUpload ( input, progressbar, xtra, callback ) { |
|||
// input and progressbar are jQuery objects
|
|||
// callback is the callback function for completion
|
|||
var formData= new FormData(); |
|||
$.each( input.prop('files'), function( dx, file ) { |
|||
formData.append( "uploadfile", file ); |
|||
}); |
|||
var xhr= new XMLHttpRequest(); |
|||
var url= '/cgi-bin/koha/tools/upload-file.pl?' + xtra; |
|||
progressbar.val( 0 ); |
|||
progressbar.next('.fileuploadpercent').text( '0' ); |
|||
xhr.open('POST', url, true); |
|||
xhr.upload.onprogress = function (e) { |
|||
var p = Math.round( (e.loaded/e.total) * 100 ); |
|||
progressbar.val( p ); |
|||
progressbar.next('.fileuploadpercent').text( p ); |
|||
} |
|||
xhr.onload = function (e) { |
|||
var data = JSON.parse( xhr.responseText ); |
|||
if( data.status == 'done' ) { |
|||
progressbar.val( 100 ); |
|||
progressbar.next('.fileuploadpercent').text( '100' ); |
|||
} |
|||
callback( data.status, data.fileid, data.errors ); |
|||
} |
|||
xhr.onerror = function (e) { |
|||
// Probably only fires for network failure
|
|||
alert('An error occurred while uploading.'); |
|||
} |
|||
xhr.send( formData ); |
|||
return xhr; |
|||
} |
@ -0,0 +1,342 @@ |
|||
[% INCLUDE 'doc-head-open.inc' %] |
|||
[% USE Koha %] |
|||
[% IF plugin %] |
|||
<title>Upload plugin</title> |
|||
[% ELSE %] |
|||
<title>Koha › Tools › Upload</title> |
|||
[% END %] |
|||
[% INCLUDE 'doc-head-close.inc' %] |
|||
|
|||
[% BLOCK plugin_pars %] |
|||
[% IF plugin %] |
|||
<input type="hidden" name="plugin" value="1" /> |
|||
<input type="hidden" name="index" value="[% index %]" /> |
|||
[% END %] |
|||
[% END %] |
|||
|
|||
[% BLOCK breadcrumbs %] |
|||
<div id="breadcrumbs"> |
|||
<a href="/cgi-bin/koha/mainpage.pl">Home</a> |
|||
› |
|||
<a href="/cgi-bin/koha/tools/tools-home.pl">Tools</a> |
|||
› |
|||
<a href="/cgi-bin/koha/tools/upload.pl">Upload</a> |
|||
› |
|||
<span id="lastbreadcrumb"> |
|||
[% IF mode=='new' || mode =='deleted'%] |
|||
Add new upload or search |
|||
[% ELSE %] |
|||
Results |
|||
[% END %] |
|||
</span> |
|||
</div> |
|||
[% END %] |
|||
|
|||
[% BLOCK form_new %] |
|||
<form method="post" action="[% SCRIPT_NAME %]" id="uploadfile" enctype="multipart/form-data"> |
|||
[% PROCESS plugin_pars %] |
|||
<fieldset class="rows" id="uploadform"> |
|||
<legend>Upload new files</legend> |
|||
<ol> |
|||
<li> |
|||
<div id="fileuploadform"> |
|||
<label for="fileToUpload">Select files: </label> |
|||
<input type="file" id="fileToUpload" name="fileToUpload" multiple/> |
|||
</div> |
|||
</li> |
|||
[% IF uploadcategories %] |
|||
<li> |
|||
<label for="uploadcategory">Category: </label> |
|||
<select id="uploadcategory" name="uploadcategory"> |
|||
[% IF !plugin %] |
|||
<option value="" disabled hidden selected></option> |
|||
[% END %] |
|||
[% FOREACH cat IN uploadcategories %] |
|||
<option value="[% cat.code %]">[% cat.name %]</option> |
|||
[% END %] |
|||
</select> |
|||
</li> |
|||
[% END %] |
|||
[% IF !plugin %] |
|||
<li> |
|||
[% IF uploadcategories %] |
|||
<div class="hint">Note: For temporary uploads do not select a category.</div> |
|||
[% ELSE %] |
|||
<div class="hint">Note: No upload categories have been defined. All uploads will be marked as temporary.</div> |
|||
[% END %] |
|||
</li> |
|||
[% END %] |
|||
<li> |
|||
[% IF plugin %] |
|||
<input type="hidden" id="public" name="public" value="1"/> |
|||
[% ELSE %] |
|||
<label> </label> |
|||
<input type="checkbox" id="public" name="public"> |
|||
Allow public downloads |
|||
</input> |
|||
[% END %] |
|||
</li> |
|||
</ol> |
|||
<fieldset class="action"> |
|||
<button id="fileuploadbutton" onclick="StartUpload(); return false;">Upload</button> |
|||
<button id="fileuploadcancel" onclick="CancelUpload(); return false;">Cancel</button> |
|||
</fieldset> |
|||
</fieldset> |
|||
<div id="fileuploadpanel"> |
|||
<div id="fileuploadstatus">Upload progress: |
|||
<progress id="fileuploadprogress" min="0" max="100" value="0"> |
|||
</progress> |
|||
<span class="fileuploadpercent">0</span>% |
|||
</div> |
|||
<div id="fileuploadfailed"></div> |
|||
</div> |
|||
</form> |
|||
[% END %] |
|||
|
|||
[% BLOCK form_search %] |
|||
<form method="post" id="searchfile" action="[% SCRIPT_NAME %]" enctype="multipart/form-data"> |
|||
[% PROCESS plugin_pars %] |
|||
<input type="hidden" name="op" value="search"/> |
|||
<fieldset class="rows"> |
|||
<legend>Search uploads by name or hashvalue</legend> |
|||
<ol> |
|||
<li> |
|||
<label for="searchupload">Search term: </label> |
|||
<input type="text" id="term" name="term" value=""/> |
|||
</li> |
|||
<li> |
|||
<fieldset class="action"> |
|||
<button id="searchbutton" onclick="return CheckSearch();" class="submit">Search</button> |
|||
</fieldset> |
|||
</li> |
|||
</ol> |
|||
</fieldset> |
|||
</form> |
|||
[% END %] |
|||
|
|||
[% BLOCK submitter %] |
|||
<form id="submitter" style="display:none;" method="post"> |
|||
[% PROCESS plugin_pars %] |
|||
<input type="hidden" name="op" id="op" value=""/> |
|||
<input type="hidden" name="id" id="id" value="" /> |
|||
<input type="hidden" name="msg" id="msg" value="" /> |
|||
</form> |
|||
[% END %] |
|||
|
|||
[% BLOCK closer %] |
|||
[% IF plugin %] |
|||
<form id="closer"> |
|||
<fieldset class="action"> |
|||
<button onclick="window.close();return false;">Close</button> |
|||
</fieldset> |
|||
</form> |
|||
[% END %] |
|||
[% END %] |
|||
|
|||
[% BLOCK newsearch %] |
|||
<form id="newsearch"> |
|||
<fieldset class="action"> |
|||
<button onclick="SubmitMe('new'); return false;">New search</button> |
|||
[% IF plugin %] |
|||
<button onclick="window.close();return false;">Close</button> |
|||
[% END %] |
|||
</fieldset> |
|||
</form> |
|||
[% END %] |
|||
|
|||
[% BLOCK table_results %] |
|||
<table> |
|||
<thead> |
|||
<tr> |
|||
<th>Filename</td> |
|||
<th>Size</td> |
|||
<th>Hashvalue</td> |
|||
<th>Category</td> |
|||
[% IF !plugin %]<th>Public</td>[% END %] |
|||
[% IF !plugin %]<th>Temporary</td>[% END %] |
|||
<th>Actions</td> |
|||
</tr> |
|||
</thead> |
|||
<tbody> |
|||
[% FOREACH record IN uploads %] |
|||
<tr> |
|||
<td>[% record.name %]</td> |
|||
<td>[% record.filesize %]</td> |
|||
<td>[% record.hashvalue %]</td> |
|||
<td>[% record.categorycode %]</td> |
|||
[% IF !plugin %] |
|||
<td>[% IF record.public %]Yes[% ELSE %]No[% END %]</td> |
|||
<td>[% IF record.permanent %]No[% ELSE %]Yes[% END %]</td> |
|||
[% END %] |
|||
<td> |
|||
[% IF plugin %] |
|||
<a href="" onclick="Choose('[% record.hashvalue %]'); return false;">Choose</a> |
|||
[% END %] |
|||
<a href="" onclick="SubmitMe( 'download', [% record.id %] ); return false;">Download</a> |
|||
<a href="" onclick="ClearField(); SubmitMe( 'delete', [% record.id %] ); return false;">Delete</a> |
|||
</td> |
|||
</tr> |
|||
[% END %] |
|||
</tbody> |
|||
</table> |
|||
[% END %] |
|||
|
|||
<style type="text/css"> |
|||
#fileuploadstatus,#fileuploadfailed { display : none; } |
|||
#fileuploadstatus { margin:.4em; } |
|||
#fileuploadprogress { width:150px;height:10px;border:1px solid #666;background:url('[% interface %]/[% theme %]/img/progress.png') -300px 0px no-repeat; } |
|||
</style> |
|||
|
|||
<script type="text/javascript"> |
|||
//<![CDATA[ |
|||
var errMESSAGES = [ |
|||
"Error 0: Not in use", |
|||
_("This file already exists (in this category)."), |
|||
_("File could not be created. Check permissions."), |
|||
_("Your koha-conf.xml does not contain a valid upload_path."), |
|||
_("No temporary directory found."), |
|||
_("File could not be read."), |
|||
_("File has been deleted."), |
|||
_("File could not be deleted."), |
|||
]; |
|||
//]]> |
|||
</script> |
|||
<script type="text/javascript" src="[% themelang %]/js/file-upload.js"></script> |
|||
<script type="text/javascript"> |
|||
//<![CDATA[ |
|||
function StartUpload() { |
|||
if( $('#fileToUpload').prop('files').length == 0 ) return; |
|||
$('#fileToUpload').prop('disabled',true); |
|||
$('#fileuploadbutton').hide(); |
|||
$("#fileuploadcancel").show(); |
|||
$("#fileuploadfailed").html(''); |
|||
$("#myalerts").hide(''); |
|||
$("#myalerts").html(''); |
|||
$("#fileuploadstatus").show(); |
|||
$("#uploadedfileid").val(''); |
|||
$("#searchfile").hide(); |
|||
$("#lastbreadcrumb").text( _("Add a new upload") ); |
|||
|
|||
var cat, xtra=''; |
|||
if( $("#uploadcategory").val() ) |
|||
cat = encodeURIComponent( $("#uploadcategory").val() ); |
|||
if( cat ) xtra= 'category=' + cat + '&'; |
|||
[% IF plugin %] |
|||
xtra = xtra + 'public=1&temp=0'; |
|||
[% ELSE %] |
|||
if( !cat ) xtra = 'temp=1&'; |
|||
if( $('#public').prop('checked') ) xtra = xtra + 'public=1'; |
|||
[% END %] |
|||
xhr= AjaxUpload( $('#fileToUpload'), $('#fileuploadprogress'), xtra, cbUpload ); |
|||
} |
|||
function CancelUpload() { |
|||
if( xhr ) xhr.abort(); |
|||
$("#fileuploadstatus").hide(); |
|||
$('#fileToUpload').prop('disabled', false); |
|||
$('#fileuploadbutton').show(); |
|||
$("#fileuploadcancel").hide(); |
|||
$("#fileuploadfailed").show(); |
|||
$("#fileuploadfailed").text( _("Upload status: Cancelled ") ); |
|||
} |
|||
function cbUpload( status, fileid, err ) { |
|||
$('#fileToUpload').prop('disabled', false); |
|||
if( status=='done' ) { |
|||
var e = err? JSON.stringify(err): ''; |
|||
SubmitMe( 'search', fileid, e ); |
|||
} else { |
|||
$('#fileuploadbutton').show(); |
|||
$("#fileuploadcancel").hide(); |
|||
$("#fileuploadstatus").hide(); |
|||
$("#fileuploadfailed").show(); |
|||
$("#fileuploadfailed").html( _("Upload status: ") + |
|||
( status=='failed'? _("Failed"): |
|||
( status=='denied'? _("Denied"): status )) |
|||
); |
|||
ShowAlerts( err ); |
|||
} |
|||
} |
|||
function ShowAlerts(err) { |
|||
var str = ''; |
|||
for( var file in err ) { |
|||
str= str + '<p>' + file + ': ' + |
|||
errMESSAGES[ err[file] ] + '</p>'; |
|||
} |
|||
if( str ) { |
|||
$('#myalerts').html(str); |
|||
$('#myalerts').show(); |
|||
} |
|||
} |
|||
function CheckSearch() { |
|||
if( $("#term").val()=="" ) { |
|||
alert( _("Please enter a search term.") ); |
|||
return false; |
|||
} |
|||
return true; |
|||
} |
|||
function SubmitMe(op, id, msg ) { |
|||
$("#submitter #op").val( op ); |
|||
$("#submitter #id").val( id ); |
|||
$("#submitter #msg").val( msg ); |
|||
$("#submitter").submit(); |
|||
} |
|||
function ClearField() { |
|||
[% IF plugin %] |
|||
$(window.opener.document).find('#[% index %]').val( '' ); |
|||
[% END %] |
|||
} |
|||
function Choose(hashval) { |
|||
var res = '[% Koha.Preference('OPACBaseURL') %]'; |
|||
res = res.replace( /\/$/, ''); |
|||
res = res + '/cgi-bin/koha/opac-retrieve-file.pl?id=' + hashval; |
|||
[% IF index %] |
|||
$(window.opener.document).find('#[% index %]').val( res ); |
|||
[% END %] |
|||
window.close(); |
|||
} |
|||
$(document).ready(function() { |
|||
[% IF msg %] |
|||
ShowAlerts( [% msg %] ); |
|||
[% END %] |
|||
$("#fileuploadcancel").hide(); |
|||
}); |
|||
//]]> |
|||
</script> |
|||
</head> |
|||
|
|||
<body id="tools_upload" class="tools"> |
|||
[% IF !plugin %] |
|||
[% INCLUDE 'header.inc' %] |
|||
[% INCLUDE 'cat-search.inc' %] |
|||
[% PROCESS breadcrumbs %] |
|||
[% END %] |
|||
|
|||
<div id="doc3" class="yui-t2"> |
|||
<div id="bd"> |
|||
<div id="yui-main"> |
|||
<div class="yui-b"> |
|||
|
|||
<h1>Upload</h1> |
|||
|
|||
<div class="dialog alert" id="myalerts" style="display:none;"></div> |
|||
|
|||
[% PROCESS submitter %] |
|||
[% IF mode == 'new' || mode == 'deleted' %] |
|||
[% PROCESS form_new %] |
|||
[% PROCESS form_search %] |
|||
[% PROCESS closer %] |
|||
[% ELSIF mode == 'report' %] |
|||
[% IF uploads %] |
|||
<h3>Your request gave the following results:</h3> |
|||
[% PROCESS table_results %] |
|||
[% PROCESS closer %] |
|||
[% ELSE %] |
|||
<h4>Sorry, your request had no results.</h4> |
|||
[% PROCESS newsearch %] |
|||
[% END %] |
|||
[% END %] |
|||
|
|||
</div> |
|||
</div> |
|||
</div> |
|||
|
|||
[% INCLUDE 'intranet-bottom.inc' %] |
@ -0,0 +1,6 @@ |
|||
[%# This template is called only for a very simple error message %] |
|||
<html> |
|||
<body> |
|||
Your search [% IF hash %]for [% hash %][% END %] was not successful. |
|||
</body> |
|||
</html> |
@ -0,0 +1,101 @@ |
|||
#!/usr/bin/perl |
|||
|
|||
# This file is part of Koha. |
|||
# |
|||
# Copyright (C) 2015 Rijksmuseum |
|||
# |
|||
# Koha is free software; you can redistribute it and/or modify it |
|||
# under the terms of the GNU General Public License as published by |
|||
# the Free Software Foundation; either version 3 of the License, or |
|||
# (at your option) any later version. |
|||
# |
|||
# Koha is distributed in the hope that it will be useful, but |
|||
# WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
# GNU General Public License for more details. |
|||
# |
|||
# You should have received a copy of the GNU General Public License |
|||
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
|||
|
|||
use Modern::Perl; |
|||
use CGI qw/-utf8/; |
|||
use JSON; |
|||
|
|||
use C4::Auth; |
|||
use C4::Output; |
|||
use Koha::Upload; |
|||
|
|||
my $input = CGI::->new; |
|||
my $op = $input->param('op') // 'new'; |
|||
my $plugin = $input->param('plugin'); |
|||
my $index = $input->param('index'); # MARC editor input field id |
|||
my $term = $input->param('term'); |
|||
my $id = $input->param('id'); |
|||
my $msg = $input->param('msg'); |
|||
|
|||
my ( $template, $loggedinuser, $cookie ) = get_template_and_user( |
|||
{ template_name => "tools/upload.tt", |
|||
query => $input, |
|||
type => "intranet", |
|||
authnotrequired => 0, |
|||
flagsrequired => { editcatalogue => '*' }, |
|||
} |
|||
); |
|||
|
|||
$template->param( |
|||
plugin => $plugin, |
|||
index => $index, |
|||
); |
|||
|
|||
my $upar = $plugin ? { public => 1 } : {}; |
|||
if ( $op eq 'new' ) { |
|||
$template->param( |
|||
mode => 'new', |
|||
uploadcategories => Koha::Upload->getCategories, |
|||
); |
|||
output_html_with_http_headers $input, $cookie, $template->output; |
|||
} elsif ( $op eq 'search' ) { |
|||
my $h = $id ? { id => $id } : { term => $term }; |
|||
my @uploads = Koha::Upload->new($upar)->get($h); |
|||
$template->param( |
|||
mode => 'report', |
|||
msg => $msg, |
|||
uploads => \@uploads, |
|||
); |
|||
output_html_with_http_headers $input, $cookie, $template->output; |
|||
} elsif ( $op eq 'delete' ) { |
|||
|
|||
# delete only takes the id parameter |
|||
my $upl = Koha::Upload->new($upar); |
|||
my ($fn) = $upl->delete( { id => $id } ); |
|||
my $e = $upl->err; |
|||
my $msg = |
|||
$fn ? JSON::to_json( { $fn => 6 } ) |
|||
: $e ? JSON::to_json($e) |
|||
: undef; |
|||
$template->param( |
|||
mode => 'deleted', |
|||
msg => $msg, |
|||
uploadcategories => $upl->getCategories, |
|||
); |
|||
output_html_with_http_headers $input, $cookie, $template->output; |
|||
} elsif ( $op eq 'download' ) { |
|||
my $upl = Koha::Upload->new($upar); |
|||
my $rec = $upl->get( { id => $id, filehandle => 1 } ); |
|||
my $fh = $rec->{fh}; |
|||
if ( !$rec || !$fh ) { |
|||
$template->param( |
|||
mode => 'new', |
|||
msg => JSON::to_json( { $id => 5 } ), |
|||
uploadcategories => $upl->getCategories, |
|||
); |
|||
output_html_with_http_headers $input, $cookie, $template->output; |
|||
} else { |
|||
my @hdr = $upl->httpheaders( $rec->{name} ); |
|||
print Encode::encode_utf8( $input->header(@hdr) ); |
|||
while (<$fh>) { |
|||
print $_; |
|||
} |
|||
$fh->close; |
|||
} |
|||
} |
Loading…
Reference in new issue