MARC import: part 1 of adding support for large files

Signed-off-by: Joshua Ferraro <jmf@liblime.com>
This commit is contained in:
Galen Charlton 2007-11-23 14:44:49 -06:00 committed by Joshua Ferraro
parent 7ee7effc89
commit 2e1b9fd295
5 changed files with 433 additions and 5 deletions

View file

@ -0,0 +1,39 @@
<!-- AJAX file upload stuff -->
<script type="text/javascript" src="<!-- TMPL_VAR name="themelang" -->/lib/jquery/plugins/ajaxfileupload.js"></script>
<script type="text/javascript" src="/intranet-tmpl/prog/en/lib/jquery/plugins/ajaxfileupload.js"></script>
<script type="text/javascript">
//<![CDATA
function updateProgress() {
$.getJSON("/cgi-bin/koha/tools/upload-file-progress.pl", function(json) {
$("#fileuploadprogress").text(json.progress + '%');
if (json.progress < 100) {
setTimeout("updateProgress()",200);
}
});
}
function ajaxFileUpload()
{
$("#fileuploadstatus").show();
setTimeout("updateProgress()",2000);
$.ajaxFileUpload (
{
url:'/cgi-bin/koha/tools/upload-file.pl',
secureuri:false,
global:false,
fileElementId:'fileToUpload',
dataType: 'json',
success: function (data, status) {
$("#uploadedfileid").val(data.fileid);
},
error: function (data, status, e) {
alert(e);
}
}
)
updateProgress();
return false;
}
//]]>
</script>

View file

@ -0,0 +1,200 @@
jQuery.extend({
createUploadIframe: function(id, uri)
{
//create frame
var frameId = 'jUploadFrame' + id;
if(window.ActiveXObject) {
var io = document.createElement('<iframe id="' + frameId + '" name="' + frameId + '" />');
if(typeof uri== 'boolean'){
io.src = 'javascript:false';
}
else if(typeof uri== 'string'){
io.src = uri;
}
}
else {
var io = document.createElement('iframe');
io.id = frameId;
io.name = frameId;
}
io.style.position = 'absolute';
io.style.top = '-1000px';
io.style.left = '-1000px';
document.body.appendChild(io);
return io
},
createUploadForm: function(id, fileElementId)
{
//create form
var formId = 'jUploadForm' + id;
var fileId = 'jUploadFile' + id;
var form = $('<form action="" method="POST" name="' + formId + '" id="' + formId + '" enctype="multipart/form-data"></form>');
var oldElement = $('#' + fileElementId);
var newElement = $(oldElement).clone();
$(oldElement).attr('id', fileId);
$(oldElement).before(newElement);
$(oldElement).appendTo(form);
//set attributes
$(form).css('position', 'absolute');
$(form).css('top', '-1200px');
$(form).css('left', '-1200px');
$(form).appendTo('body');
return form;
},
ajaxFileUpload: function(s) {
// TODO introduce global settings, allowing the client to modify them for all requests, not only timeout
s = jQuery.extend({}, jQuery.ajaxSettings, s);
var id = new Date().getTime()
var form = jQuery.createUploadForm(id, s.fileElementId);
var io = jQuery.createUploadIframe(id, s.secureuri);
var frameId = 'jUploadFrame' + id;
var formId = 'jUploadForm' + id;
// Watch for a new set of requests
if ( s.global && ! jQuery.active++ )
{
jQuery.event.trigger( "ajaxStart" );
}
var requestDone = false;
// Create the request object
var xml = {}
if ( s.global )
jQuery.event.trigger("ajaxSend", [xml, s]);
// Wait for a response to come back
var uploadCallback = function(isTimeout)
{
var io = document.getElementById(frameId);
try
{
if(io.contentWindow)
{
xml.responseText = io.contentWindow.document.body?io.contentWindow.document.body.innerHTML:null;
xml.responseXML = io.contentWindow.document.XMLDocument?io.contentWindow.document.XMLDocument:io.contentWindow.document;
}else if(io.contentDocument)
{
xml.responseText = io.contentDocument.document.body?io.contentDocument.document.body.innerHTML:null;
xml.responseXML = io.contentDocument.document.XMLDocument?io.contentDocument.document.XMLDocument:io.contentDocument.document;
}
}catch(e)
{
jQuery.handleError(s, xml, null, e);
}
if ( xml || isTimeout == "timeout")
{
requestDone = true;
var status;
try {
status = isTimeout != "timeout" ? "success" : "error";
// Make sure that the request was successful or notmodified
if ( status != "error" )
{
// process the data (runs the xml through httpData regardless of callback)
var data = jQuery.uploadHttpData( xml, s.dataType );
// If a local callback was specified, fire it and pass it the data
if ( s.success )
s.success( data, status );
// Fire the global callback
if( s.global )
jQuery.event.trigger( "ajaxSuccess", [xml, s] );
} else
jQuery.handleError(s, xml, status);
} catch(e)
{
status = "error";
jQuery.handleError(s, xml, status, e);
}
// The request was completed
if( s.global )
jQuery.event.trigger( "ajaxComplete", [xml, s] );
// Handle the global AJAX counter
if ( s.global && ! --jQuery.active )
jQuery.event.trigger( "ajaxStop" );
// Process result
if ( s.complete )
s.complete(xml, status);
jQuery(io).unbind()
setTimeout(function()
{ try
{
$(io).remove();
$(form).remove();
} catch(e)
{
jQuery.handleError(s, xml, null, e);
}
}, 100)
xml = null
}
}
// Timeout checker
if ( s.timeout > 0 )
{
setTimeout(function(){
// Check to see if the request is still happening
if( !requestDone ) uploadCallback( "timeout" );
}, s.timeout);
}
try
{
// var io = $('#' + frameId);
var form = $('#' + formId);
$(form).attr('action', s.url);
$(form).attr('method', 'POST');
$(form).attr('target', frameId);
if(form.encoding)
{
form.encoding = 'multipart/form-data';
}
else
{
form.enctype = 'multipart/form-data';
}
$(form).submit();
} catch(e)
{
jQuery.handleError(s, xml, null, e);
}
if(window.attachEvent){
document.getElementById(frameId).attachEvent('onload', uploadCallback);
}
else{
document.getElementById(frameId).addEventListener('load', uploadCallback, false);
}
return {abort: function () {}};
},
uploadHttpData: function( r, type ) {
var data = !type;
data = type == "xml" || data ? r.responseXML : r.responseText;
// If the type is "script", eval it in global context
if ( type == "script" )
jQuery.globalEval( data );
// Get the JavaScript object, if JSON is used.
if ( type == "json" )
eval( "data = " + data );
// evaluate scripts within html
if ( type == "html" )
jQuery("<div>").html(data).evalScripts();
//alert($('param', data).each(function(){alert($(this).attr('value'));}));
return data;
}
})

