mirror of
https://github.com/TheAnachronism/docspell.git
synced 2025-06-22 02:18:26 +00:00
Add support for archive files
Each attachment is now first extracted into potentially multiple ones, if it is recognized as an archive. This is the first step in processing. The original archive file is also stored and the resulting attachments are associated to their original archive. First support is implemented for zip files.
This commit is contained in:
48
modules/files/src/main/scala/docspell/files/Zip.scala
Normal file
48
modules/files/src/main/scala/docspell/files/Zip.scala
Normal file
@ -0,0 +1,48 @@
|
||||
package docspell.files
|
||||
|
||||
import cats.effect._
|
||||
import cats.implicits._
|
||||
import fs2.{Pipe, Stream}
|
||||
import java.io.InputStream
|
||||
import java.util.zip.ZipInputStream
|
||||
import java.nio.file.Paths
|
||||
|
||||
object Zip {
|
||||
|
||||
case class Entry[F[_]](name: String, data: Stream[F, Byte])
|
||||
|
||||
def unzipP[F[_]: ConcurrentEffect: ContextShift](
|
||||
chunkSize: Int,
|
||||
blocker: Blocker
|
||||
): Pipe[F, Byte, Entry[F]] =
|
||||
s => unzip[F](chunkSize, blocker)(s)
|
||||
|
||||
def unzip[F[_]: ConcurrentEffect: ContextShift](chunkSize: Int, blocker: Blocker)(
|
||||
data: Stream[F, Byte]
|
||||
): Stream[F, Entry[F]] =
|
||||
data.through(fs2.io.toInputStream[F]).flatMap(in => unzipJava(in, chunkSize, blocker))
|
||||
|
||||
def unzipJava[F[_]: Sync: ContextShift](
|
||||
in: InputStream,
|
||||
chunkSize: Int,
|
||||
blocker: Blocker
|
||||
): Stream[F, Entry[F]] = {
|
||||
val zin = new ZipInputStream(in)
|
||||
|
||||
val nextEntry = Resource.make(Sync[F].delay(Option(zin.getNextEntry))) {
|
||||
case Some(_) => Sync[F].delay(zin.closeEntry())
|
||||
case None => ().pure[F]
|
||||
}
|
||||
|
||||
Stream
|
||||
.resource(nextEntry)
|
||||
.repeat
|
||||
.unNoneTerminate
|
||||
.map { ze =>
|
||||
val name = Paths.get(ze.getName()).getFileName.toString
|
||||
val data =
|
||||
fs2.io.readInputStream[F]((zin: InputStream).pure[F], chunkSize, blocker, false)
|
||||
Entry(name, data)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user