mirror of
https://github.com/TheAnachronism/docspell.git
synced 2025-06-22 10:28:27 +00:00
Add a file-repository for better organizing files
Docspell now must use a new api for accessing files. Issue: #1379
This commit is contained in:
@ -14,6 +14,7 @@ import fs2.Stream
|
||||
import docspell.backend.ops.OItemSearch._
|
||||
import docspell.common._
|
||||
import docspell.store._
|
||||
import docspell.store.file.FileMetadata
|
||||
import docspell.store.queries.{QAttachment, QItem}
|
||||
import docspell.store.records._
|
||||
|
||||
@ -89,18 +90,21 @@ object OItemSearch {
|
||||
trait BinaryData[F[_]] {
|
||||
def data: Stream[F, Byte]
|
||||
def name: Option[String]
|
||||
def meta: RFileMeta
|
||||
def fileId: Ident
|
||||
def meta: FileMetadata
|
||||
def fileId: FileKey
|
||||
}
|
||||
case class AttachmentData[F[_]](ra: RAttachment, meta: RFileMeta, data: Stream[F, Byte])
|
||||
extends BinaryData[F] {
|
||||
case class AttachmentData[F[_]](
|
||||
ra: RAttachment,
|
||||
meta: FileMetadata,
|
||||
data: Stream[F, Byte]
|
||||
) extends BinaryData[F] {
|
||||
val name = ra.name
|
||||
val fileId = ra.fileId
|
||||
}
|
||||
|
||||
case class AttachmentSourceData[F[_]](
|
||||
rs: RAttachmentSource,
|
||||
meta: RFileMeta,
|
||||
meta: FileMetadata,
|
||||
data: Stream[F, Byte]
|
||||
) extends BinaryData[F] {
|
||||
val name = rs.name
|
||||
@ -109,7 +113,7 @@ object OItemSearch {
|
||||
|
||||
case class AttachmentPreviewData[F[_]](
|
||||
rs: RAttachmentPreview,
|
||||
meta: RFileMeta,
|
||||
meta: FileMetadata,
|
||||
data: Stream[F, Byte]
|
||||
) extends BinaryData[F] {
|
||||
val name = rs.name
|
||||
@ -118,7 +122,7 @@ object OItemSearch {
|
||||
|
||||
case class AttachmentArchiveData[F[_]](
|
||||
rs: RAttachmentArchive,
|
||||
meta: RFileMeta,
|
||||
meta: FileMetadata,
|
||||
data: Stream[F, Byte]
|
||||
) extends BinaryData[F] {
|
||||
val name = rs.name
|
||||
@ -188,7 +192,7 @@ object OItemSearch {
|
||||
AttachmentData[F](
|
||||
ra,
|
||||
m,
|
||||
store.fileStore.getBytes(m.id)
|
||||
store.fileRepo.getBytes(m.id)
|
||||
)
|
||||
}
|
||||
|
||||
@ -208,7 +212,7 @@ object OItemSearch {
|
||||
AttachmentSourceData[F](
|
||||
ra,
|
||||
m,
|
||||
store.fileStore.getBytes(m.id)
|
||||
store.fileRepo.getBytes(m.id)
|
||||
)
|
||||
}
|
||||
|
||||
@ -228,7 +232,7 @@ object OItemSearch {
|
||||
AttachmentPreviewData[F](
|
||||
ra,
|
||||
m,
|
||||
store.fileStore.getBytes(m.id)
|
||||
store.fileRepo.getBytes(m.id)
|
||||
)
|
||||
}
|
||||
|
||||
@ -248,7 +252,7 @@ object OItemSearch {
|
||||
AttachmentPreviewData[F](
|
||||
ra,
|
||||
m,
|
||||
store.fileStore.getBytes(m.id)
|
||||
store.fileRepo.getBytes(m.id)
|
||||
)
|
||||
}
|
||||
|
||||
@ -268,7 +272,7 @@ object OItemSearch {
|
||||
AttachmentArchiveData[F](
|
||||
ra,
|
||||
m,
|
||||
store.fileStore.getBytes(m.id)
|
||||
store.fileRepo.getBytes(m.id)
|
||||
)
|
||||
}
|
||||
|
||||
@ -276,9 +280,11 @@ object OItemSearch {
|
||||
(None: Option[AttachmentArchiveData[F]]).pure[F]
|
||||
}
|
||||
|
||||
private def makeBinaryData[A](fileId: Ident)(f: RFileMeta => A): F[Option[A]] =
|
||||
store.fileStore
|
||||
.findMeta(fileId)
|
||||
private def makeBinaryData[A](fileId: FileKey)(f: FileMetadata => A): F[Option[A]] =
|
||||
OptionT(
|
||||
store.fileRepo
|
||||
.findMeta(fileId)
|
||||
)
|
||||
.map(fm => f(fm))
|
||||
.value
|
||||
|
||||
|
@ -249,7 +249,7 @@ object OMail {
|
||||
} yield {
|
||||
val addAttach = m.attach.filter(ras).map { a =>
|
||||
Attach[F](
|
||||
store.fileStore.getBytes(a._2.id)
|
||||
store.fileRepo.getBytes(a._2.id)
|
||||
).withFilename(a._1.name)
|
||||
.withLength(a._2.length.bytes)
|
||||
.withMimeType(a._2.mimetype.toEmil)
|
||||
|
@ -126,7 +126,7 @@ object OUpload {
|
||||
): F[OUpload.UploadResult] =
|
||||
(for {
|
||||
_ <- checkExistingItem(itemId, account.collective)
|
||||
files <- right(data.files.traverse(saveFile).map(_.flatten))
|
||||
files <- right(data.files.traverse(saveFile(account)).map(_.flatten))
|
||||
_ <- checkFileList(files)
|
||||
lang <- data.meta.language match {
|
||||
case Some(lang) => right(lang.pure[F])
|
||||
@ -200,10 +200,18 @@ object OUpload {
|
||||
} yield UploadResult.Success
|
||||
|
||||
/** Saves the file into the database. */
|
||||
private def saveFile(file: File[F]): F[Option[ProcessItemArgs.File]] =
|
||||
private def saveFile(
|
||||
accountId: AccountId
|
||||
)(file: File[F]): F[Option[ProcessItemArgs.File]] =
|
||||
logger.finfo(s"Receiving file $file") *>
|
||||
file.data
|
||||
.through(store.fileStore.save(MimeTypeHint(file.name, None)))
|
||||
.through(
|
||||
store.fileRepo.save(
|
||||
accountId.collective,
|
||||
FileCategory.AttachmentSource,
|
||||
MimeTypeHint(file.name, None)
|
||||
)
|
||||
)
|
||||
.compile
|
||||
.lastOrError
|
||||
.attempt
|
||||
|
Reference in New Issue
Block a user