View file

@ -1,6 +1,20 @@
<!-- TMPL_INCLUDE NAME="doc-head-open.inc" -->
<title>Koha &rsaquo; Tools &rsaquo; Stage MARC Records For Import</title>
<!-- TMPL_INCLUDE NAME="doc-head-close.inc" -->
<!-- TMPL_INCLUDE NAME="file-upload.inc" -->
<script type="text/javascript">
//<![CDATA[
function CheckForm(f) {
if (f.uploadedfileid.value == '') {
alert('Please upload a file first.');
} else {
f.submit();
}
}
//]]!>
</script>
</head>
<body>
<!-- TMPL_INCLUDE NAME="header.inc" -->
@ -41,14 +55,20 @@
<li>Select a MARC file to stage in the import reservoir. It will be parsed, and each valid record staged for later import into the catalogue.</li>
<li>You can enter a name for this import. It may be useful, when creating a biblio, to remember where the suggested MARC data comes from!</li>
</ul>
<form method="post" action="<!-- TMPL_VAR name="SCRIPT_NAME" -->" enctype="multipart/form-data">
<fieldset class="rows">
<legend>Stage records into the reservoir</legend><ol>
<li>
<label for="uploadmarc">Select the file to stage: </label>
<input type="file" id="uploadmarc" name="uploadmarc" /><br />
<div id="fileuploadform">
<form method="post" action="<!-- TMPL_VAR name="SCRIPT_NAME" -->" enctype="multipart/form-data">
<label for="fileToUpload">Select the file to stage: </label>
<input type="file" id="fileToUpload" name="fileToUpload" /><br />
<button clase="input" onclick="return ajaxFileUpload();">Upload file</button>
</form>
<div id="fileuploadstatus" style="display:none">Upload progress: <span id="fileuploadprogress">0<span>%</div>
</div>
</li>
<form method="post" action="<!-- TMPL_VAR name="SCRIPT_NAME" -->" enctype="multipart/form-data">
<input type="hidden" name="uploadedfileid" id="uploadedfileid" value="" />
<li>
<label for="comments">Notes about this file: </label>
<input type="text" id="comments" name="comments" />
@ -85,7 +105,7 @@
</li>
</ol>
</fieldset>
<fieldset class="action"><input type="submit" value="Stage for import" /></fieldset>
<fieldset class="action"><input type="button" id="mainformsubmit" onclick="CheckForm(this.form);" value="Stage for import" /></fieldset>
</form>
<!-- /TMPL_IF -->

59
tools/upload-file-progress.pl Executable file
View file

