Replace generating preview images with an admin endpoint

It doesn't make much sense to have this per collective, because this
is triggered by an admin after changing the server config file. So it
is now implemented as an admin endpoint that affects all files.
This commit is contained in:
eikek
2021-07-04 21:37:34 +02:00
parent 4e7b516d57
commit 1120434cd9
8 changed files with 85 additions and 60 deletions

View File

@ -193,6 +193,14 @@ trait OItem[F[_]] {
account: AccountId,
notifyJoex: Boolean
): F[UpdateResult]
/** Submits a task that (re)generates the preview images for all
* attachments.
*/
def generateAllPreviews(
storeMode: MakePreviewArgs.StoreMode,
notifyJoex: Boolean
): F[UpdateResult]
}
object OItem {
@ -699,6 +707,16 @@ object OItem {
_ <- if (notifyJoex) joex.notifyAllNodes else ().pure[F]
} yield UpdateResult.success
def generateAllPreviews(
storeMode: MakePreviewArgs.StoreMode,
notifyJoex: Boolean
): F[UpdateResult] =
for {
job <- JobFactory.allPreviews[F](AllPreviewsArgs(None, storeMode), None)
_ <- queue.insertIfNew(job)
_ <- if (notifyJoex) joex.notifyAllNodes else ().pure[F]
} yield UpdateResult.success
private def onSuccessIgnoreError(update: F[Unit])(ar: UpdateResult): F[Unit] =
ar match {
case UpdateResult.Success =>

View File

@ -1136,30 +1136,6 @@ paths:
schema:
$ref: "#/components/schemas/BasicResult"
/sec/collective/previews:
post:
operationId: "sec-collective-previews-start-generate"
tags: [ Collective ]
summary: Starts the generate previews task
description: |
Submits a task that re-generates preview images of all
attachments of the current collective. Each existing preview
image will be replaced.
This can be used after changing the `preview` settings.
If only preview images of selected attachments should be
regenerated, see the `/sec/attachment/{id}/preview` endpoint.
security:
- authTokenHeader: []
responses:
200:
description: Ok
content:
application/json:
schema:
$ref: "#/components/schemas/BasicResult"
/sec/user:
get:
operationId: "sec-user-get-all"
@ -1348,6 +1324,32 @@ paths:
schema:
$ref: "#/components/schemas/ResetPasswordResult"
/admin/attachments/generatePreviews:
post:
operationId: "admin-attachments-generate-previews"
tags: [Attachment, Admin]
summary: (Re)generate all preview images
description: |
Submits a task that re-generates preview images of all
attachments. Each existing preview image will be replaced.
This can be used after changing the `preview` settings.
If only preview images of selected attachments should be
regenerated, see the `/sec/attachment/{id}/preview` endpoint.
This is an admin route, you need to specify the secret from
the config file via a http header `Docspell-Admin-Secret`.
security:
- adminHeader: []
responses:
200:
description: Ok
content:
application/json:
schema:
$ref: "#/components/schemas/BasicResult"
/sec/source:
get:
operationId: "sec-source-get-all"

View File

@ -108,9 +108,10 @@ object RestServer {
def adminRoutes[F[_]: Async](cfg: Config, restApp: RestApp[F]): HttpRoutes[F] =
Router(
"fts" -> FullTextIndexRoutes.admin(cfg, restApp.backend),
"user" -> UserRoutes.admin(restApp.backend),
"info" -> InfoRoutes.admin(cfg)
"fts" -> FullTextIndexRoutes.admin(cfg, restApp.backend),
"user" -> UserRoutes.admin(restApp.backend),
"info" -> InfoRoutes.admin(cfg),
"attachments" -> AttachmentRoutes.admin(restApp.backend)
)
def redirectTo[F[_]: Async](path: String): HttpRoutes[F] = {

View File

@ -189,4 +189,19 @@ object AttachmentRoutes {
} yield resp
}
}
def admin[F[_]: Async](backend: BackendApp[F]): HttpRoutes[F] = {
val dsl = Http4sDsl[F]
import dsl._
HttpRoutes.of { case POST -> Root / "generatePreviews" =>
for {
res <- backend.item.generateAllPreviews(MakePreviewArgs.StoreMode.Replace, true)
resp <- Ok(
Conversions.basicResult(res, "Generate all previews task submitted.")
)
} yield resp
}
}
}

View File

@ -12,7 +12,7 @@ import cats.implicits._
import docspell.backend.BackendApp
import docspell.backend.auth.AuthToken
import docspell.backend.ops.OCollective
import docspell.common.{ListType, MakePreviewArgs}
import docspell.common.ListType
import docspell.restapi.model._
import docspell.restserver.conv.Conversions
import docspell.restserver.http4s._
@ -101,18 +101,6 @@ object CollectiveRoutes {
resp <- Ok(BasicResult(true, "Task submitted"))
} yield resp
case POST -> Root / "previews" =>
for {
res <- backend.collective.generatePreviews(
MakePreviewArgs.StoreMode.Replace,
user.account,
true
)
resp <- Ok(
Conversions.basicResult(res, "Generate all previews task submitted.")
)
} yield resp
case GET -> Root =>
for {
collDb <- backend.collective.find(user.account.collective)