Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8841787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:48:25+00:00 2026-06-14T10:48:25+00:00

I am using the HDImageService plugin for grails to do the heavy lifting for

  • 0

I am using the HDImageService plugin for grails to do the heavy lifting for scaling my images that users upload. I have created ImageService.groovy to save to my Amazon S3 bucket. Everything works fine, user chooses file, clicks publish and wahla the image is scaled, stored and displayed. My problem is that I do not know how to restrict the user from uploading files other than images. I would like to only allow files of type jpeg, jpg, gif ,or png to be uploaded. I have created an ENUM class with these variables, but I do not know where or how to implement. Can anyone point me in the right direction


RegistrationController.groovy: Get File and Save to Bucket

         if ( params.photo ) {
            MultipartFile file = request.getFile( 'photo' )
            byte[] fileBytes = file.bytes
            ByteArrayInputStream bais = new ByteArrayInputStream( fileBytes )
            BufferedImage image = ImageIO.read( bais )
            def width = image.width
            def height = image.height
            def maxWidth = 500

            // Calculate the ratio that we will need to resize the image
            double ratio = 1.0f
            if ( width > maxWidth ) {
                def r = maxWidth / width
                ratio = r < 1.0f ? r : 1.0f
                bais.reset()
                fileBytes = hdImageService.scale( bais, maxWidth, Math.round( height * ratio ) as int )
            }

            geolink.custPhoto = imageService.storeS3Image(
                imageService.buildPPJPhotoPath( geolink, file.getOriginalFilename() ),
                fileBytes,
                file.contentType
            )
        }

ImageService.groovy: Enum

    String getFormatName( byte[] raw ) {
    try {
        // Create an image input stream on the image
        ImageInputStream iis = ImageIO.createImageInputStream( new ByteArrayInputStream( raw ) )

        // Find all image readers that recognize the image format
        Iterator iter = ImageIO.getImageReaders(iis)
        if (!iter.hasNext()) {
            // No readers found
            log.debug( "Unable to get format" )
            return null;
        }

        // Use the first reader
        ImageReader reader = (ImageReader)iter.next()

        // Close stream
        iis.close()

        // Return the format name
        log.debug( "Format: ${reader.getFormatName() }" )
        return reader.getFormatName()
    }
    catch (IOException e) {
        log.warn( "Unable to determine image format", e )
    }
    // The image could not be read
    return null;
}

ImageFormat getContentType( String filename ) {
    String[] parts = filename.split( '\\.' )
    return ImageFormat.valueOf( parts[parts.length - 1].toUpperCase() )
}}

    public enum ImageFormat {
JPEG( 'image/jpeg', 'jpg' ),
JPG( 'image/jpeg', 'jpg' ),
PNG( 'image/png', 'png' ),
GIF( 'image/gif', 'gif' )

String mimeType
String extension

public ImageFormat( String mime, String ext ) {
    this.mimeType = mime
    this.extension = ext
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T10:48:26+00:00Added an answer on June 14, 2026 at 10:48 am

    Here’s how I do it. It should be pretty easy to modify this into an Enum:

    def downloadedFile = request.getFile('imageFile')
    def okContentTypes = ['image/png', 'image/jpeg', 'image/jpg', 'image/gif']
    
    if (!okContentTypes.contains(downloadedFile.getContentType())) {
        myDomainInstance.errors.rejectValue("image", "Image type must be one of: ${okContentTypes}")
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using EF Code First I have an model object that has multiple properties that
Using SSRS 2008 R2 I have a background process that dynamically generates RDL for
Using TortoiseSVN against VisualSVN I delete a source file that I should not have
Using CRM 4, I have an entity form that contains a tab with an
using this http://bl.ocks.org/950642 we can see how to add images to nodes, the question
Using MVC2 I have an AJAX form which is posting to a bound model.
Using Core Data, I have a fetch request to fetch the minimum of a
Using version 2.5.3 of ExpressionEngine, I have a list of products displayed by category,
Using Java,I have to fetch multiple sets of values from an XML file to
Using MVVM. I have a DataTemplate which I am using to display an expander

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.