forked from zio/zio-http
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
180 additions
and
5 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
zio-http-example/src/main/scala/example/MultipartServer.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package example | ||
|
||
import zio.http._ | ||
import zio.http.model.Multipart.FileUpload | ||
import zio.stream.ZPipeline | ||
import zio.{ZIO, ZIOAppDefault, ZLayer} | ||
|
||
object MultipartServer extends ZIOAppDefault { | ||
|
||
val app = Http.collectZIO[Request] { case req => | ||
ZIO.scoped { | ||
req.body.multipart.flatMap(x => | ||
ZIO.foreachDiscard(x)(m => | ||
(ZIO.debug(s"${m.name} - ${m.getClass.getSimpleName} - ${m.length}")) *> | ||
ZIO.whenCase(m) { case u: FileUpload => | ||
ZIO.debug(s"File name: ${u.filename}}") *> | ||
u.content.via(ZPipeline.utf8Decode >>> ZPipeline.splitLines).foreach(ZIO.debug(_)) | ||
}, | ||
), | ||
) | ||
} as Response.ok | ||
} | ||
|
||
val run = | ||
ZIO.debug("To test upload big file:\ncurl -v -F key1=value1 -F upload=@bigfile.txt localhost:8080") *> | ||
Server | ||
.serve(app) | ||
.provide(ZLayer.succeed(ServerConfig.default.objectAggregator(1024 * 3000)), Server.live) | ||
.exitCode | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package zio.http | ||
|
||
import io.netty.handler.codec.http.multipart._ | ||
import zio.{RIO, Scope, ZIO} | ||
import zio.http.model.Multipart | ||
import zio.http.model.Multipart.{Attribute, FileUpload} | ||
import zio.stream.ZStream | ||
|
||
import java.nio.file.Paths | ||
|
||
object MultipartConverter { | ||
|
||
def convert(data: InterfaceHttpData): RIO[Scope, Multipart] = data match { | ||
|
||
case attribute: io.netty.handler.codec.http.multipart.Attribute => | ||
val value: RIO[Scope, String] = attribute match { | ||
case a: DiskAttribute => ZIO.attempt(a.getValue) | ||
case a: MemoryAttribute => ZIO.succeed(a.getValue) | ||
case a: MixedAttribute => | ||
// TODO should I just use this instead of pattern matching? | ||
if (a.isInMemory) | ||
ZIO.succeed(a.getValue) | ||
else | ||
ZIO.attempt(a.getValue) | ||
} | ||
|
||
value.map(v => | ||
Attribute( | ||
attribute.getName, | ||
attribute.length, | ||
attribute.definedLength, | ||
attribute.getCharset.name, | ||
v, | ||
), | ||
) | ||
|
||
case fileUpload: io.netty.handler.codec.http.multipart.FileUpload => | ||
val value: ZStream[Any, Throwable, Byte] = fileUpload match { | ||
case u: DiskFileUpload => | ||
ZStream.fromPath(u.getFile.toPath) | ||
case u: MemoryFileUpload => | ||
ZStream.fromIterable(u.get) | ||
case u: MixedFileUpload => | ||
// TODO should I just use this instead of pattern matching? | ||
if (u.isInMemory) | ||
ZStream.fromIterable(u.get) | ||
else { | ||
println(s"FFF ${u.getFile.toPath}") | ||
ZStream.fromPath(u.getFile.toPath) | ||
} | ||
} | ||
ZIO.succeed( | ||
FileUpload( | ||
fileUpload.getName, | ||
fileUpload.length, | ||
fileUpload.definedLength, | ||
fileUpload.getCharset.name, | ||
fileUpload.getFilename, | ||
fileUpload.getContentType, | ||
fileUpload.getContentTransferEncoding, | ||
value, | ||
), | ||
) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package zio.http.model | ||
|
||
import zio.http.model.Multipart.{Attribute, FileUpload} | ||
import zio.stream.ZStream | ||
|
||
sealed trait Multipart { | ||
def name: String | ||
def length: Long | ||
|
||
/** | ||
* Returns the defined length of the HttpData. | ||
* | ||
* If no Content-Length is provided in the request, the defined length is | ||
* always 0 (whatever during decoding or in final state). | ||
* | ||
* If Content-Length is provided in the request, this is this given defined | ||
* length. This value does not change, whatever during decoding or in the | ||
* final state. | ||
* | ||
* This method could be used for instance to know the amount of bytes | ||
* transmitted for one particular HttpData, for example one {@link FileUpload} | ||
* or any known big {@link Attribute}. | ||
* | ||
* @return | ||
* the defined length of the HttpData | ||
*/ | ||
def definedLength: Long | ||
def charset: String | ||
} | ||
|
||
object Multipart { | ||
|
||
case class Attribute( | ||
name: String, | ||
length: Long, | ||
definedLength: Long, | ||
charset: String, | ||
value: String, | ||
) extends Multipart | ||
|
||
case class FileUpload( | ||
name: String, | ||
length: Long, | ||
definedLength: Long, | ||
charset: String, | ||
filename: String, | ||
contentType: String, | ||
contentTransferEncoding: String, | ||
content: ZStream[Any, Throwable, Byte], | ||
) extends Multipart | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters