Bug 18736: Calculate tax depending on rounding
[koha.git] / C4 / HTML5Media.pm
index 1094aa9..c77e8a4 100644 (file)
@@ -22,8 +22,7 @@ use warnings;
 
 use C4::Context;
 use MARC::Field;
-use Koha::Upload;
-use WWW::YouTube::Download qw(playback_url);
+use Koha::UploadedFiles;
 
 =head1 HTML5Media
 
@@ -51,8 +50,8 @@ sub gethtml5media {
     my @HTML5MediaExtensions = split( /\|/, C4::Context->preference("HTML5MediaExtensions") );
     my $HTML5MediaYouTube    = C4::Context->preference("HTML5MediaYouTube");
     my $marcflavour          = C4::Context->preference("marcflavour");
-    my $isyoutube            = 0;
     foreach my $HTML5Media_field (@HTML5Media_fields) {
+    my $isyoutube            = 0;
         my %HTML5Media;
         # protocol
         if ( $HTML5Media_field->indicator(1) eq '1' ) {
@@ -99,16 +98,20 @@ sub gethtml5media {
         # src
         if ( $HTML5Media_field->subfield('u') ) {
             $HTML5Media{srcblock} = $HTML5Media_field->subfield('u');
-            if (grep /youtube/, $HTML5Media_field->subfield('u') ) { # TODO is there an official YT URL shortener? Can we use that too?
+            if (grep /youtu\.?be/, $HTML5Media_field->subfield('u') ) {
                 if ($HTML5MediaYouTube == 1) {
+                    require WWW::YouTube::Download;
+                    import  WWW::YouTube::Download qw(playback_url);
                     my $youtube           = WWW::YouTube::Download->new;
-                    $HTML5Media{srcblock} = $youtube->playback_url(
-                        $HTML5Media_field->subfield('u'), {
-                            'fmt' => '43' #webm is the only format compatible to all modern browsers. maybe check for available qualities
-                        }
-                    );
-                    # TODO handle error if format not availabe. Does that ever occur?
-                    $isyoutube = 1;
+                    eval {
+                        $HTML5Media{srcblock} = $youtube->playback_url(
+                            $HTML5Media_field->subfield('u'), {
+                                'fmt' => '43' #webm is the only format compatible to all modern browsers. maybe check for available qualities
+                            }
+                        );
+                    };
+                    if ($@) { warn $@; }
+                    else  { $isyoutube = 1;}
                 }
                else {
                    next; # do not embed youtube videos
@@ -131,10 +134,12 @@ sub gethtml5media {
         if ( $HTML5Media{srcblock} =~ /\Qopac-retrieve-file.pl\E/ ) {
             my ( undef, $id ) = split /id=/, $HTML5Media{srcblock};
             next if !$id;
-            my $public = ( ( caller )[1] =~ /opac/ ) ? { public => 1 }: {};
-            my $upl = Koha::Upload->new( $public )->get({ hashvalue => $id });
-            next if !$upl || $upl->{name} !~ /\./;
-            $HTML5Media{extension} = ( $upl->{name} =~ m/([^.]+)$/ )[0];
+            my %public = ( ( caller )[1] =~ /opac/ ) ? ( public => 1 ): ();
+            my $upload = Koha::UploadedFiles->search({
+                hashvalue => $id, %public,
+            })->next;
+            next if !$upload || $upload->filename !~ /\./;
+            $HTML5Media{extension} = ( $upload->filename =~ m/([^.]+)$/ )[0];
         }
         # check remote files
         else {