Bug 17501: Move getCategories and httpheaders from Upload.pm
[koha.git] / tools / upload.pl
index 5d9c8e7..76f2cb6 100755 (executable)
@@ -23,7 +23,8 @@ use JSON;
 
 use C4::Auth;
 use C4::Output;
-use Koha::Upload;
+use Koha::UploadedFile;
+use Koha::UploadedFiles;
 
 my $input  = CGI::->new;
 my $op     = $input->param('op') // 'new';
@@ -38,61 +39,79 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
         query           => $input,
         type            => "intranet",
         authnotrequired => 0,
-        flagsrequired   => { editcatalogue => '*' },
+        flagsrequired   => { tools => 'upload_general_files' },
     }
 );
 
 $template->param(
-    plugin => $plugin,
-    index  => $index,
+    index      => $index,
+    owner      => $loggedinuser,
+    plugin     => $plugin,
 );
 
-my $upar = $plugin ? { public => 1 } : {};
 if ( $op eq 'new' ) {
     $template->param(
         mode             => 'new',
-        uploadcategories => Koha::Upload->getCategories,
+        uploadcategories => Koha::UploadedFile->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);
+    my $uploads;
+    if( $id ) {
+        my $rec = Koha::UploadedFiles->search({
+            id => $id,
+            $plugin? ( public => 1 ) : (),
+        })->next;
+        push @$uploads, $rec->unblessed if $rec;
+    } else {
+        $uploads = Koha::UploadedFiles->search_term({
+            term => $term,
+            $plugin? (): ( include_private => 1 ),
+        })->unblessed;
+    }
+
     $template->param(
         mode    => 'report',
         msg     => $msg,
-        uploads => \@uploads,
+        uploads => $uploads,
     );
     output_html_with_http_headers $input, $cookie, $template->output;
-} elsif ( $op eq 'delete' ) {
 
+} 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;
+    my $upload = $plugin?
+         Koha::UploadedFiles->search({ public => 1, id => $id })->next:
+         Koha::UploadedFiles->find($id);
+    my $fn = $upload? $upload->delete: undef;
+    #TODO Improve error handling
+    my $msg = $fn?
+        JSON::to_json({ $fn => 6 }):
+        JSON::to_json({
+            $upload? $upload->filename: ( $id? "id $id": '[No id]' ), 7,
+        });
     $template->param(
         mode             => 'deleted',
         msg              => $msg,
-        uploadcategories => $upl->getCategories,
+        uploadcategories => Koha::UploadedFile->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};
+    my $rec = Koha::UploadedFiles->search({
+        id => $id,
+        $plugin? ( public => 1 ) : (),
+    })->next;
+    my $fh  = $rec? $rec->file_handle:  undef;
     if ( !$rec || !$fh ) {
         $template->param(
             mode             => 'new',
             msg              => JSON::to_json( { $id => 5 } ),
-            uploadcategories => $upl->getCategories,
+            uploadcategories => Koha::UploadedFile->getCategories,
         );
         output_html_with_http_headers $input, $cookie, $template->output;
     } else {
-        my @hdr = $upl->httpheaders( $rec->{name} );
-        print Encode::encode_utf8( $input->header(@hdr) );
+        print Encode::encode_utf8( $input->header( $rec->httpheaders ) );
         while (<$fh>) {
             print $_;
         }