@@ -, +, @@ ajaxfileupload, ajaxFileUpload UploadedFile: Only a reference to DBIx file is found ajaxfileupload: Only release notes --- C4/UploadedFile.pm | 309 -------------------- C4/UploadedFiles.pm | 323 --------------------- .../lib/jquery/plugins/ajaxfileupload.js | 200 ------------- .../intranet-tmpl/prog/en/includes/file-upload.inc | 70 ----- .../en/modules/cataloguing/value_builder/upload.tt | 103 ------- .../value_builder/upload_delete_file.tt | 63 ---- .../prog/en/modules/test/progressbar.tt | 43 --- t/db_dependent/UploadedFile.t | 16 - t/db_dependent/UploadedFiles.t | 61 ---- test/progressbar.pl | 55 ---- test/progressbarsubmit.pl | 104 ------- tools/upload-file-progress.pl | 62 ---- 12 files changed, 1409 deletions(-) delete mode 100644 C4/UploadedFile.pm delete mode 100644 C4/UploadedFiles.pm delete mode 100644 koha-tmpl/intranet-tmpl/lib/jquery/plugins/ajaxfileupload.js delete mode 100644 koha-tmpl/intranet-tmpl/prog/en/includes/file-upload.inc delete mode 100644 koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/value_builder/upload.tt delete mode 100644 koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/value_builder/upload_delete_file.tt delete mode 100644 koha-tmpl/intranet-tmpl/prog/en/modules/test/progressbar.tt delete mode 100755 t/db_dependent/UploadedFile.t delete mode 100644 t/db_dependent/UploadedFiles.t delete mode 100755 test/progressbar.pl delete mode 100755 test/progressbarsubmit.pl delete mode 100755 tools/upload-file-progress.pl --- a/C4/UploadedFile.pm +++ a/C4/UploadedFile.pm @@ -1,309 +0,0 @@ -package C4::UploadedFile; - -# Copyright (C) 2007 LibLime -# Galen Charlton -# -# 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, see . - -use strict; -#use warnings; FIXME - Bug 2505 -use C4::Context; -use C4::Auth qw/get_session/; -use IO::File; - -use vars qw($VERSION); - -BEGIN { - # set the version for version checking - $VERSION = 3.07.00.049; -} - -=head1 NAME - -C4::UploadedFile - manage files uploaded by the user -for later processing. - -=head1 SYNOPSIS - - # create and store data - my $uploaded_file = C4::UploadedFile->new($sessionID); - my $fileID = $uploaded_file->id(); - $uploaded_file->name('c:\temp\file.mrc'); - $uploaded_file->max_size(1024); - while ($have_more_data) { - $uploaded_file->stash($data, $bytes_read); - } - $uploaded_file->done(); - - # check status of current file upload - my $progress = C4::UploadedFile->upload_progress($sessionID); - - # get file handle for reading uploaded file - my $uploaded_file = C4::UploadedFile->fetch($fileID); - my $fh = $uploaded_file->fh(); - - -Stores files uploaded by the user from their web browser. The -uploaded files are temporary and at present are not guaranteed -to survive beyond the life of the user's session. - -This module allows for tracking the progress of the file -currently being uploaded. - -TODO: implement secure persistent storage of uploaded files. - -=cut - -=head1 METHODS - -=cut - -=head2 new - - my $uploaded_file = C4::UploadedFile->new($sessionID); - -Creates a new object to represent the uploaded file. Requires -the current session ID. - -=cut - -sub new { - my $class = shift; - my $sessionID = shift; - - my $self = {}; - - $self->{'sessionID'} = $sessionID; - $self->{'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/$self->{'fileID'}"; - my $fh = new IO::File $tmp_file_name, "w"; - unless (defined $fh) { - return undef; - } - $fh->binmode(); # Windows compatibility - $self->{'fh'} = $fh; - $self->{'tmp_file_name'} = $tmp_file_name; - $self->{'max_size'} = 0; - $self->{'progress'} = 0; - $self->{'name'} = ''; - - bless $self, $class; - $self->_serialize(); - - my $session = get_session($sessionID); - $session->param('current_upload', $self->{'fileID'}); - $session->flush(); - - return $self; - -} - -sub _serialize { - my $self = shift; - - my $prefix = "upload_" . $self->{'fileID'}; - my $session = get_session($self->{'sessionID'}); - - # temporarily take file handle out of structure - my $fh = $self->{'fh'}; - delete $self->{'fh'}; - $session->param($prefix, $self); - $session->flush(); - $self->{'fh'} =$fh; -} - -=head2 id - - my $fileID = $uploaded_file->id(); - -=cut - -sub id { - my $self = shift; - return $self->{'fileID'}; -} - -=head2 name - - my $name = $uploaded_file->name(); - $uploaded_file->name($name); - -Accessor method for the name by which the file is to be known. - -=cut - -sub name { - my $self = shift; - if (@_) { - $self->{'name'} = shift; - $self->_serialize(); - } else { - return $self->{'name'}; - } -} - -=head2 filename - - my $filename = $uploaded_file->filename(); - -Accessor method for the name by which the file is to be known. - -=cut - -sub filename { - my $self = shift; - if (@_) { - $self->{'tmp_file_name'} = shift; - $self->_serialize(); - } else { - return $self->{'tmp_file_name'}; - } -} - -=head2 max_size - - my $max_size = $uploaded_file->max_size(); - $uploaded_file->max_size($max_size); - -Accessor method for the maximum size of the uploaded file. - -=cut - -sub max_size { - my $self = shift; - @_ ? $self->{'max_size'} = shift : $self->{'max_size'}; -} - -=head2 stash - - $uploaded_file->stash($dataref, $bytes_read); - -Write C<$dataref> to the temporary file. C<$bytes_read> represents -the number of bytes (out of C<$max_size>) transmitted so far. - -=cut - -sub stash { - my $self = shift; - my $dataref = shift; - my $bytes_read = shift; - - my $fh = $self->{'fh'}; - print $fh $$dataref; - - my $percentage = int(($bytes_read / $self->{'max_size'}) * 100); - if ($percentage > $self->{'progress'}) { - $self->{'progress'} = $percentage; - $self->_serialize(); - } -} - -=head2 done - - $uploaded_file->done(); - -Indicates that all of the bytes have been uploaded. - -=cut - -sub done { - my $self = shift; - $self->{'progress'} = 'done'; - $self->{'fh'}->close(); - $self->_serialize(); -} - -=head2 upload_progress - - my $upload_progress = C4::UploadFile->upload_progress($sessionID); - -Returns (as an integer from 0 to 100) the percentage -progress of the current file upload. - -=cut - -sub upload_progress { - my ($class, $sessionID) = shift; - - my $session = get_session($sessionID); - - my $fileID = $session->param('current_upload'); - - my $reported_progress = 0; - if (defined $fileID and $fileID ne "") { - my $file = C4::UploadedFile->fetch($sessionID, $fileID); - my $progress = $file->{'progress'}; - if (defined $progress) { - if ($progress eq "done") { - $reported_progress = 100; - } else { - $reported_progress = $progress; - } - } - } - return $reported_progress; -} - -=head2 fetch - - my $uploaded_file = C4::UploadedFile->fetch($sessionID, $fileID); - -Retrieves an uploaded file object from the current session. - -=cut - -sub fetch { - my $class = shift; - my $sessionID = shift; - my $fileID = shift; - - my $session = get_session($sessionID); - my $prefix = "upload_$fileID"; - my $self = $session->param($prefix); - my $fh = new IO::File $self->{'tmp_file_name'}, "r"; - $self->{'fh'} = $fh; - - bless $self, $class; - return $self; -} - -=head2 fh - - my $fh = $uploaded_file->fh(); - -Returns an IO::File handle to read the uploaded file. - -=cut - -sub fh { - my $self = shift; - return $self->{'fh'}; -} - -1; -__END__ - -=head1 AUTHOR - -Koha Development Team - -Galen Charlton - -=cut --- a/C4/UploadedFiles.pm +++ a/C4/UploadedFiles.pm @@ -1,323 +0,0 @@ -package C4::UploadedFiles; - -# This file is part of Koha. -# -# Copyright (C) 2011-2012 BibLibre -# -# 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 . - -=head1 NAME - -C4::UploadedFiles - Functions to deal with files uploaded with cataloging plugin upload.pl - -=head1 SYNOPSIS - - use C4::UploadedFiles; - - my $filename = $cgi->param('uploaded_file'); - my $file = $cgi->upload('uploaded_file'); - my $dir = $input->param('dir'); - - # upload file - my $id = C4::UploadedFiles::UploadFile($filename, $dir, $file->handle); - - # retrieve file infos - my $uploaded_file = C4::UploadedFiles::GetUploadedFile($id); - - # delete file - C4::UploadedFiles::DelUploadedFile($id); - -=head1 DESCRIPTION - -This module provides basic functions for adding, retrieving and deleting files related to -cataloging plugin upload.pl. - -It uses uploaded_files table. - -It is not related to C4::UploadedFile - -=head1 FUNCTIONS - -=cut - -use Modern::Perl; -use Digest::SHA; -use Fcntl; -use Encode; - -use C4::Context; -use C4::Koha; - -sub _get_file_path { - my ($hash, $dirname, $filename) = @_; - - my $upload_path = C4::Context->config('upload_path'); - if( !-d "$upload_path/$dirname" ) { - mkdir "$upload_path/$dirname"; - } - my $filepath = "$upload_path/$dirname/${hash}_$filename"; - $filepath =~ s|/+|/|g; - - return $filepath; -} - -=head2 GetUploadedFile - - my $file = C4::UploadedFiles::GetUploadedFile($id); - -Returns a hashref containing infos on uploaded files. -Hash keys are: - -=over 2 - -=item * id: id of the file (same as given in argument) - -=item * filename: name of the file - -=item * dir: directory where file is stored (relative to config variable 'upload_path') - -=back - -It returns undef if file is not found - -=cut - -sub GetUploadedFile { - my ( $hashvalue ) = @_; - - return unless $hashvalue; - - my $dbh = C4::Context->dbh; - my $query = qq{ - SELECT hashvalue, filename, dir - FROM uploaded_files - WHERE hashvalue = ? - }; - my $sth = $dbh->prepare($query); - $sth->execute( $hashvalue ); - my $file = $sth->fetchrow_hashref; - if ($file) { - $file->{filepath} = _get_file_path($file->{hashvalue}, $file->{dir}, - $file->{filename}); - } - - return $file; -} - -=head2 UploadFile - - my $id = C4::UploadedFiles::UploadFile($filename, $dir, $io_handle); - -Upload a new file and returns its id (its SHA-1 sum, actually). - -Parameters: - -=over 2 - -=item * $filename: name of the file - -=item * $dir: directory where to store the file (path relative to config variable 'upload_path' - -=item * $io_handle: valid IO::Handle object, can be retrieved with -$cgi->upload('uploaded_file')->handle; - -=back - -=cut - -sub UploadFile { - my ($filename, $dir, $handle) = @_; - $filename = decode_utf8($filename); - if($filename =~ m#(^|/)\.\.(/|$)# or $dir =~ m#(^|/)\.\.(/|$)#) { - warn "Filename or dirname contains '..'. Aborting upload"; - return; - } - - my $buffer; - my $data = ''; - while($handle->read($buffer, 1024)) { - $data .= $buffer; - } - $handle->close; - - my $sha = new Digest::SHA; - $sha->add($data); - $sha->add($filename); - $sha->add($dir); - my $hash = $sha->hexdigest; - - # Test if this id already exist - my $file = GetUploadedFile($hash); - if ($file) { - return $file->{hashvalue}; - } - - my $file_path = _get_file_path($hash, $dir, $filename); - - my $out_fh; - # Create the file only if it doesn't exist - unless( sysopen($out_fh, $file_path, O_WRONLY|O_CREAT|O_EXCL) ) { - warn "Failed to open file '$file_path': $!"; - return; - } - - print $out_fh $data; - my $size= tell($out_fh); - close $out_fh; - - my $dbh = C4::Context->dbh; - my $query = qq{ - INSERT INTO uploaded_files (hashvalue, filename, filesize, dir, categorycode, owner) VALUES (?,?,?,?,?,?); - }; - my $sth = $dbh->prepare($query); - my $uid= C4::Context->userenv? C4::Context->userenv->{number}: undef; - # uid is null in unit test - if($sth->execute($hash, $filename, $size, $dir, $dir, $uid)) { - return $hash; - } - - return; -} - -=head2 DanglingEntry - - C4::UploadedFiles::DanglingEntry($id,$isfileuploadurl); - -Determine if a entry is dangling. - -Returns: 2 == no db entry - 1 == no plain file - 0 == both a file and db entry. - -1 == N/A (undef id / non-file-upload URL) - -=cut - -sub DanglingEntry { - my ($id,$isfileuploadurl) = @_; - my $retval; - - if (defined($id)) { - my $file = GetUploadedFile($id); - if($file) { - my $file_path = $file->{filepath}; - my $file_deleted = 0; - unless( -f $file_path ) { - $retval = 1; - } else { - $retval = 0; - } - } - else { - if ( $isfileuploadurl ) { - $retval = 2; - } - else { - $retval = -1; - } - } - } - else { - $retval = -1; - } - return $retval; -} - -=head2 DelUploadedFile - - C4::UploadedFiles::DelUploadedFile( $hash ); - -Remove a previously uploaded file, given its hash value. - -Returns: 1 == file deleted - 0 == file not deleted - -1== no file to delete / no meaninful id passed - -=cut - -sub DelUploadedFile { - my ( $hashval ) = @_; - my $retval; - - if ( $hashval ) { - my $file = GetUploadedFile( $hashval ); - if($file) { - my $file_path = $file->{filepath}; - my $file_deleted = 0; - unless( -f $file_path ) { - warn "Id $file->{hashvalue} is in database but no plain file found, removing id from database"; - $file_deleted = 1; - } else { - if(unlink $file_path) { - $file_deleted = 1; - } - } - - unless($file_deleted) { - warn "File $file_path cannot be deleted: $!"; - } - - my $dbh = C4::Context->dbh; - my $query = qq{ - DELETE FROM uploaded_files - WHERE hashvalue = ? - }; - my $sth = $dbh->prepare($query); - my $numrows = $sth->execute( $hashval ); - # if either a DB entry or file was deleted, - # then clearly we have a deletion. - if ($numrows>0 || $file_deleted==1) { - $retval = 1; - } - else { - $retval = 0; - } - } - else { - warn "There was no file for hash $hashval."; - $retval = -1; - } - } - else { - warn "DelUploadFile called without hash value."; - $retval = -1; - } - return $retval; -} - -=head2 getCategories - - getCategories returns a list of upload category codes and names - -=cut - -sub getCategories { - my $cats = C4::Koha::GetAuthorisedValues('UPLOAD'); - [ map {{ code => $_->{authorised_value}, name => $_->{lib} }} @$cats ]; -} - -=head2 httpheaders - - httpheaders returns http headers for a retrievable upload - Will be extended by report 14282 - -=cut - -sub httpheaders { - my $file= shift; - return - ( '-type' => 'application/octet-stream', - '-attachment' => $file, ); -} - -1; --- a/koha-tmpl/intranet-tmpl/lib/jquery/plugins/ajaxfileupload.js +++ a/koha-tmpl/intranet-tmpl/lib/jquery/plugins/ajaxfileupload.js @@ -1,200 +0,0 @@ - -jQuery.extend({ - - createUploadIframe: function(id, uri) - { - //create frame - var frameId = 'jUploadFrame' + id; - - try { - var io = document.createElement('