Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor improvements to profile image upload #1229

Merged
merged 1 commit into from
Jan 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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