@@ -8,10 +8,11 @@ import app.softnetwork.resource.handlers.GenericResourceHandler
88import app .softnetwork .resource .message .ResourceMessages ._
99import app .softnetwork .resource .spi .{ResourceProvider , SimpleResource }
1010import app .softnetwork .session .service .ServiceWithSessionEndpoints
11+ import org .apache .tika .mime .MediaType
1112import org .softnetwork .session .model .Session
1213import sttp .capabilities .akka .AkkaStreams
1314import sttp .model .headers .CookieValueWithMeta
14- import sttp .model .{Method , Part , StatusCode }
15+ import sttp .model .{HeaderNames , Method , Part , StatusCode }
1516import sttp .tapir ._
1617import sttp .tapir .json .json4s .jsonBody
1718import sttp .tapir .server .{PartialServerEndpointWithSecurityOutput , ServerEndpoint }
@@ -56,27 +57,31 @@ trait ResourceServiceEndpoints
5657
5758 def loadResourceBusinessLogic (segments : List [String ]): Either [
5859 ApiErrors .ErrorInfo ,
59- Source [ByteString , Any ]
60+ ( Source [ByteString , Any ], Option [ MediaType ])
6061 ] =
6162 loadResource(segments) match {
62- case Some ((path, _ )) => Right (FileIO .fromPath(path))
63- case _ => Left (error(ResourceNotFound ))
63+ case Some ((path, media )) => Right (( FileIO .fromPath(path), media ))
64+ case _ => Left (error(ResourceNotFound ))
6465 }
6566
66- val getResource : ServerEndpoint [Any with AkkaStreams , Future ] =
67- secureEndpoint.get
67+ def getResource (prefix : Option [String ]): ServerEndpoint [Any with AkkaStreams , Future ] = {
68+ (prefix match {
69+ case Some (value) => secureEndpoint.get.in(value)
70+ case _ => secureEndpoint.get
71+ })
6872 .in(paths)
6973 .out(streamBinaryBody(AkkaStreams )(CodecFormat .OctetStream ()))
70- .serverLogic(_ => segments => Future .successful(loadResourceBusinessLogic(segments)))
71-
72- val getImage : ServerEndpoint [Any with AkkaStreams , Future ] =
73- secureEndpoint.get
74- .in(" images" )
75- .in(paths)
76- .out(
77- streamBinaryBody(AkkaStreams )(CodecFormat .OctetStream ())
74+ .out(header[Option [String ]](HeaderNames .ContentType ))
75+ .serverLogic(_ =>
76+ segments =>
77+ Future .successful(loadResourceBusinessLogic(segments) match {
78+ case Left (l) => Left (l)
79+ case Right (r) => Right (r._1, r._2.map(_.toString))
80+ })
7881 )
79- .serverLogic(_ => segments => Future .successful(loadResourceBusinessLogic(segments)))
82+ }
83+
84+ val getImage : ServerEndpoint [Any with AkkaStreams , Future ] = getResource(Some (" images" ))
8085
8186 def uploadResource [T <: Upload ](implicit
8287 multipartCodec : MultipartCodec [T ]
@@ -251,7 +256,7 @@ trait ResourceServiceEndpoints
251256 deleteImage,
252257 addResource[UploadResource ],
253258 updateResource[UploadResource ],
254- getResource,
259+ getResource( None ) ,
255260 deleteResource
256261 )
257262
0 commit comments