@@ -10,11 +10,11 @@ package no.ndla.imageapi.controller
1010
1111import cats .implicits .catsSyntaxEitherId
1212import no .ndla .common .errors .{ValidationException , ValidationMessage }
13- import no .ndla .imageapi .model .domain .ImageStream
14- import no .ndla .imageapi .service .{ ImageConverter , ImageStorageService , PercentPoint , PixelPoint , ReadService }
13+ import no .ndla .imageapi .model .domain .{ ImageStream , ProcessableImageStream , UnprocessableImageStream }
14+ import no .ndla .imageapi .service .*
1515import no .ndla .network .clients .MyNDLAApiClient
16- import no .ndla .network .tapir .{AllErrors , ErrorHandling , DynamicHeaders , ErrorHelpers , TapirController }
1716import no .ndla .network .tapir .TapirUtil .errorOutputsFor
17+ import no .ndla .network .tapir .*
1818import sttp .tapir .*
1919import sttp .tapir .server .ServerEndpoint
2020
@@ -29,8 +29,8 @@ class RawController(using
2929 readService : ReadService ,
3030 myNDLAApiClient : MyNDLAApiClient ,
3131) extends TapirController {
32- import errorHelpers .*
3332 import errorHandling .*
33+ import errorHelpers .*
3434 override val serviceName : String = " raw"
3535 override val prefix : EndpointInput [Unit ] = " image-api" / serviceName
3636 override val enableSwagger : Boolean = true
@@ -39,7 +39,7 @@ class RawController(using
3939
4040 private def toImageResponse (image : ImageStream ): Either [AllErrors , (DynamicHeaders , InputStream )] = {
4141 val headers = DynamicHeaders .fromValue(" Content-Type" , image.contentType)
42- Right (headers -> image.stream )
42+ Right (headers -> image.toStream )
4343 }
4444
4545 def getImageFile : ServerEndpoint [Any , Eff ] = endpoint
@@ -69,9 +69,7 @@ class RawController(using
6969 .out(inputStreamBody)
7070 .serverLogicPure { case (imageId, imageParams) =>
7171 readService.getImageFileName(imageId, imageParams.language) match {
72- case Success (Some (fileName)) =>
73- val x = getRawImage(fileName, imageParams)
74- x match {
72+ case Success (Some (fileName)) => getRawImage(fileName, imageParams) match {
7573 case Failure (ex) => returnLeftError(ex)
7674 case Success (img) => toImageResponse(img)
7775 }
@@ -88,18 +86,19 @@ class RawController(using
8886 resize
8987 }
9088 }
91- val nonResizableMimeTypes = List (" image/gif" , " image/svg" , " image/svg+xml" )
9289 imageStorage.get(imageName) match {
93- case Success (img) if nonResizableMimeTypes.contains(img.contentType.toLowerCase ) => Success (img)
94- case Success (img) => crop(img, imageParams)
90+ case Success (img : UnprocessableImageStream ) => Success (img)
91+ case Success (img : ProcessableImageStream ) => crop(img, imageParams)
9592 .flatMap(stream => dynamicCropOrResize(stream, imageParams))
9693 .recoverWith {
9794 case ex : ValidationException => Failure (ex)
9895 case ex =>
9996 logger.error(s " Could not crop or resize image ' $imageName', got exception: ' ${ex.getMessage}' " , ex)
10097 Success (img)
10198 }
102- case Failure (e) => Failure (e)
99+ case Failure (ex) =>
100+ logger.error(s " Failed to get image ' $imageName' from S3 " , ex)
101+ Failure (ex)
103102 }
104103 }
105104
@@ -113,7 +112,7 @@ class RawController(using
113112 }
114113 }
115114
116- private def crop (image : ImageStream , imageParams : ImageParams ): Try [ImageStream ] = {
115+ private def crop (image : ProcessableImageStream , imageParams : ImageParams ): Try [ProcessableImageStream ] = {
117116 val unit = imageParams.cropUnit.getOrElse(" percent" )
118117 unit match {
119118 case " percent" =>
@@ -145,7 +144,7 @@ class RawController(using
145144 .isDefined || imageParams.ratio.isDefined)
146145 }
147146
148- private def dynamicCrop (image : ImageStream , imageParams : ImageParams ): Try [ImageStream ] = {
147+ private def dynamicCrop (image : ProcessableImageStream , imageParams : ImageParams ): Try [ProcessableImageStream ] = {
149148
150149 (imageParams.focalX, imageParams.focalY, imageParams.width, imageParams.height) match {
151150 case (Some (fx), Some (fy), w, h) =>
@@ -154,7 +153,7 @@ class RawController(using
154153 }
155154 }
156155
157- private def resize (image : ImageStream , imageParams : ImageParams ): Try [ImageStream ] = {
156+ private def resize (image : ProcessableImageStream , imageParams : ImageParams ): Try [ProcessableImageStream ] = {
158157 (imageParams.width, imageParams.height) match {
159158 case (Some (width), Some (height)) => imageConverter.resize(image, width.toInt, height.toInt)
160159 case (Some (width), _) => imageConverter.resizeWidth(image, width.toInt)
0 commit comments