Add upload limit size constraints and upload progress infratructure

Bug #475446 reported by dunsun
12
This bug affects 2 people
Affects Status Importance Assigned to Milestone
play framework
Fix Committed
Undecided
Unassigned
1.0
Won't Fix
Undecided
Unassigned
1.1
Fix Committed
Undecided
Unassigned

Bug Description

It is very useful and for some projects necessary to be able to define different upload size constraints for different upload artefacts.

It should be possible to define different constraints in application.conf:
upload.limit.my_images = 5MB
upload.limit.clips = 20MB
upload.limit.movies = 500MB

More over Play should contain some upload progress API .

Changed in play:
status: New → Confirmed
Revision history for this message
Stephane Epardaud (stef-inforealm) wrote :

I would say those two things are different (limit and progress), and that the upload progress API is critical to any application that receives files. I would also say that the upload progress API should have a way to cancel the upload.

Revision history for this message
dirk (australiandeveloper) wrote :

I created a new validation annotation for checking the size of a file that may be useful. It is used like this:

{{{#!java
    public static void updatePicture(Long userId,
            @FileMaxSize(Config.PROFILE_MAX_SIZE_BYTES) File picture) {
        ...
    }
}}}

The code for the annotation and supporting check class is:

{{{#!java
package play.data.validation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import net.sf.oval.configuration.annotation.Constraint;

/**
 * This field size must be lower than.
 * Message key: validation.maxSize
 * $1: field name
 * $2: reference value
 */
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.PARAMETER})
@Constraint(checkWith = MaxSizeCheck.class)
public @interface MaxSize {

    String message() default MaxSizeCheck.mes;
    int value();
}

package play.data.validation;

import java.util.HashMap;
import java.util.Map;
import net.sf.oval.Validator;
import net.sf.oval.configuration.annotation.AbstractAnnotationCheck;
import net.sf.oval.context.OValContext;

public class MaxSizeCheck extends AbstractAnnotationCheck<MaxSize> {

    final static String mes = "validation.maxSize";

    int maxSize;

    @Override
    public void configure(MaxSize annotation) {
        this.maxSize = annotation.value();
        setMessage(annotation.message());
    }

    public boolean isSatisfied(Object validatedObject, Object value, OValContext context, Validator validator) {
        requireMessageVariablesRecreation();
        if (value == null || value.toString().length() == 0) {
            return true;
        }
        return value.toString().length() <= maxSize;
    }

    @Override
    public Map<String, String> createMessageVariables() {
        Map<String, String> messageVariables = new HashMap();
        messageVariables.put("maxSize", Integer.toString(maxSize));
        return messageVariables;
    }

}
}}}

Revision history for this message
Guillaume Bort (guillaume-bort) wrote :

It has to be handled by the reverse proxy. There is no correct way to check that in the play application because the reverse proxy will load all the request content itself before contacting the play application.

Since it is a 'production' problem, and that in production environment you should run behind a reverse proxy, it's a bad idea to try to solve it in play.

Changed in play:
status: Confirmed → Fix Committed
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.