delete all remaining DbImage code - feels good
parent
6799979ae3
commit
63a27c68f0
|
@ -17,7 +17,6 @@ import lila.user.{ Holder, User }
|
|||
|
||||
final class Env(
|
||||
val config: Configuration,
|
||||
val imageRepo: lila.db.ImageRepo,
|
||||
val api: lila.api.Env,
|
||||
val user: lila.user.Env,
|
||||
val mailer: lila.mailer.Env,
|
||||
|
@ -212,7 +211,6 @@ final class EnvBoot(
|
|||
implicit def idGenerator = game.idGenerator
|
||||
|
||||
lazy val mainDb: lila.db.Db = mongo.blockingDb("main", config.get[String]("mongodb.uri"))
|
||||
lazy val imageRepo = new lila.db.ImageRepo(mainDb(CollName("image")))
|
||||
|
||||
// wire all the lila modules
|
||||
lazy val memo: lila.memo.Env = wire[lila.memo.Env]
|
||||
|
|
|
@ -1,66 +0,0 @@
|
|||
package lila.coach
|
||||
|
||||
import scala.concurrent.duration._
|
||||
import akka.stream.scaladsl._
|
||||
import play.api.libs.Files
|
||||
import play.api.mvc.MultipartFormData
|
||||
import reactivemongo.akkastream.cursorProducer
|
||||
|
||||
import lila.common.LilaStream
|
||||
import lila.db.dsl._
|
||||
import lila.game.Pov
|
||||
import lila.memo.PicfitApi
|
||||
import akka.util.ByteString
|
||||
|
||||
final private class CoachPictureMigration(
|
||||
coll: Coll,
|
||||
picfitApi: PicfitApi,
|
||||
imageRepo: lila.db.ImageRepo
|
||||
)(implicit
|
||||
ec: scala.concurrent.ExecutionContext,
|
||||
mat: akka.stream.Materializer
|
||||
) {
|
||||
|
||||
import BsonHandlers._
|
||||
|
||||
def apply() =
|
||||
coll
|
||||
.find($doc("picturePath" $exists true, "picture" $exists false))
|
||||
.sort($sort desc "user.seenAt")
|
||||
.cursor[Bdoc]()
|
||||
.documentSource()
|
||||
.throttle(20, 1 second)
|
||||
.mapAsync(1)(migrate)
|
||||
.via(lila.common.LilaStream.logRate[Unit]("coach.picfit.migration")(logger))
|
||||
.toMat(LilaStream.sinkCount)(Keep.right)
|
||||
.run()
|
||||
.addEffect { count =>
|
||||
println(s"$count coaches migrated")
|
||||
}
|
||||
.void
|
||||
|
||||
private def migrate(s: Bdoc): Funit = {
|
||||
val coachId = s string "_id" err "missing id"
|
||||
val picPath = s string "picturePath" err "missing picturePath"
|
||||
val picId = picPath.split('/').headOption err "missing picId"
|
||||
imageRepo.fetch(picId) flatMap {
|
||||
case None =>
|
||||
println(s"Missing picture for coach $coachId pic $picPath")
|
||||
funit
|
||||
case Some(pic) =>
|
||||
val part: MultipartFormData.FilePart[Source[ByteString, _]] = MultipartFormData.FilePart(
|
||||
key = "data",
|
||||
filename = pic.name,
|
||||
contentType = "image/jpeg".some,
|
||||
ref = Source(ByteString.fromArray(pic.data) :: Nil),
|
||||
fileSize = pic.size
|
||||
)
|
||||
picfitApi.uploadSource(s"coach:$coachId", part, coachId, monitor = false) flatMap { picture =>
|
||||
coll.updateField($id(coachId), "picture", picture.id.value).void
|
||||
}
|
||||
} recover { case e: Exception =>
|
||||
if (!e.getMessage.contains("Invalid file type: "))
|
||||
logger.error(s"Can't migrate coach $coachId", e)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -21,8 +21,7 @@ final class Env(
|
|||
notifyApi: lila.notify.NotifyApi,
|
||||
cacheApi: lila.memo.CacheApi,
|
||||
db: lila.db.Db,
|
||||
picfitApi: lila.memo.PicfitApi,
|
||||
imageRepo: lila.db.ImageRepo
|
||||
picfitApi: lila.memo.PicfitApi
|
||||
)(implicit ec: scala.concurrent.ExecutionContext, system: akka.actor.ActorSystem) {
|
||||
|
||||
private val config = appConfig.get[CoachConfig]("coach")(AutoConfig.loader)
|
||||
|
@ -59,8 +58,4 @@ final class Env(
|
|||
black ?? api.setRating
|
||||
}.unit
|
||||
}
|
||||
|
||||
system.scheduler.scheduleOnce(1 minute) {
|
||||
wire[CoachPictureMigration]().unit
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,50 +0,0 @@
|
|||
package lila.db
|
||||
|
||||
import java.nio.file.{ Files, Path }
|
||||
import org.joda.time.DateTime
|
||||
import reactivemongo.api.bson._
|
||||
|
||||
import dsl._
|
||||
|
||||
case class DbImage(
|
||||
_id: String,
|
||||
data: Array[Byte],
|
||||
hash: String,
|
||||
name: String,
|
||||
contentType: Option[String],
|
||||
size: Int, // in bytes
|
||||
createdAt: DateTime,
|
||||
createdBy: Option[String]
|
||||
) {
|
||||
|
||||
def id = _id
|
||||
|
||||
def path = s"$id/$hash/$name"
|
||||
}
|
||||
|
||||
object DbImage {
|
||||
|
||||
def make(
|
||||
id: String,
|
||||
name: String,
|
||||
contentType: Option[String],
|
||||
path: Path,
|
||||
size: Int,
|
||||
createdBy: String
|
||||
) = {
|
||||
import com.roundeights.hasher.Implicits._
|
||||
val data = Files.readAllBytes(path)
|
||||
DbImage(
|
||||
_id = id,
|
||||
data = data,
|
||||
hash = data.md5.hex take 8,
|
||||
name = name,
|
||||
contentType = contentType,
|
||||
size = size,
|
||||
createdAt = DateTime.now,
|
||||
createdBy = createdBy.some
|
||||
)
|
||||
}
|
||||
|
||||
implicit val DbImageBSONHandler = Macros.handler[DbImage]
|
||||
}
|
|
@ -1,13 +0,0 @@
|
|||
package lila.db
|
||||
|
||||
import DbImage.DbImageBSONHandler
|
||||
|
||||
import lila.db.dsl._
|
||||
|
||||
final class ImageRepo(val coll: Coll)(implicit ec: scala.concurrent.ExecutionContext) {
|
||||
|
||||
def fetch(id: String): Fu[Option[DbImage]] = coll.byId[DbImage](id)
|
||||
|
||||
def save(image: DbImage): Funit =
|
||||
coll.update.one($id(image.id), image, upsert = true).void
|
||||
}
|
|
@ -29,8 +29,7 @@ final class Env(
|
|||
notifyApi: lila.notify.NotifyApi,
|
||||
userRepo: lila.user.UserRepo,
|
||||
timeline: lila.hub.actors.Timeline,
|
||||
db: lila.db.Db,
|
||||
imageRepo: lila.db.ImageRepo
|
||||
db: lila.db.Db
|
||||
)(implicit
|
||||
ec: scala.concurrent.ExecutionContext,
|
||||
system: ActorSystem
|
||||
|
|
|
@ -1,68 +0,0 @@
|
|||
package lila.streamer
|
||||
|
||||
import scala.concurrent.duration._
|
||||
import akka.stream.scaladsl._
|
||||
import play.api.libs.Files
|
||||
import play.api.mvc.MultipartFormData
|
||||
import reactivemongo.akkastream.cursorProducer
|
||||
|
||||
import lila.common.LilaStream
|
||||
import lila.db.dsl._
|
||||
import lila.game.Pov
|
||||
import lila.memo.PicfitApi
|
||||
import akka.util.ByteString
|
||||
|
||||
final private class StreamerPictureMigration(
|
||||
api: StreamerApi,
|
||||
coll: Coll,
|
||||
picfitApi: PicfitApi,
|
||||
imageRepo: lila.db.ImageRepo
|
||||
)(implicit
|
||||
ec: scala.concurrent.ExecutionContext,
|
||||
mat: akka.stream.Materializer
|
||||
) {
|
||||
|
||||
import BsonHandlers._
|
||||
|
||||
def apply() =
|
||||
coll
|
||||
.find($doc("picturePath" $exists true, "picture" $exists false))
|
||||
.sort($sort desc "liveAt")
|
||||
.cursor[Bdoc]()
|
||||
.documentSource()
|
||||
.throttle(20, 1 second)
|
||||
.mapAsync(1)(migrate)
|
||||
.via(lila.common.LilaStream.logRate[Unit]("streamer.picfit.migration")(logger))
|
||||
.toMat(LilaStream.sinkCount)(Keep.right)
|
||||
.run()
|
||||
.addEffect { count =>
|
||||
println(s"$count streamers migrated")
|
||||
}
|
||||
.void
|
||||
|
||||
private def migrate(s: Bdoc): Funit = {
|
||||
val streamerId = s string "_id" err "missing id"
|
||||
val picPath = s string "picturePath" err "missing picturePath"
|
||||
val picId = picPath.split('/').headOption err "missing picId"
|
||||
imageRepo.fetch(picId) flatMap {
|
||||
case None =>
|
||||
println(s"Missing picture for streamer $streamerId pic $picPath")
|
||||
funit
|
||||
case Some(pic) =>
|
||||
val part: MultipartFormData.FilePart[Source[ByteString, _]] = MultipartFormData.FilePart(
|
||||
key = "data",
|
||||
filename = pic.name,
|
||||
contentType = "image/jpeg".some,
|
||||
ref = Source(ByteString.fromArray(pic.data) :: Nil),
|
||||
fileSize = pic.size
|
||||
)
|
||||
picfitApi.uploadSource(s"streamer:$streamerId", part, streamerId, monitor = false) flatMap {
|
||||
picture =>
|
||||
coll.updateField($id(streamerId), "picture", picture.id.value).void
|
||||
}
|
||||
} recover { case e: Exception =>
|
||||
if (!e.getMessage.contains("Invalid file type: "))
|
||||
logger.error(s"Can't migrate streamer $streamerId", e)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue