Fix deleting items and attachments to also remove the binary files

This commit is contained in:
Eike Kettner 2020-02-22 00:49:03 +01:00
parent 8cfecfb3dd
commit 74a037887d
2 changed files with 10 additions and 8 deletions

View File

@ -8,22 +8,22 @@ import doobie.implicits._
import docspell.common.{Ident, MetaProposalList}
import docspell.store.Store
import docspell.store.impl.Implicits._
import docspell.store.records.{RAttachment, RAttachmentMeta, RItem}
import docspell.store.records.{RAttachment, RAttachmentMeta, RAttachmentSource, RItem}
object QAttachment {
def deleteById[F[_]: Sync](store: Store[F])(attachId: Ident, coll: Ident): F[Int] =
for {
raOpt <- store.transact(RAttachment.findByIdAndCollective(attachId, coll))
n <- raOpt.traverse(_ => store.transact(RAttachment.delete(attachId)))
f <- Stream
.emit(raOpt)
.unNoneTerminate
.map(_.fileId.id)
raFile <- store.transact(RAttachment.findByIdAndCollective(attachId, coll)).map(_.map(_.fileId))
rsFile <- store.transact(RAttachmentSource.findById(attachId)).map(_.map(_.fileId))
n <- store.transact(RAttachment.delete(attachId))
f <- Stream.emits(raFile.toSeq ++ rsFile.toSeq)
.map(_.id)
.flatMap(store.bitpeace.delete)
.map(flag => if (flag) 1 else 0)
.compile
.last
} yield n.getOrElse(0) + f.map(_ => 1).getOrElse(0)
} yield n + f.getOrElse(0)
def deleteAttachment[F[_]: Sync](store: Store[F])(ra: RAttachment): F[Int] =
for {

View File

@ -59,4 +59,6 @@ object RAttachmentSource {
selectSimple(all.map(_.prefix("a")), from, where).query[RAttachmentSource].option
}
}