mirror of
https://github.com/TheAnachronism/docspell.git
synced 2025-06-21 18:08:25 +00:00
Fix encodings for mails with non-utf8 html parts
This commit is contained in:
@ -1,8 +1,9 @@
|
||||
package docspell.common
|
||||
|
||||
import fs2.{Pipe, Stream}
|
||||
import fs2.{Chunk, Pipe, Stream}
|
||||
import java.nio.charset.Charset
|
||||
import java.nio.charset.StandardCharsets
|
||||
import scodec.bits.ByteVector
|
||||
|
||||
final case class Binary[F[_]](name: String, mime: MimeType, data: Stream[F, Byte]) {
|
||||
|
||||
@ -25,9 +26,15 @@ object Binary {
|
||||
def text[F[_]](name: String, content: String): Binary[F] =
|
||||
utf8(name, content).withMime(MimeType.plain.withUtf8Charset)
|
||||
|
||||
def text[F[_]](name: String, content: ByteVector, cs: Charset): Binary[F] =
|
||||
Binary(name, MimeType.plain.withCharset(cs), Stream.chunk(Chunk.byteVector(content)))
|
||||
|
||||
def html[F[_]](name: String, content: String): Binary[F] =
|
||||
utf8(name, content).withMime(MimeType.html.withUtf8Charset)
|
||||
|
||||
def html[F[_]](name: String, content: ByteVector, cs: Charset): Binary[F] =
|
||||
Binary(name, MimeType.html.withCharset(cs), Stream.chunk(Chunk.byteVector(content)))
|
||||
|
||||
def decode[F[_]](cs: Charset): Pipe[F, Byte, String] =
|
||||
if (cs == StandardCharsets.UTF_8) {
|
||||
fs2.text.utf8Decode
|
||||
|
@ -28,9 +28,9 @@ object ReadMail {
|
||||
)(mail: Mail[F]): Stream[F, Binary[F]] = {
|
||||
val bodyEntry: F[Option[Binary[F]]] = mail.body.fold(
|
||||
_ => (None: Option[Binary[F]]).pure[F],
|
||||
txt => txt.text.map(c => Binary.text[F]("mail.txt", c).some),
|
||||
html => html.html.map(c => Binary.html[F]("mail.html", c).some),
|
||||
both => both.html.map(c => Binary.html[F]("mail.html", c).some)
|
||||
txt => txt.text.map(c => Binary.text[F]("mail.txt", c.bytes, c.charsetOrUtf8).some),
|
||||
html => html.html.map(c => Binary.html[F]("mail.html", c.bytes, c.charsetOrUtf8).some),
|
||||
both => both.html.map(c => Binary.html[F]("mail.html", c.bytes, c.charsetOrUtf8).some)
|
||||
)
|
||||
|
||||
Stream.eval(
|
||||
|
Reference in New Issue
Block a user