From e7f2470881af57201ca6405ab81cc6f04f2d666a Mon Sep 17 00:00:00 2001
From: Shi Yao Wang <shi-yao.wang@inlibro.com>
Date: Mon, 29 Jan 2024 11:42:17 -0500
Subject: [PATCH] Bug 35797: return the image directly instead of base64 in
 json

Test plan:
1- Apply the patch
2- Go to administration -> sys.pref. -> set patronimages to allow
3- Log in koha using a user with edit_borrowers privileges
4- Have a patron with a profile image (add an image if necessary by
searching for a patron and then clicking on the default profile image on
the top left)
5- Open a new browser tab, write
{intranet_url}/api/v1/patrons/{borrowernumber}/default_image
where {intranet_url} is the base intranet url of koha and {borrowernumber}
is the borrowernumber of a borrower that has an image
6- Notice the image displays in the browser
7- Do step 5 with a borrowernumber of a patron without an image and a
borrowernumber of a patron that doesn't exist (and other edge cases
where there should be an error message displayed such as "Access
forbidden", "Authentication required", etc.)
8- Notice an appropriate error message is displayed
---
 Koha/REST/V1/Patrons/Image.pm                | 10 +++-------
 api/v1/swagger/definitions/patron_image.yaml | 14 --------------
 api/v1/swagger/paths/patrons_image.yaml      |  6 ++----
 api/v1/swagger/swagger.yaml                  |  6 ++----
 4 files changed, 7 insertions(+), 29 deletions(-)
 delete mode 100644 api/v1/swagger/definitions/patron_image.yaml

diff --git a/Koha/REST/V1/Patrons/Image.pm b/Koha/REST/V1/Patrons/Image.pm
index d9b53b6ae2..3cba4612a9 100644
--- a/Koha/REST/V1/Patrons/Image.pm
+++ b/Koha/REST/V1/Patrons/Image.pm
@@ -40,7 +40,6 @@ Controller method that gets a patron's image
 =cut
 
 sub get {
-
     my $c = shift->openapi->valid_input or return;
 
     my $patron = Koha::Patrons->find( $c->param('patron_id') );
@@ -57,18 +56,15 @@ sub get {
 
         unless ($image) {
             return $c->render(
-                status => 404,
+                status  => 404,
                 openapi => { error => "Patron image not found." }
             );
         }
 
         return $c->render(
             status => 200,
-            openapi => {
-                patron_id => $image->borrowernumber,
-                mimetype => $image->mimetype,
-                imagefile => encode_base64($image->imagefile)
-            }
+            format => "png",              # currently all patron images are converted to png upon upload
+            data   => $image->imagefile
         );
     } catch {
         $c->unhandled_exception($_);
diff --git a/api/v1/swagger/definitions/patron_image.yaml b/api/v1/swagger/definitions/patron_image.yaml
deleted file mode 100644
index 34aef0361f..0000000000
--- a/api/v1/swagger/definitions/patron_image.yaml
+++ /dev/null
@@ -1,14 +0,0 @@
----
-type: object
-properties:
-  patron_id:
-    type: integer
-    description: Internal patron identifier
-  mimetype:
-    type: string
-    description: Patron image mimetype
-  imagefile:
-    type: string
-    description: Patron image raw data
-required:
-  - patron_id
diff --git a/api/v1/swagger/paths/patrons_image.yaml b/api/v1/swagger/paths/patrons_image.yaml
index e6c69f0c02..0aff520037 100644
--- a/api/v1/swagger/paths/patrons_image.yaml
+++ b/api/v1/swagger/paths/patrons_image.yaml
@@ -1,5 +1,5 @@
 ---
-"/patrons/{patron_id}/image":
+"/patrons/{patron_id}/default_image":
   get:
     x-mojo-to: Patrons::Image#get
     operationId: getPatronImage
@@ -9,12 +9,10 @@
     parameters:
       - $ref: "../swagger.yaml#/parameters/patron_id_pp"
     produces:
-      - application/json
+      - image/png
     responses:
       "200":
         description: Patron's image
-        schema:
-          $ref: "../swagger.yaml#/definitions/patron_image"
       "401":
         description: Authentication required
         schema:
diff --git a/api/v1/swagger/swagger.yaml b/api/v1/swagger/swagger.yaml
index 0f1eb959c6..20f00b24e7 100644
--- a/api/v1/swagger/swagger.yaml
+++ b/api/v1/swagger/swagger.yaml
@@ -130,8 +130,6 @@ definitions:
     $ref: ./definitions/patron_balance.yaml
   patron_extended_attribute:
     $ref: ./definitions/patron_extended_attribute.yaml
-  patron_image:
-    $ref: ./definitions/patron_image.yaml
   preservation_config:
     $ref: ./definitions/preservation_config.yaml
   preservation_train:
@@ -425,8 +423,8 @@ paths:
     $ref: "./paths/patrons_extended_attributes.yaml#/~1patrons~1{patron_id}~1extended_attributes~1{extended_attribute_id}"
   "/patrons/{patron_id}/holds":
     $ref: "./paths/patrons_holds.yaml#/~1patrons~1{patron_id}~1holds"
-  "/patrons/{patron_id}/image":
-    $ref: "./paths/patrons_image.yaml#/~1patrons~1{patron_id}~1image"
+  "/patrons/{patron_id}/default_image":
+    $ref: "./paths/patrons_image.yaml#/~1patrons~1{patron_id}~1default_image"
   "/patrons/{patron_id}/password":
     $ref: "./paths/patrons_password.yaml#/~1patrons~1{patron_id}~1password"
   "/patrons/{patron_id}/password/expiration_date":
-- 
2.34.1