-
Notifications
You must be signed in to change notification settings - Fork 788
/
Part.scala
91 lines (79 loc) · 3.01 KB
/
Part.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* Copyright 2013 http4s.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.http4s
package multipart
import cats.effect.Sync
import fs2.Stream
import fs2.io.file.Files
import fs2.io.file.Flags
import fs2.io.file.Path
import fs2.io.readInputStream
import fs2.text.utf8
import org.http4s.headers.`Content-Disposition`
import org.typelevel.ci._
import java.io.File
import java.io.InputStream
import java.net.URL
final case class Part[F[_]](headers: Headers, body: Stream[F, Byte]) extends Media[F] {
def name: Option[String] = headers.get[`Content-Disposition`].flatMap(_.parameters.get(ci"name"))
def filename: Option[String] =
headers.get[`Content-Disposition`].flatMap(_.parameters.get(ci"filename"))
override def covary[F2[x] >: F[x]]: Part[F2] = this.asInstanceOf[Part[F2]]
}
object Part {
private val ChunkSize = 8192
def formData[F[_]](name: String, value: String, headers: Header.ToRaw*): Part[F] =
Part(
Headers(`Content-Disposition`("form-data", Map(ci"name" -> name))).put(headers: _*),
Stream.emit(value).through(utf8.encode),
)
@deprecated("Use overload with fs2.io.file.Path", "0.23.5")
def fileData[F[_]: Files](name: String, file: File, headers: Header.ToRaw*): Part[F] =
fileData(name, Path.fromNioPath(file.toPath), headers: _*)
def fileData[F[_]: Files](name: String, path: Path, headers: Header.ToRaw*): Part[F] =
fileData(
name,
path.fileName.toString,
Files[F].readAll(path, ChunkSize, Flags.Read),
headers: _*
)
def fileData[F[_]: Sync](name: String, resource: URL, headers: Header.ToRaw*): Part[F] =
fileData(name, resource.getPath.split("/").last, resource.openStream(), headers: _*)
def fileData[F[_]](
name: String,
filename: String,
entityBody: EntityBody[F],
headers: Header.ToRaw*
): Part[F] =
Part(
Headers(
`Content-Disposition`("form-data", Map(ci"name" -> name, ci"filename" -> filename)),
"Content-Transfer-Encoding" -> "binary",
).put(headers: _*),
entityBody,
)
// The InputStream is passed by name, and we open it in the by-name
// argument in callers, so we can avoid lifting into an effect. Exposing
// this API publicly would invite unsafe use, and the `EntityBody` version
// should be safe.
private def fileData[F[_]](
name: String,
filename: String,
in: => InputStream,
headers: Header.ToRaw*
)(implicit F: Sync[F]): Part[F] =
fileData(name, filename, readInputStream(F.delay(in), ChunkSize), headers: _*)
}