module Servant.API.ImageJpeg where

import Codec.Picture.Jpg
import Control.Lens
import Data.ByteString.Lazy qualified as BL
import Data.Kind
import Data.OpenApi
import Data.Proxy
import GHC.Generics
import Network.HTTP.Media qualified
import Servant.API

type ImageJpegBytes :: Type
newtype ImageJpegBytes = MkImageJpegBytes {ImageJpegBytes -> ByteString
unImageJpegBytes :: BL.ByteString}
  deriving stock (ImageJpegBytes -> ImageJpegBytes -> Bool
(ImageJpegBytes -> ImageJpegBytes -> Bool)
-> (ImageJpegBytes -> ImageJpegBytes -> Bool) -> Eq ImageJpegBytes
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ImageJpegBytes -> ImageJpegBytes -> Bool
== :: ImageJpegBytes -> ImageJpegBytes -> Bool
$c/= :: ImageJpegBytes -> ImageJpegBytes -> Bool
/= :: ImageJpegBytes -> ImageJpegBytes -> Bool
Eq, (forall x. ImageJpegBytes -> Rep ImageJpegBytes x)
-> (forall x. Rep ImageJpegBytes x -> ImageJpegBytes)
-> Generic ImageJpegBytes
forall x. Rep ImageJpegBytes x -> ImageJpegBytes
forall x. ImageJpegBytes -> Rep ImageJpegBytes x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. ImageJpegBytes -> Rep ImageJpegBytes x
from :: forall x. ImageJpegBytes -> Rep ImageJpegBytes x
$cto :: forall x. Rep ImageJpegBytes x -> ImageJpegBytes
to :: forall x. Rep ImageJpegBytes x -> ImageJpegBytes
Generic, Eq ImageJpegBytes
Eq ImageJpegBytes =>
(ImageJpegBytes -> ImageJpegBytes -> Ordering)
-> (ImageJpegBytes -> ImageJpegBytes -> Bool)
-> (ImageJpegBytes -> ImageJpegBytes -> Bool)
-> (ImageJpegBytes -> ImageJpegBytes -> Bool)
-> (ImageJpegBytes -> ImageJpegBytes -> Bool)
-> (ImageJpegBytes -> ImageJpegBytes -> ImageJpegBytes)
-> (ImageJpegBytes -> ImageJpegBytes -> ImageJpegBytes)
-> Ord ImageJpegBytes
ImageJpegBytes -> ImageJpegBytes -> Bool
ImageJpegBytes -> ImageJpegBytes -> Ordering
ImageJpegBytes -> ImageJpegBytes -> ImageJpegBytes
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: ImageJpegBytes -> ImageJpegBytes -> Ordering
compare :: ImageJpegBytes -> ImageJpegBytes -> Ordering
$c< :: ImageJpegBytes -> ImageJpegBytes -> Bool
< :: ImageJpegBytes -> ImageJpegBytes -> Bool
$c<= :: ImageJpegBytes -> ImageJpegBytes -> Bool
<= :: ImageJpegBytes -> ImageJpegBytes -> Bool
$c> :: ImageJpegBytes -> ImageJpegBytes -> Bool
> :: ImageJpegBytes -> ImageJpegBytes -> Bool
$c>= :: ImageJpegBytes -> ImageJpegBytes -> Bool
>= :: ImageJpegBytes -> ImageJpegBytes -> Bool
$cmax :: ImageJpegBytes -> ImageJpegBytes -> ImageJpegBytes
max :: ImageJpegBytes -> ImageJpegBytes -> ImageJpegBytes
$cmin :: ImageJpegBytes -> ImageJpegBytes -> ImageJpegBytes
min :: ImageJpegBytes -> ImageJpegBytes -> ImageJpegBytes
Ord, ReadPrec [ImageJpegBytes]
ReadPrec ImageJpegBytes
Int -> ReadS ImageJpegBytes
ReadS [ImageJpegBytes]
(Int -> ReadS ImageJpegBytes)
-> ReadS [ImageJpegBytes]
-> ReadPrec ImageJpegBytes
-> ReadPrec [ImageJpegBytes]
-> Read ImageJpegBytes
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS ImageJpegBytes
readsPrec :: Int -> ReadS ImageJpegBytes
$creadList :: ReadS [ImageJpegBytes]
readList :: ReadS [ImageJpegBytes]
$creadPrec :: ReadPrec ImageJpegBytes
readPrec :: ReadPrec ImageJpegBytes
$creadListPrec :: ReadPrec [ImageJpegBytes]
readListPrec :: ReadPrec [ImageJpegBytes]
Read, Int -> ImageJpegBytes -> ShowS
[ImageJpegBytes] -> ShowS
ImageJpegBytes -> String
(Int -> ImageJpegBytes -> ShowS)
-> (ImageJpegBytes -> String)
-> ([ImageJpegBytes] -> ShowS)
-> Show ImageJpegBytes
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ImageJpegBytes -> ShowS
showsPrec :: Int -> ImageJpegBytes -> ShowS
$cshow :: ImageJpegBytes -> String
show :: ImageJpegBytes -> String
$cshowList :: [ImageJpegBytes] -> ShowS
showList :: [ImageJpegBytes] -> ShowS
Show)

