Skip to content

Commit

Permalink
Merge pull request EHRI#1229 from mikesname/image_size_error
Browse files Browse the repository at this point in the history
Minor improvements to profile image upload
  • Loading branch information
mikesname committed Jan 16, 2020
2 parents 428c5b2 + e42c4f6 commit fd52ec7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion conf/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ ehri {
}

profile {
maxImageSize: 5000000 // bytes
maxImageSize: 5MB
}
}

Expand Down
15 changes: 10 additions & 5 deletions modules/portal/app/controllers/portal/users/UserProfiles.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package controllers.portal.users

import java.io.File
import javax.inject._

import javax.inject._
import akka.stream.Materializer
import controllers.generic.Search
import controllers.portal.base.PortalController
Expand All @@ -11,6 +11,7 @@ import models._
import models.base.Model
import net.coobird.thumbnailator.Thumbnails
import net.coobird.thumbnailator.tasks.UnsupportedFormatException
import play.api.Logger
import play.api.http.{HeaderNames, MimeTypes}
import play.api.i18n.Messages
import play.api.libs.Files.TemporaryFile
Expand All @@ -37,6 +38,7 @@ case class UserProfiles @Inject()(
with CsvHelpers {

private implicit val mat: Materializer = appComponents.materializer
private val logger = Logger(classOf[UserProfiles])

private val profileRoutes = controllers.portal.users.routes.UserProfiles

Expand Down Expand Up @@ -281,17 +283,20 @@ case class UserProfiles @Inject()(
// Defer to the standard profile update page...
def updateProfileImage(): Action[AnyContent] = updateProfile()

// Body parser that'll refuse anything larger than 5MB
// Body parser that'll refuse anything larger than maxImageSize
private def uploadParser = parsers.maxLength(
config.get[Int]("ehri.portal.profile.maxImageSize"), parsers.multipartFormData)
config.underlying.getBytes("ehri.portal.profile.maxImageSize"), parsers.multipartFormData)

def updateProfileImagePost(): Action[Either[MaxSizeExceeded, MultipartFormData[TemporaryFile]]] = WithUserAction.async(uploadParser) { implicit request =>

def onError(err: String, status: Status = BadRequest): Future[Result] = immediate(
status(views.html.userProfile.editProfile(profileDataForm,
imageForm.withGlobalError(err), accountPrefsForm)))

request.body match {
case Left(MaxSizeExceeded(length)) => onError("errors.imageTooLarge", EntityTooLarge)
case Left(MaxSizeExceeded(size)) =>
logger.debug(s"Profile image upload size too large: $size")
onError("errors.imageTooLarge", EntityTooLarge)
case Right(multipartForm) => multipartForm.file("image").map { file =>
if (isValidContentType(file)) {
try {
Expand All @@ -301,7 +306,7 @@ case class UserProfiles @Inject()(
} yield Redirect(profileRoutes.profile())
.flashing("success" -> "profile.update.confirmation")
} catch {
case e: UnsupportedFormatException => onError("errors.badFileType")
case _: UnsupportedFormatException => onError("errors.badFileType")
}
} else {
onError("errors.badFileType")
Expand Down
2 changes: 1 addition & 1 deletion modules/portal/conf/messages
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ errors.permissionDenied.explanation=It looks like you can''t do whatever it is y
to do. This is probably an error on our part. We''ll try and fix that ASAP.
errors.errorDetails=Details
errors.noFurtherInfo=No additional information is available.
errors.imageTooLarge=This image was too large. Please select one smaller than 5 metabytes in size.
errors.imageTooLarge=This image was too large. Please select one smaller than 5 megabytes in size.
errors.badFileType=The uploaded file was not a supported image type.
errors.noFileGiven=No image file was selected.

Expand Down

0 comments on commit fd52ec7

Please sign in to comment.