Blob Blame History Raw
From 96188d8067a365b2f4a4ffbdb327b2ec8214c19c Mon Sep 17 00:00:00 2001
From: Adam Williamson <awilliam@redhat.com>
Date: Fri, 1 Nov 2019 12:03:35 -0700
Subject: [PATCH] Fix URLs for needles in subdirectories (POO #58959)

As discussed in the POO, this was broken by PR #2410 commit
36aa974 - it assumes you can always find a needle simply by
sticking the needle filename on the end of a `needledir` call,
but you can't, needles are allowed to be in subdirectories of
needledir. This should hopefully fix that without breaking
the custom run case by using the *whole* of the JSON file path -
we just figure out the subdirectory component from it. This works
for me in the 'needle is in a subdirectory of the normal needle
dir' case, but I didn't test it in the custom run case.

Signed-off-by: Adam Williamson <awilliam@redhat.com>
---
 lib/OpenQA/WebAPI/Controller/File.pm | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/lib/OpenQA/WebAPI/Controller/File.pm b/lib/OpenQA/WebAPI/Controller/File.pm
index ad1b004f..ed73f9be 100644
--- a/lib/OpenQA/WebAPI/Controller/File.pm
+++ b/lib/OpenQA/WebAPI/Controller/File.pm
@@ -43,7 +43,25 @@ sub needle {
     }
 
     # locate the needle in the needle directory for the given distri and version
-    push(@{($self->{static} = Mojolicious::Static->new)->paths}, needledir($distri, $version));
+    my $needledir = needledir($distri, $version);
+    # we need to handle the needle being in a subdirectory - we cannot
+    # assume it is always just '$needledir/$name.$format'. figure out
+    # subdirectory elements from the JSON file path
+    my ($dummy1, $path, $dummy2) = fileparse($jsonfile);
+    # drop the trailing / from $path
+    $path = substr($path, 0, -1);
+    if (index($path, '/needles') != -1) {
+        # we got something like /var/lib/openqa/share/tests/distri/needles/(subdir)/needle.json
+        my @elems = split('/needles', $path, 2);
+        if (defined $elems[1]) {
+            $needledir .= $elems[1];
+        }
+    }
+    elsif ($path ne '.') {
+        # we got something like subdir/needle.json, $path will be "subdir"
+        $needledir .= "/$path";
+    }
+    push(@{($self->{static} = Mojolicious::Static->new)->paths}, $needledir);
 
     # name is an URL parameter and can't contain slashes, so it should be safe
     return $self->serve_static_($name . $format);
-- 
2.24.0.rc1