Bugzilla – Attachment 41873 Details for
Bug 14321
Merge UploadedFile and UploadedFiles into Koha::Upload
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 14321: Using Koha::Upload in tools/upload-file.pl
Bug-14321-Using-KohaUpload-in-toolsupload-filepl.patch (text/plain), 30.58 KB, created by
Marcel de Rooy
on 2015-08-25 07:02:51 UTC
(
hide
)
Description:
Bug 14321: Using Koha::Upload in tools/upload-file.pl
Filename:
MIME Type:
Creator:
Marcel de Rooy
Created:
2015-08-25 07:02:51 UTC
Size:
30.58 KB
patch
obsolete
>From 7f42de7a1ba3bda0c0099a01b60030b67c7e92d6 Mon Sep 17 00:00:00 2001 >From: Marcel de Rooy <m.de.rooy@rijksmuseum.nl> >Date: Thu, 30 Jul 2015 19:19:44 +0200 >Subject: [PATCH] Bug 14321: Using Koha::Upload in tools/upload-file.pl >Content-Type: text/plain; charset=utf-8 > >This patch introduces Koha::Upload. It will replace the modules >C4::UploadedFile.pm and the new C4::UploadedFiles.pm (from BZ 6874). >This operation will be performed in a few steps. > >As a first step, we will use Koha::Upload instead of UploadedFile in the >following places. (In these three places files are uploaded temporarily.) > >[1] stage-marc-import.pl >[2] upload-cover-image.pl >[3] offline_circ/process_koc.pl and enqueue_koc.pl > >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. Also we do not need the jquery plugin >ajaxfileupload.js anymore. A subsequent patch will remove these files. > >The js changes now also allow for aborting a file upload. This feature has >been added to stage-marc-import only for now. > >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 2: The changes to upload-file and upload-file-progress in respect to >replacing the indirect syntax for CGI::Cookie come from bug 14589. > >Test plan: >[1] Upload a marc file in stage-marc-import. >[2] Check new entry in table uploaded_files. Look for the file in the > directory defined by upload_path in koha-conf.xml. >[3] Upload another (larger) file and abort the upload. Check table and > directory again. >[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. >--- > Koha/Upload.pm | 272 ++++++++++++++++++++ > koha-tmpl/intranet-tmpl/prog/en/js/file-upload.js | 32 +++ > .../prog/en/modules/offline_circ/process_koc.tt | 39 ++- > .../prog/en/modules/tools/stage-marc-import.tt | 64 ++++- > .../prog/en/modules/tools/upload-images.tt | 45 +++- > offline_circ/enqueue_koc.pl | 6 +- > offline_circ/process_koc.pl | 8 +- > tools/stage-marc-import.pl | 9 +- > tools/upload-cover-image.pl | 8 +- > tools/upload-file-progress.pl | 5 +- > tools/upload-file.pl | 57 ++-- > 11 files changed, 467 insertions(+), 78 deletions(-) > create mode 100644 Koha/Upload.pm > create mode 100644 koha-tmpl/intranet-tmpl/prog/en/js/file-upload.js > >diff --git a/Koha/Upload.pm b/Koha/Upload.pm >new file mode 100644 >index 0000000..c3a7e47 >--- /dev/null >+++ b/Koha/Upload.pm >@@ -0,0 +1,272 @@ >+package Koha::Upload; >+ >+# Copyright 2015 Rijksmuseum >+# >+# 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 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, write to the Free Software Foundation, Inc., >+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. >+ >+=head1 NAME >+ >+Koha::Upload - Facilitate file upload >+ >+=head1 SYNOPSIS >+ >+ use Koha::Upload; >+ >+=head1 DESCRIPTION >+ >+ This class >+ >+=head1 METHODS >+ >+=head2 new >+ >+ Create object (via Class::Accessor). >+ >+=head1 PROPERTIES >+ >+=head2 ??? >+ >+ ??? >+ >+=head1 ADDITIONAL COMMENTS >+ >+=cut >+ >+use constant KOHA_UPLOAD => 'koha_upload'; >+use constant BYTES_DIGEST => 2048; >+ >+use Modern::Perl; >+use CGI; # no utf8 flag, since it may interfere with binary uploads >+use Digest::MD5; >+use File::Spec; >+use IO::File; >+use Time::HiRes; >+ >+use base qw(Class::Accessor); >+ >+use C4::Context; >+ >+__PACKAGE__->mk_ro_accessors( qw| name >+|); >+ >+=head2 new >+ >+ Returns new object based on Class::Accessor. >+ >+=cut >+ >+sub new { >+ my ( $class, $params ) = @_; >+ my $self = $class->SUPER::new(); >+ $self->_init( $params ); >+ return $self; >+} >+ >+=head2 cgi >+ >+ Returns new object based on Class::Accessor. >+ >+=cut >+ >+sub cgi { >+ my ( $self ) = @_; >+ >+ # Next call handles the actual upload via CGI hook. >+ # The third parameter (0) below means: no CGI temporary storage. >+ # Cancelling an upload will make CGI abort the script; no problem, >+ # the file(s) without db entry will be removed later. >+ my $query = CGI::->new( sub { $self->_hook(@_); }, {}, 0 ); >+ if( $query ) { >+ $self->_done; >+ return $query; >+ } >+} >+ >+=head2 result >+ >+ Returns new object based on Class::Accessor. >+ >+=cut >+ >+sub result { >+ my ( $self ) = @_; >+ my @a = map { $self->{files}->{$_}->{id} } keys $self->{files}; >+ return join ',', @a; >+} >+ >+=head2 get >+ >+ Returns array >+ optional parameter: filehandle => 1 (name, path only by default) >+ >+=cut >+ >+sub get { >+ my ( $self, $params ) = @_; >+ my $temp= $self->_lookup( $params ); >+ my ( @rv, $res ); >+ foreach my $r ( @$temp ) { >+ $res->{name} = $r->{filename}; >+ $res->{path}= $self->_full_fname($r); >+ $res->{fh} = IO::File->new( $res->{path}, "r" ) >+ if $params->{filehandle}; >+ push @rv, $res; >+ last if !wantarray; >+ } >+ return wantarray? @rv: $res; >+} >+ >+sub DESTROY { >+} >+ >+# ************** INTERNAL ROUTINES ******************************************** >+ >+sub _init { >+ my ( $self, $params ) = @_; >+ >+ $self->{rootdir} = C4::Context->config('upload_path'); >+ $self->{tmpdir} = File::Spec->tmpdir; >+ if( exists $params->{tmp} || exists $params->{temp} || >+ !$params->{category} ) { >+ $self->{temporary} = 1; >+ $self->{category} = undef; >+ } else { >+ $self->{category} = $params->{category}; >+ } >+ >+ $self->{errors} = []; >+ $self->{files} = {}; >+ $self->{uid} = C4::Context->userenv->{number} if C4::Context->userenv; >+} >+ >+sub _fh { >+ my ( $self, $filename ) = @_; >+ if( $self->{files}->{$filename} ) { >+ return $self->{files}->{$filename}->{fh}; >+ } >+} >+ >+sub _create_file { >+ my ( $self, $filename ) = @_; >+ my $fh; >+ if( $self->{files}->{$filename} && >+ $self->{files}->{$filename}->{errcode} ) { >+ #skip >+ } elsif( $self->{temporary} && !$self->{tmpdir} ) { >+ $self->{files}->{$filename}->{errcode} = 2; >+ } else { >+ my $dir = $self->_dir; >+ my $fn = $self->{files}->{$filename}->{hash}. '_'. $filename; >+ $fh = IO::File->new( "$dir/$fn", "w"); >+ if( $fh ) { >+ $fh->binmode; >+ $self->{files}->{$filename}->{fh}= $fh; >+ } else { >+ $self->{files}->{$filename}->{errcode} = 1; >+ # push @{$self->{errors}}, [ 1, $filename ]; >+ } >+ } >+ return $fh; >+} >+ >+sub _dir { >+ my ( $self ) = @_; >+ my $dir = $self->{temporary}? $self->{tmpdir}: $self->{rootdir}; >+ $dir.= '/'. ( $self->{category}? $self->{category}: KOHA_UPLOAD ); >+ mkdir $dir if !-d $dir; >+ return $dir; >+} >+ >+sub _full_fname { >+ my ( $self, $rec ) = @_; >+ if( ref $rec ) { >+ return ( $rec->{category}? $self->{rootdir}: $self->{tmpdir} ). >+ '/'. $rec->{dir}. '/'. $rec->{hashvalue}. '_'. $rec->{filename}; >+ } >+} >+ >+sub _hook { >+ my ( $self, $filename, $buffer, $bytes_read, $data ) = @_; >+ $self->_compute( $filename, $buffer ); >+ my $fh = $self->_fh( $filename ) // $self->_create_file( $filename ); >+ print $fh $buffer if $fh; >+} >+ >+sub _done { >+ my ( $self ) = @_; >+ $self->{done} = 1; >+ foreach my $f ( keys $self->{files} ) { >+ my $fh = $self->_fh($f); >+ $self->_register( $f, $fh? tell( $fh ): undef ) >+ if !$self->{files}->{$f}->{errcode}; >+ $fh->close if $fh; >+ } >+} >+ >+sub _register { >+ my ( $self, $filename, $size ) = @_; >+ my $dbh= C4::Context->dbh; >+ my $sql= "INSERT INTO uploaded_files (hashvalue, filename, dir, filesize, >+ owner, categorycode) VALUES (?,?,?,?,?,?)"; >+ my @pars= ( $self->{files}->{$filename}->{hash}, >+ $filename, >+ $self->{temporary}? KOHA_UPLOAD: $self->{category}, >+ $size, >+ $self->{uid}, >+ $self->{category}, >+ ); >+ $dbh->do( $sql, undef, @pars ); >+ my $i = $dbh->last_insert_id(undef, undef, 'uploaded_files', undef); >+ $self->{files}->{$filename}->{id} = $i if $i; >+} >+ >+sub _lookup { >+ my ( $self, $params ) = @_; >+ my $dbh = C4::Context->dbh; >+ my $sql = 'SELECT id,hashvalue,filename,dir,categorycode '. >+ 'FROM uploaded_files '; >+ if( $params->{id} ) { >+ $sql.= "WHERE id=?"; >+ } else { >+ $sql.= "WHERE hashvalue=?"; >+ } >+ my $temp= $dbh->selectall_arrayref( $sql, { Slice => {} }, >+ ( $params->{id} // $params->{hashvalue} // 0 ) ); >+ return $temp; >+} >+ >+sub _compute { >+# Computes hash value when sub hook feeds the first block >+# For temporary files, the id is made unique with time >+ my ( $self, $name, $block ) = @_; >+ if( !$self->{files}->{$name}->{hash} ) { >+ my $str = $name. ( $self->{uid} // '0' ). >+ ( $self->{temporary}? Time::HiRes::time(): '' ). >+ ( $self->{category} // 'tmp' ). substr( $block, 0, BYTES_DIGEST ); >+ # since Digest cannot handle wide chars, we need to encode here >+ # there could be a wide char in the filename or the category >+ my $h = Digest::MD5::md5_hex( Encode::encode_utf8( $str ) ); >+ $self->{files}->{$name}->{hash} = $h; >+ } >+} >+ >+=head1 AUTHOR >+ >+ Marcel de Rooy, Rijksmuseum Amsterdam, The Netherlands >+ >+=cut >+ >+1; >diff --git a/koha-tmpl/intranet-tmpl/prog/en/js/file-upload.js b/koha-tmpl/intranet-tmpl/prog/en/js/file-upload.js >new file mode 100644 >index 0000000..789f11d >--- /dev/null >+++ b/koha-tmpl/intranet-tmpl/prog/en/js/file-upload.js >@@ -0,0 +1,32 @@ >+function AjaxUpload ( input, progressbar, 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'; >+ 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 ); >+ } >+ xhr.onerror = function (e) { >+ // Probably only fires for network failure >+ alert('An error occurred while uploading.'); >+ } >+ xhr.send( formData ); >+ return xhr; >+} >diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/offline_circ/process_koc.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/offline_circ/process_koc.tt >index 76c317a..2a44d82 100644 >--- a/koha-tmpl/intranet-tmpl/prog/en/modules/offline_circ/process_koc.tt >+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/offline_circ/process_koc.tt >@@ -1,22 +1,43 @@ > [% INCLUDE 'doc-head-open.inc' %] > <title>Koha › Circulation › Offline circulation file upload</title> > [% INCLUDE 'doc-head-close.inc' %] >-[% INCLUDE 'file-upload.inc' %] >+ > <script type="text/javascript" src="[% themelang %]/js/background-job-progressbar.js"></script> >+<script type="text/javascript" src="[% themelang %]/js/file-upload.js"></script> > <script type="text/javascript"> > //<![CDATA[ >+var xhr; > $(document).ready(function(){ > $("#enqueuefile").hide(); > $("#processfile").hide(); > }); > >-function CheckUpload(f){ >- if (f.fileToUpload.value == ""){ >- alert(_("Please choose a file to upload")); >+function StartUpload() { >+ if( $('#fileToUpload').prop('files').length == 0 ) return; >+ $('#fileuploadform input.submit').prop('disabled',true); >+ $("#fileuploadfailed").hide(); >+ $("#processfile").hide(); >+ $("#fileuploadstatus").show(); >+ $("form#processfile #uploadedfileid").val(''); >+ $("form#enqueuefile #uploadedfileid").val(''); >+ xhr= AjaxUpload( $('#fileToUpload'), $('#fileuploadprogress'), cbUpload ); >+} >+ >+function cbUpload( status, fileid ) { >+ if( status=='done' ) { >+ $("form#processfile #uploadedfileid").val( fileid ); >+ $("form#enqueuefile #uploadedfileid").val( fileid ); >+ $('#fileToUpload').prop('disabled',true); >+ $("#processfile").show(); >+ $("#enqueuefile").show(); > } else { >- return ajaxFileUpload() >+ $("#fileuploadstatus").hide(); >+ $("#fileuploadfailed").show(); >+ $("#fileuploadfailed").text( _("Upload status: ") + >+ ( status=='failed'? _("Failed"): >+ ( status=='denied'? _("Denied"): status )) >+ ); > } >- return false; > } > > function CheckForm(f) { >@@ -25,6 +46,8 @@ function CheckForm(f) { > } else { > $("#fileuploadstatus").hide(); > $("#fileuploadform").slideUp(); >+ $("#mainformsubmit").prop('disabled',true); >+ $("#queueformsubmit").prop('disabled',true); > return submitBackgroundJob(f); > } > return false; >@@ -70,10 +93,10 @@ function CheckForm(f) { > <fieldset class="brief"> > <ol><li><label for="fileToUpload">Choose .koc file: </label> > <input type="file" id="fileToUpload" size="50" name="fileToUpload" /></li></ol> >- <fieldset class="action"><input type="button" class="submit" value="Upload file" onclick="CheckUpload(this.form);" /></fieldset> >+ <fieldset class="action"><input type="button" class="submit" value="Upload file" onclick="StartUpload();return false;" /></fieldset> > </fieldset> > </form> >- <div id="fileuploadstatus" style="display:none">Upload progress: <div id="fileuploadprogress"></div> <span id="fileuploadpercent">0</span>%</div> >+ <div id="fileuploadstatus" style="display:none">Upload progress: <progress id="fileuploadprogress" min="0" max="100" value="0"></progress> <span class="fileuploadpercent">0</span>%</div> > <div id="fileuploadfailed" style="display:none"></div> > </div> > >diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/stage-marc-import.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/stage-marc-import.tt >index dea732a..fed3104 100644 >--- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/stage-marc-import.tt >+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/stage-marc-import.tt >@@ -1,14 +1,19 @@ > [% INCLUDE 'doc-head-open.inc' %] > <title>Koha › Tools › Stage MARC records for import</title> > [% INCLUDE 'doc-head-close.inc' %] >-[% INCLUDE 'file-upload.inc' %] >+ > <script type="text/javascript" src="[% themelang %]/js/background-job-progressbar.js"></script> >+<script type="text/javascript" src="[% themelang %]/js/file-upload.js"></script> >+ > <style type="text/css"> >- #uploadpanel,#fileuploadstatus,#fileuploadfailed,#jobpanel,#jobstatus,#jobfailed { display : none; } >+ #fileuploadstatus,#fileuploadfailed,#fileuploadcancel,#jobpanel,#jobstatus,#jobfailed { display : none; } > #fileuploadstatus,#jobstatus { margin:.4em; } >- #fileuploadprogress,#jobprogress{ width:150px;height:10px;border:1px solid #666;background:url('[% interface %]/[% theme %]/img/progress.png') -300px 0px no-repeat; }</style> >+ #fileuploadprogress,#jobprogress { 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 xhr; > $(document).ready(function(){ > $("#processfile").hide(); > $("#record_type").change(function() { >@@ -27,7 +32,43 @@ function CheckForm(f) { > } > return false; > } >- >+function StartUpload() { >+ if( $('#fileToUpload').prop('files').length == 0 ) return; >+ $('#fileuploadbutton').hide(); >+ $("#fileuploadfailed").hide(); >+ $("#processfile").hide(); >+ $("#fileuploadstatus").show(); >+ $("#uploadedfileid").val(''); >+ xhr= AjaxUpload( $('#fileToUpload'), $('#fileuploadprogress'), cbUpload ); >+ $("#fileuploadcancel").show(); >+} >+function CancelUpload() { >+ if( xhr ) xhr.abort(); >+ $("#fileuploadstatus").hide(); >+ $('#fileuploadbutton').show(); >+ $("#fileuploadcancel").hide(); >+ $("#fileuploadfailed").show(); >+ $("#fileuploadfailed").text( _("Upload status: Cancelled ") ); >+} >+function cbUpload( status, fileid ) { >+ if( status=='done' ) { >+ $("#uploadedfileid").val( fileid ); >+ $('#fileToUpload').prop('disabled',true); >+ $('#fileuploadbutton').prop('disabled',true); >+ $('#fileuploadbutton').show(); >+ $("#fileuploadcancel").hide(); >+ $("#processfile").show(); >+ } else { >+ $('#fileuploadbutton').show(); >+ $("#fileuploadcancel").hide(); >+ $("#fileuploadstatus").hide(); >+ $("#fileuploadfailed").show(); >+ $("#fileuploadfailed").text( _("Upload status: ") + >+ ( status=='failed'? _("Failed"): >+ ( status=='denied'? _("Denied"): status )) >+ ); >+ } >+} > //]]> > </script> > </head> >@@ -90,11 +131,20 @@ function CheckForm(f) { > <input type="file" id="fileToUpload" name="fileToUpload" /> > </div> </li> > </ol> >- <fieldset class="action"><button class="submit" onclick="return ajaxFileUpload();">Upload file</button></fieldset> >+ <fieldset class="action"> >+ <button id="fileuploadbutton" onclick="StartUpload(); return false;">Upload file</button> >+ <button id="fileuploadcancel" onclick="CancelUpload(); return false;">Cancel</button> >+ </fieldset> > </fieldset> > >- <div id="uploadpanel"><div id="fileuploadstatus">Upload progress: <div id="fileuploadprogress"></div> <span id="fileuploadpercent">0</span>%</div> >- <div id="fileuploadfailed"></div></div> >+ <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> > > <form method="post" id="processfile" action="[% SCRIPT_NAME %]" enctype="multipart/form-data"> >diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/upload-images.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/upload-images.tt >index 4565995..812df23 100644 >--- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/upload-images.tt >+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/upload-images.tt >@@ -1,14 +1,40 @@ > [% INCLUDE 'doc-head-open.inc' %] > <title>Koha › Tools › Upload images</title> > [% INCLUDE 'doc-head-close.inc' %] >-[% INCLUDE 'file-upload.inc' %] >+ > <script type="text/javascript" src="[% themelang %]/js/background-job-progressbar.js"></script> >+<script type="text/javascript" src="[% themelang %]/js/file-upload.js"></script> >+ > <style type="text/css"> >- #uploadpanel,#fileuploadstatus,#fileuploadfailed,#jobpanel,#jobstatus,#jobfailed { display : none; } >+ #fileuploadstatus,#fileuploadfailed,#jobpanel,#jobstatus,#jobfailed { display : none; } > #fileuploadstatus,#jobstatus { margin:.4em; } >- #fileuploadprogress,#jobprogress{ width:150px;height:10px;border:1px solid #666;background:url('[% interface %]/[% theme %]/img/progress.png') -300px 0px no-repeat; }</style> >+ #fileuploadprogress,#jobprogress { 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[ >+function StartUpload() { >+ if( $('#fileToUpload').prop('files').length == 0 ) return; >+ $('#uploadform button.submit').prop('disabled',true); >+ $("#fileuploadstatus").show(); >+ $("#uploadedfileid").val(''); >+ xhr= AjaxUpload( $('#fileToUpload'), $('#fileuploadprogress'), cbUpload ); >+} >+function cbUpload( status, fileid ) { >+ if( status=='done' ) { >+ $("#uploadedfileid").val( fileid ); >+ $('#fileToUpload').prop('disabled',true); >+ $("#processfile").show(); >+ } else { >+ $("#fileuploadstatus").hide(); >+ $("#fileuploadfailed").show(); >+ $("#fileuploadfailed").text( _("Upload status: ") + >+ ( status=='failed'? _("Failed"): >+ ( status=='denied'? _("Denied"): status )) >+ ); >+ $("#processfile").hide(); >+ } >+} > $(document).ready(function(){ > $("#processfile").hide(); > $("#zipfile").click(function(){ >@@ -19,7 +45,8 @@ $(document).ready(function(){ > }); > $("#uploadfile").validate({ > submitHandler: function(form) { >- ajaxFileUpload(); >+ StartUpload(); >+ return false; > } > }); > }); >@@ -76,8 +103,14 @@ $(document).ready(function(){ > <fieldset class="action"><button class="submit">Upload file</button></fieldset> > </fieldset> > >- <div id="uploadpanel"><div id="fileuploadstatus">Upload progress: <div id="fileuploadprogress"></div> <span id="fileuploadpercent">0</span>%</div> >- <div id="fileuploadfailed"></div></div> >+ <div id="uploadpanel"> >+ <div id="fileuploadstatus">Upload progress: >+ <progress min="0" max="100" value="0" id="fileuploadprogress"> >+ </progress> >+ <span class="fileuploadpercent">0</span>% >+ </div> >+ <div id="fileuploadfailed"></div> >+ </div> > </form> > > <form method="post" id="processfile" action="[% SCRIPT_NAME %]" enctype="multipart/form-data"> >diff --git a/offline_circ/enqueue_koc.pl b/offline_circ/enqueue_koc.pl >index 6798f3f..a81aecb 100755 >--- a/offline_circ/enqueue_koc.pl >+++ b/offline_circ/enqueue_koc.pl >@@ -32,7 +32,7 @@ use C4::Circulation; > use C4::Items; > use C4::Members; > use C4::Stats; >-use C4::UploadedFile; >+use Koha::Upload; > > use Date::Calc qw( Add_Delta_Days Date_to_Days ); > >@@ -60,8 +60,8 @@ my $sessionID = $cookies{'CGISESSID'}->value; > our $dbh = C4::Context->dbh(); > > if ($fileID) { >- my $uploaded_file = C4::UploadedFile->fetch($sessionID, $fileID); >- my $fh = $uploaded_file->fh(); >+ my $upload = Koha::Upload->new->get({ id => $fileID, filehandle => 1 }); >+ my $fh = $upload->{fh}; > my @input_lines = <$fh>; > > my $header_line = shift @input_lines; >diff --git a/offline_circ/process_koc.pl b/offline_circ/process_koc.pl >index d7e5606..21462ef 100755 >--- a/offline_circ/process_koc.pl >+++ b/offline_circ/process_koc.pl >@@ -32,7 +32,7 @@ use C4::Circulation; > use C4::Items; > use C4::Members; > use C4::Stats; >-use C4::UploadedFile; >+use Koha::Upload; > use C4::BackgroundJob; > > use Date::Calc qw( Add_Delta_Days Date_to_Days ); >@@ -69,11 +69,11 @@ if ($completedJobID) { > $template->param(transactions_loaded => 1); > $template->param(messages => $results->{results}); > } elsif ($fileID) { >- my $uploaded_file = C4::UploadedFile->fetch($sessionID, $fileID); >- my $fh = $uploaded_file->fh(); >+ my $upload = Koha::Upload->new->get({ id => $fileID, filehandle => 1 }); >+ my $fh = $upload->{fh}; >+ my $filename = $upload->{name}; > my @input_lines = <$fh>; > >- my $filename = $uploaded_file->name(); > my $job = undef; > > if ($runinbackground) { >diff --git a/tools/stage-marc-import.pl b/tools/stage-marc-import.pl >index c39b6ff..1a7b587 100755 >--- a/tools/stage-marc-import.pl >+++ b/tools/stage-marc-import.pl >@@ -39,7 +39,7 @@ use C4::Output; > use C4::Biblio; > use C4::ImportBatch; > use C4::Matcher; >-use C4::UploadedFile; >+use Koha::Upload; > use C4::BackgroundJob; > use C4::MarcModificationTemplates; > use Koha::Plugins; >@@ -84,8 +84,9 @@ if ($completedJobID) { > my $results = $job->results(); > $template->param(map { $_ => $results->{$_} } keys %{ $results }); > } elsif ($fileID) { >- my $uploaded_file = C4::UploadedFile->fetch($sessionID, $fileID); >- my $fh = $uploaded_file->fh(); >+ my $upload = Koha::Upload->new->get({ id => $fileID, filehandle => 1 }); >+ my $fh = $upload->{fh}; >+ my $filename = $upload->{name}; # filename only, no path > my $marcrecord=''; > $/ = "\035"; > while (<$fh>) { >@@ -93,8 +94,8 @@ if ($completedJobID) { > s/\s+$//; > $marcrecord.=$_; > } >+ $fh->close; > >- my $filename = $uploaded_file->name(); > my $job = undef; > my $dbh; > if ($runinbackground) { >diff --git a/tools/upload-cover-image.pl b/tools/upload-cover-image.pl >index a2576ac..79c37f2 100755 >--- a/tools/upload-cover-image.pl >+++ b/tools/upload-cover-image.pl >@@ -47,7 +47,7 @@ use C4::Context; > use C4::Auth; > use C4::Output; > use C4::Images; >-use C4::UploadedFile; >+use Koha::Upload; > use C4::Log; > > my $debug = 1; >@@ -83,9 +83,9 @@ $template->{VARS}->{'biblionumber'} = $biblionumber; > my $total = 0; > > if ($fileID) { >- my $uploaded_file = C4::UploadedFile->fetch( $sessionID, $fileID ); >+ my $upload = Koha::Upload->new->get({ id => $fileID, filehandle => 1 }); > if ( $filetype eq 'image' ) { >- my $fh = $uploaded_file->fh(); >+ my $fh = $upload->{fh}; > my $srcimage = GD::Image->new($fh); > if ( defined $srcimage ) { > my $dberror = PutImage( $biblionumber, $srcimage, $replace ); >@@ -102,7 +102,7 @@ if ($fileID) { > undef $srcimage; > } > else { >- my $filename = $uploaded_file->filename(); >+ my $filename = $upload->{path}; > my $dirname = File::Temp::tempdir( CLEANUP => 1 ); > unless ( system( "unzip", $filename, '-d', $dirname ) == 0 ) { > $error = 'UZIPFAIL'; >diff --git a/tools/upload-file-progress.pl b/tools/upload-file-progress.pl >index 91af827..6b52625 100755 >--- a/tools/upload-file-progress.pl >+++ b/tools/upload-file-progress.pl >@@ -36,7 +36,7 @@ my $flags_required = [ > {tools => 'upload_local_cover_images'}, > ]; > >-my %cookies = fetch CGI::Cookie; >+my %cookies = CGI::Cookie->fetch; > > my ($auth_status, $sessionID) = check_cookie_auth($cookies{'CGISESSID'}->value); > >@@ -47,8 +47,8 @@ foreach my $flag_required ( @{$flags_required} ) { > } > } > >+my $reply = CGI->new; > if ($auth_failure) { >- my $reply = CGI->new(""); > print $reply->header(-type => 'text/html'); > print '{"progress":"0"}'; > exit 0; >@@ -56,7 +56,6 @@ if ($auth_failure) { > > my $reported_progress = C4::UploadedFile->upload_progress($sessionID); > >-my $reply = CGI->new(""); > print $reply->header(-type => 'text/html'); > # response will be sent back as JSON > print '{"progress":"' . $reported_progress . '"}'; >diff --git a/tools/upload-file.pl b/tools/upload-file.pl >index 94a33a6..ec0313d 100755 >--- a/tools/upload-file.pl >+++ b/tools/upload-file.pl >@@ -17,18 +17,14 @@ > # You should have received a copy of the GNU General Public License > # along with Koha; if not, see <http://www.gnu.org/licenses>. > >-use strict; >-#use warnings; FIXME - Bug 2505 >+use Modern::Perl; >+use CGI::Cookie; > >-# standard or CPAN modules used >-use IO::File; > use CGI qw ( -utf8 ); >-use CGI::Session; >+#use CGI::Session; > use C4::Context; > use C4::Auth qw/check_cookie_auth haspermission/; >-use CGI::Cookie; # need to check cookies before >- # having CGI parse the POST request >-use C4::UploadedFile; >+use Koha::Upload; > > # upload-file.pl must authenticate the user > # before processing the POST request, >@@ -36,7 +32,7 @@ use C4::UploadedFile; > # not authorized. Consequently, unlike > # most of the other CGI scripts, upload-file.pl > # requires that the session cookie already >-# have been created. >+# has been created. > > my $flags_required = [ > {circulate => 'circulate_remaining_permissions'}, >@@ -44,10 +40,11 @@ my $flags_required = [ > {tools => 'upload_local_cover_images'} > ]; > >-my %cookies = fetch CGI::Cookie; >+my %cookies = CGI::Cookie->fetch; >+my $sid = $cookies{'CGISESSID'}->value; > > my $auth_failure = 1; >-my ( $auth_status, $sessionID ) = check_cookie_auth( $cookies{'CGISESSID'}->value ); >+my ( $auth_status, $sessionID ) = check_cookie_auth( $sid ); > foreach my $flag_required ( @{$flags_required} ) { > if ( my $flags = haspermission( C4::Context->config('user'), $flag_required ) ) { > $auth_failure = 0 if $auth_status eq 'ok'; >@@ -56,40 +53,22 @@ foreach my $flag_required ( @{$flags_required} ) { > > if ($auth_failure) { > $auth_status = 'denied' if $auth_status eq 'failed'; >- send_reply($auth_status, ""); >+ send_reply( $auth_status ); > exit 0; > } > >-our $uploaded_file = C4::UploadedFile->new($sessionID); >-unless (defined $uploaded_file) { >- # FIXME - failed to create file for some reason >- send_reply('failed', ''); >- exit 0; >+my $upload = Koha::Upload->new({ }); >+if( !$upload || ! $upload->cgi ) { >+ send_reply( 'failed' ); >+} else { >+ send_reply( 'done', $upload->result ); > } >-$uploaded_file->max_size($ENV{'CONTENT_LENGTH'}); # may not be the file size, exactly >- >-my $query; >-$query = new CGI \&upload_hook; >-$uploaded_file->done(); >-send_reply('done', $uploaded_file->id()); >- >-# 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 upload_hook { >- my ($file_name, $buffer, $bytes_read, $session) = @_; >- $uploaded_file->stash(\$buffer, $bytes_read); >- if ( ! $uploaded_file->name && $file_name ) { # save name on first chunk >- $uploaded_file->name($file_name); >- } >-} >- >-sub send_reply { >- my ($upload_status, $fileid) = @_; >- >+sub send_reply { # response will be sent back as JSON >+ my ( $upload_status, $data ) = @_; > my $reply = CGI->new(""); > print $reply->header(-type => 'text/html'); >- # response will be sent back as JSON >- print '{"status":"' . $upload_status . '","fileid":"' . $fileid . '"}'; >+ print '{"status":"' . $upload_status . >+ ( $data? '","fileid":"' . $data: '' ) . '"}'; > } >-- >1.7.10.4
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 14321
:
41498
|
41499
|
41500
|
41501
|
41555
|
41556
|
41557
|
41558
|
41559
|
41560
|
41561
|
41562
|
41665
|
41666
|
41667
|
41668
|
41669
|
41677
|
41873
|
41874
|
41875
|
41876
|
41877
|
41878
|
41879
|
41880
|
41881
|
42411
|
42412
|
42413
|
42414
|
42558
|
42559
|
42560
|
42561
|
42584
|
42587
|
42588
|
42589
|
42590
|
42591
|
42660
|
42661
|
42662
|
42663
|
42664
|
42665
|
42666
|
42707
|
42726
|
42727
|
42728
|
42729
|
42730
|
42731
|
42732
|
42889
|
42890