instance ToParamSchema ImageJpegBytes where
  toParamSchema :: Proxy ImageJpegBytes -> Schema
toParamSchema Proxy ImageJpegBytes
Proxy =
    Schema
forall a. Monoid a => a
mempty
      Schema -> (Schema -> Schema) -> Schema
forall a b. a -> (a -> b) -> b
& (Maybe OpenApiType -> Identity (Maybe OpenApiType))
-> Schema -> Identity Schema
forall s a. HasType s a => Lens' s a
Lens' Schema (Maybe OpenApiType)
type_ ((Maybe OpenApiType -> Identity (Maybe OpenApiType))
 -> Schema -> Identity Schema)
-> OpenApiType -> Schema -> Schema
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ OpenApiType
OpenApiString
      Schema -> (Schema -> Schema) -> Schema
forall a b. a -> (a -> b) -> b
& (Maybe Format -> Identity (Maybe Format))
-> Schema -> Identity Schema
forall s a. HasFormat s a => Lens' s a
Lens' Schema (Maybe Format)
format ((Maybe Format -> Identity (Maybe Format))
 -> Schema -> Identity Schema)
-> Format -> Schema -> Schema
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Format
"binary"
instance ToSchema ImageJpegBytes where
  declareNamedSchema :: Proxy ImageJpegBytes -> Declare (Definitions Schema) NamedSchema
declareNamedSchema = NamedSchema -> Declare (Definitions Schema) NamedSchema
forall a. a -> DeclareT (Definitions Schema) Identity a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NamedSchema -> Declare (Definitions Schema) NamedSchema)
-> (Proxy ImageJpegBytes -> NamedSchema)
-> Proxy ImageJpegBytes
-> Declare (Definitions Schema) NamedSchema
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe Format -> Schema -> NamedSchema
NamedSchema (Format -> Maybe Format
forall a. a -> Maybe a
Just Format
"ImageJpegBytes") (Schema -> NamedSchema)
-> (Proxy ImageJpegBytes -> Schema)
-> Proxy ImageJpegBytes
-> NamedSchema
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Proxy ImageJpegBytes -> Schema
forall a. ToParamSchema a => Proxy a -> Schema
paramSchemaToSchema

type ImageJpeg :: Type
data ImageJpeg

instance Accept ImageJpeg where
  contentType :: Proxy ImageJpeg -> MediaType
contentType Proxy ImageJpeg
Proxy = ByteString
"image" ByteString -> ByteString -> MediaType
Network.HTTP.Media.// ByteString
"jpeg"

instance MimeRender ImageJpeg ImageJpegBytes where
  mimeRender :: Proxy ImageJpeg -> ImageJpegBytes -> ByteString
mimeRender Proxy ImageJpeg
Proxy = ImageJpegBytes -> ByteString
unImageJpegBytes

instance MimeUnrender ImageJpeg ImageJpegBytes where
  mimeUnrender :: Proxy ImageJpeg -> ByteString -> Either String ImageJpegBytes
mimeUnrender Proxy ImageJpeg
Proxy ByteString
bytes =
    case ByteString -> Either String DynamicImage
decodeJpeg (ByteString -> Either String DynamicImage)
-> ByteString -> Either String DynamicImage
forall a b. (a -> b) -> a -> b
$ ByteString -> ByteString
BL.toStrict ByteString
bytes of
      Left String
err -> String -> Either String ImageJpegBytes
forall a b. a -> Either a b
Left String
err
      Right DynamicImage
_ -> ImageJpegBytes -> Either String ImageJpegBytes
forall a b. b -> Either a b
Right (ImageJpegBytes -> Either String ImageJpegBytes)
-> ImageJpegBytes -> Either String ImageJpegBytes
forall a b. (a -> b) -> a -> b
$ ByteString -> ImageJpegBytes
MkImageJpegBytes ByteString
bytes