@ -0,0 +1,59 @@
#!/usr/bin/perl -w
# Copyright (C) 2007 LibLime
#
# This file is part of Koha.
#
# 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 2 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, write to the Free Software Foundation, Inc., 59 Temple Place,
# Suite 330, Boston, MA 02111-1307 USA
use strict;
# standard or CPAN modules used
use IO::File;
use CGI;
use CGI::Session;
use C4::Context;
use CGI::Cookie; # need to check cookies before
# having CGI parse the POST request
use Digest::MD5;
my %cookies = fetch CGI::Cookie;
my $sessionID = $cookies{'CGISESSID'}->value;
my $dbh = C4::Context->dbh;
# FIXME get correct session -- not just mysql
my $session = new CGI::Session("driver:MySQL", $sessionID, {Handle=>$dbh});
# FIXME - add authentication based on cookie
my $query = CGI->new;
my $fileid = $session->param('current_upload');
my $reported_progress = 0;
if (defined $fileid and $fileid ne "") {
my $progress = $session->param("$fileid.uploadprogress");
if (defined $progress) {
if ($progress eq "done") {
$reported_progress = 100;
} else {
$reported_progress = $progress;
}
}
}
my $reply = CGI->new("");
print $reply->header(-type => 'text/html');
# response will be sent back as JSON
print "{ progress: $reported_progress }";

110
tools/upload-file.pl Executable file
View file

@ -0,0 +1,110 @@
#!/usr/bin/perl -w
# Copyright (C) 2007 LibLime
#
# This file is part of Koha.
#
# 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 2 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, write to the Free Software Foundation, Inc., 59 Temple Place,
# Suite 330, Boston, MA 02111-1307 USA
use strict;
# standard or CPAN modules used
use IO::File;
use CGI;
use CGI::Session;
use C4::Context;
use CGI::Cookie; # need to check cookies before
# having CGI parse the POST request
use Digest::MD5;
my %cookies = fetch CGI::Cookie;
my $sessionID = $cookies{'CGISESSID'}->value;
my $dbh = C4::Context->dbh;
# FIXME get correct session -- not just mysql
my $session = new CGI::Session("driver:MySQL", $sessionID, {Handle=>$dbh});
# upload-file.pl must authenticate the user
# before processing the POST request,
# and quickly bounce if the user is
# not authorized. Consequently, unlike
# most of the other CGI scripts, upload-file.pl
# requires that the session cookie already
# have been created., $fileid, $tmp_file_name
# FIXME - add authentication based on cookie
my $fileid = Digest::MD5::md5_hex(Digest::MD5::md5_hex(time().{}.rand().{}.$$));
# FIXME - make staging area configurable
my $TEMPROOT = "/tmp";
my $OUTPUTDIR = "$TEMPROOT/$sessionID";
mkdir $OUTPUTDIR;
my $tmp_file_name = "$OUTPUTDIR/$fileid";
my $fh = new IO::File $tmp_file_name, "w";
unless (defined $fh) {
# FIXME - failed to create file for some reason
send_reply('failed', '', '');
exit 0;
}
$fh->binmode(); # for Windows compatibility
$session->param("$fileid.uploaded_tmpfile", $tmp_file_name);
$session->param('current_upload', $fileid);
$session->flush();
my $progress = 0;
my $first_chunk = 1;
my $max_size = $ENV{'CONTENT_LENGTH'}; # may not be the file size, exactly
my $query;
$|++;
$query = new CGI \&upload_hook, $session;
clean_up();
send_reply('done', $fileid, $tmp_file_name);
# FIXME - if possible, trap signal caused by user cancelling upload
# FIXME - something is wrong during cleanup: \t(in cleanup) Can't call method "commit" on unblessed reference at /usr/local/share/perl/5.8.8/CGI/Session/Driver/DBI.pm line 130 during global destruction.
exit 0;
sub clean_up {
$session->param("$fileid.uploadprogress", 'done');
$session->flush();
}
sub upload_hook {
my ($file_name, $buffer, $bytes_read, $session) = @_;
print $fh $buffer;
# stash received file name
if ($first_chunk) {
$session->param("$fileid.uploaded_filename", $file_name);
$session->flush();
$first_chunk = 0;
}
my $percentage = int(($bytes_read / $max_size) * 100);
if ($percentage > $progress) {
$progress = $percentage;
$session->param("$fileid.uploadprogress", $progress);
$session->flush();
}
}
sub send_reply {
my ($upload_status, $fileid, $tmp_file_name) = @_;
my $reply = CGI->new("");
print $reply->header(-type => 'text/html');
# response will be sent back as JSON
print "{ status: '$upload_status', fileid: '$fileid', tmp_file_name: '$tmp_file_name' }";
}