preprocess coach image on server side

This commit is contained in:
Thibault Duplessis 2016-08-27 10:59:45 +02:00
parent 5b19f6cd76
commit c6597d93db
6 changed files with 58 additions and 32 deletions

View file

@ -7,7 +7,7 @@ import lila.user.{ User, UserRepo }
final class CoachApi(
coll: Coll,
imageColl: Coll) {
photographer: Photographer) {
import BsonHandlers._
@ -50,18 +50,9 @@ final class CoachApi(
private val pictureMaxBytes = pictureMaxMb * 1024 * 1024
private def pictureId(id: Coach.Id) = s"coach:${id.value}:picture"
def uploadPicture(
c: Coach.WithUser,
picture: play.api.mvc.MultipartFormData.FilePart[play.api.libs.Files.TemporaryFile]): Funit =
if (picture.ref.file.length > pictureMaxBytes) fufail(s"File size must not exceed ${pictureMaxMb}MB.")
else {
val image = lila.db.DbImage.make(
id = pictureId(c.coach.id),
name = picture.filename,
contentType = picture.contentType,
file = picture.ref.file)
imageColl.update($id(image.id), image, upsert = true) >>
coll.update($id(c.coach.id), $set("picturePath" -> image.path))
def uploadPicture(c: Coach.WithUser, picture: Photographer.Uploaded): Funit =
photographer(c.coach.id, picture) flatMap { pic =>
coll.update($id(c.coach.id), $set("picturePath" -> pic.path))
} void
def deletePicture(c: Coach.WithUser): Funit =

View file

@ -16,9 +16,11 @@ final class Env(
private lazy val coachColl = db(CollectionCoach)
private lazy val imageColl = db(CollectionImage)
private lazy val photographer = new Photographer(imageColl)
lazy val api = new CoachApi(
coll = coachColl,
imageColl = imageColl)
photographer = photographer)
def cli = new lila.common.Cli {
def process = {

View file

@ -0,0 +1,43 @@
package lila.coach
import java.io.File
import lila.db.DbImage
import lila.db.dsl._
private final class Photographer(coll: Coll) {
private val uploadedMaxMb = 3
private val uploadedMaxBytes = uploadedMaxMb * 1024 * 1024
private def pictureId(id: Coach.Id) = s"coach:${id.value}:picture"
def apply(coachId: Coach.Id, uploaded: Photographer.Uploaded): Fu[DbImage] =
if (uploaded.ref.file.length > uploadedMaxBytes)
fufail(s"File size must not exceed ${uploadedMaxMb}MB.")
else {
process(uploaded.ref.file)
val image = DbImage.make(
id = pictureId(coachId),
name = uploaded.filename,
contentType = uploaded.contentType,
file = uploaded.ref.file)
coll.update($id(image.id), image, upsert = true) inject image
}
private def process(file: File) = {
import com.sksamuel.scrimage._
Image.fromFile(file)
.cover(600, 600)
.output(file)
}
}
private object Photographer {
type Uploaded = play.api.mvc.MultipartFormData.FilePart[play.api.libs.Files.TemporaryFile]
}

View file

@ -35,7 +35,7 @@ object ApplicationBuild extends Build {
scalaz, scalalib, hasher, config, apache,
jgit, findbugs, RM, akka.actor, akka.slf4j,
spray.caching, maxmind, prismic,
kamon.core, kamon.statsd, java8compat, semver),
kamon.core, kamon.statsd, java8compat, semver, scrimage),
TwirlKeys.templateImports ++= Seq(
"lila.game.{ Game, Player, Pov }",
"lila.tournament.Tournament",
@ -91,7 +91,7 @@ object ApplicationBuild extends Build {
lazy val coach = project("coach", Seq(
common, hub, db, user)).settings(
libraryDependencies ++= provided(play.api, RM)
libraryDependencies ++= provided(play.api, RM, scrimage)
)
lazy val coordinate = project("coordinate", Seq(common, db)).settings(

View file

@ -39,6 +39,7 @@ object Dependencies {
val prismic = "io.prismic" %% "scala-kit" % "1.2.11-THIB"
val java8compat = "org.scala-lang.modules" %% "scala-java8-compat" % "0.7.0"
val semver = "com.gilt" %% "gfc-semver" % "0.0.3"
val scrimage = "com.sksamuel.scrimage" %% "scrimage-core" % "2.1.7"
object play {
val version = "2.4.6"

View file

@ -38,24 +38,13 @@
}
.thumbnail {
position: relative;
display: block;
width: 200px;
height: 200px;
overflow: hidden;
text-align: center;
}
.thumbnail img {
width: 300px;
height: 300px;
margin: auto;
border-radius: 12px;
border: 7px solid #ddd;
}
.thumbnail img {
position: absolute;
left: 50%;
top: 50%;
height: 100%;
width: auto;
transform: translate(-50%,-50%);
}
.thumbnail img.portrait {
width: 100%;
height: auto;
}