All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fr.janalyse.sotohp.cli.TemporaryHackExtractPhotosFromCluster.scala Maven / Gradle / Ivy

The newest version!
package fr.janalyse.sotohp.cli

import fr.janalyse.sotohp.core.*
import fr.janalyse.sotohp.model.*
import fr.janalyse.sotohp.processor.NormalizeProcessor
import fr.janalyse.sotohp.store.{LazyPhoto, PhotoStoreService}
import wvlet.airframe.ulid.ULID
import zio.*
import zio.stream.*
import zio.config.typesafe.*
import zio.lmdb.LMDB

import java.time.temporal.ChronoUnit.{MONTHS, YEARS}
import java.time.{Instant, OffsetDateTime}
import scala.io.AnsiColor.*

object TemporaryHackExtractPhotosFromCluster extends ZIOAppDefault with CommonsCLI {

  override val bootstrap: ZLayer[ZIOAppArgs, Any, Any] =
    Runtime.setConfigProvider(TypesafeConfigProvider.fromTypesafeConfig(com.typesafe.config.ConfigFactory.load()))

  override def run =
    logic
      .provide(
        LMDB.liveWithDatabaseName("photos"),
        PhotoStoreService.live,
        Scope.default
      )

  val facePathJavaStream = {
    import java.nio.file.{Files, Path}
    ZStream.fromJavaStream(
      Files.find(Path.of("cluster-rodrigue"), 2, (path, attrs) => path.toString.contains(".png"))
    )
  }

  def faceIdToOriginalPath(faceId: FaceId) = for {
    faceFeatures <- PhotoStoreService.photoFaceFeaturesGet(faceId).some
    photoState   <- PhotoStoreService.photoStateGet(faceFeatures.photoId).some
    photoSource  <- PhotoStoreService.photoSourceGet(photoState.originalId).some
  } yield photoSource.original.path

  def extractOriginal(originalPath: java.nio.file.Path) = {
    ZIO.attempt {
      import java.nio.file.{Files, Path}
      val targetDirectoryPath = Path.of("originals-rodrigue", Path.of("/home", "ALBUMS").relativize(originalPath.getParent).toString)
      Files.createDirectories(targetDirectoryPath)
      val targetPath          = targetDirectoryPath.resolve(originalPath.getFileName)
      Files.copy(originalPath, targetPath)
    }
  }

  val logic = ZIO.logSpan("extracts-originals") {
    facePathJavaStream
      .map(path => path.toFile.getName.replace(".png", ""))
      .mapZIO(id => ZIO.attempt(FaceId(ULID.fromString(id))))
      .mapZIO(faceId => faceIdToOriginalPath(faceId))
      .runForeach(original => extractOriginal(original))
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy