Skip to content

Instantly share code, notes, and snippets.

@MrSnyder
Last active July 8, 2020 10:25
Show Gist options
  • Save MrSnyder/bef13cdd79a8e6bfb77d3d0d01c70e6e to your computer and use it in GitHub Desktop.
Save MrSnyder/bef13cdd79a8e6bfb77d3d0d01c70e6e to your computer and use it in GitHub Desktop.
Live Coding Cheatsheet

Live-Coding Cheatsheet

Image-Handling JPA+Upload

  • Explain ImageController
  • Move #loadImage from ImageController to BookController
  • Book entity: add LOB field
  • Insert example via LOAD_FILE
  • book/edit.html: Add input to form + change to multipart
  • Controller anpassen (loadImage)
  • MultipartFileToByteArrayConverter + WebConfig
  • application.properties: Adapt file POST limits
  • JpegValidator
SHOW VARIABLES WHERE variable_name='secure_file_priv';
UPDATE book SET image=LOAD_FILE('/var/lib/mysql-files/Refactoring.jpeg');
# File Upload limits
spring.servlet.multipart.max-file-size=1024KB
spring.servlet.multipart.max-request-size=1024KB
@Documented
@Constraint(validatedBy = JPEGValidator.class)
@Target( { ElementType.PARAMETER, ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
public @interface JPEGConstraint {
    String message() default "Bild hat kein JPEG-Format!";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
}

public class JPEGValidator implements
ConstraintValidator<JPEGConstraint, byte[]> {

	private ImageService imageService;
	
	@Autowired
	public void setImageService(ImageService imageService) {
		this.imageService = imageService;
	}

	@Override
	public void initialize(JPEGConstraint image) {
	}

	@Override
	public boolean isValid(byte[] imageArray,
			ConstraintValidatorContext validatorContext) {
		
		// initialize result variable
		boolean valid = false;
		
		// if image was passed
		if (imageArray.length > 0 ) {

			valid = imageService.getImageType(imageArray) == "JPEG";
		
		} else {
			valid = true;
		}
				
		return valid;

	}

}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment