Skip to content

Commit

Permalink
extract method fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
seakayone committed Aug 4, 2023
1 parent be98811 commit 184805c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 59 deletions.
1 change: 1 addition & 0 deletions src/main/scala/swiss/dasch/api/MaintenanceEndpoint.scala
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ object MaintenanceEndpoint {

val needsOriginalsEndpoint = Endpoint
.get(maintenance / "needs-originals")
.query(queryBool("imagesOnly"))
.out[String](Status.Accepted)
.outErrors(
HttpCodec.error[ProjectNotFound](Status.NotFound),
Expand Down
119 changes: 60 additions & 59 deletions src/main/scala/swiss/dasch/api/MaintenanceEndpointRoutes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import zio.nio.file.Files
import zio.{ Chunk, IO, ZIO }

import java.io.IOException
import zio.json.JsonEncoder

object MaintenanceEndpointRoutes {

Expand All @@ -21,40 +22,45 @@ object MaintenanceEndpointRoutes {
}
)

private val needsOriginalsRoute = needsOriginalsEndpoint.implement(_ =>
(for {
_ <- ZIO.logInfo(s"Checking for originals")
assetDir <- StorageService.getAssetDirectory()
tmpDir <- StorageService.getTempDirectory()
projectShortcodes <- ProjectService.listAllProjects()
_ <- ZIO
.foreach(projectShortcodes)(shortcode =>
Files
.walk(assetDir / shortcode.toString)
.mapZIOPar(8)(originalNotPresent)
.filter(identity)
.as(ProjectResponse.make(shortcode))
.runHead
)
.map(_.flatten)
.flatMap(
Files.createDirectories(tmpDir / "reports") *>
Files.deleteIfExists(tmpDir / "reports" / "needsOriginals.json") *>
Files.createFile(tmpDir / "reports" / "needsOriginals.json") *>
StorageService.saveJsonFile(tmpDir / "reports" / "needsOriginals.json", _)
)
.zipLeft(ZIO.logInfo(s"Created needsOriginals.json"))
.logError
.forkDaemon
private def saveReport[A](
tmpDir: file.Path,
name: String,
report: A,
)(implicit encoder: JsonEncoder[A]
): ZIO[StorageService, Throwable, Unit] =
Files.createDirectories(tmpDir / "reports") *>
Files.deleteIfExists(tmpDir / "reports" / s"$name.json") *>
Files.createFile(tmpDir / "reports" / s"$name.json") *>
StorageService.saveJsonFile(tmpDir / "reports" / s"$name.json", report)

} yield "work in progress")
.logError
.mapError(it => ApiProblem.internalError(it))
private val needsOriginalsRoute = needsOriginalsEndpoint.implement(imagesOnly =>
(
for {
_ <- ZIO.logInfo(s"Checking for originals")
assetDir <- StorageService.getAssetDirectory()
tmpDir <- StorageService.getTempDirectory()
projectShortcodes <- ProjectService.listAllProjects()
_ <- ZIO
.foreach(projectShortcodes)(shortcode =>
Files
.walk(assetDir / shortcode.toString)
.mapZIOPar(8)(originalNotPresent(imagesOnly))
.filter(identity)
.as(ProjectResponse.make(shortcode))
.runHead
)
.map(_.flatten)
.flatMap(saveReport(tmpDir, "needsOriginals", _))
.zipLeft(ZIO.logInfo(s"Created needsOriginals.json"))
.logError
.forkDaemon
} yield "work in progress"
).logError.mapError(ApiProblem.internalError)
)

private def originalNotPresent(path: file.Path): IO[IOException, Boolean] = {
private def originalNotPresent(imagesOnly: Boolean)(path: file.Path): IO[IOException, Boolean] = {
val assetId = AssetId.makeFromPath(path).map(_.toString).getOrElse("unknown-asset-id")
FileFilters.isImage(path) &&
(ZIO.succeed(imagesOnly).negate || FileFilters.isImage(path)) &&
Files
.list(path.parent.orNull)
.map(_.filename.toString)
Expand All @@ -79,35 +85,30 @@ object MaintenanceEndpointRoutes {

private val needsTopLeftCorrectionRoute =
needsTopLeftCorrectionEndpoint.implement(_ =>
(for {
_ <- ZIO.logInfo(s"Checking for top left correction")
assetDir <- StorageService.getAssetDirectory()
tmpDir <- StorageService.getTempDirectory()
imageService <- ZIO.service[ImageService]
projectShortcodes <- ProjectService.listAllProjects()
_ <-
ZIO
.foreach(projectShortcodes)(shortcode =>
Files
.walk(assetDir / shortcode.toString)
.mapZIOPar(8)(imageService.needsTopLeftCorrection)
.filter(identity)
.runHead
.map(_.map(_ => ProjectResponse.make(shortcode)))
)
.map(_.flatten)
.flatMap(
Files.createDirectories(tmpDir / "reports") *>
Files.deleteIfExists(tmpDir / "reports" / "needsTopLeftCorrection.json") *>
Files.createFile(tmpDir / "reports" / "needsTopLeftCorrection.json") *>
StorageService.saveJsonFile(tmpDir / "reports" / "needsTopLeftCorrection.json", _)
)
.zipLeft(ZIO.logInfo(s"Created needsTopLeftCorrection.json"))
.logError
.forkDaemon
} yield "work in progress")
.logError
.mapError(ApiProblem.internalError)
(
for {
_ <- ZIO.logInfo(s"Checking for top left correction")
assetDir <- StorageService.getAssetDirectory()
tmpDir <- StorageService.getTempDirectory()
imageService <- ZIO.service[ImageService]
projectShortcodes <- ProjectService.listAllProjects()
_ <-
ZIO
.foreach(projectShortcodes)(shortcode =>
Files
.walk(assetDir / shortcode.toString)
.mapZIOPar(8)(imageService.needsTopLeftCorrection)
.filter(identity)
.runHead
.map(_.map(_ => ProjectResponse.make(shortcode)))
)
.map(_.flatten)
.flatMap(saveReport(tmpDir, "needsTopLeftCorrection", _))
.zipLeft(ZIO.logInfo(s"Created needsTopLeftCorrection.json"))
.logError
.forkDaemon
} yield "work in progress"
).logError.mapError(ApiProblem.internalError)
)

private val applyTopLeftCorrectionRoute =
Expand Down

0 comments on commit 184805c

Please sign in to comment.