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

1077 Set 4MB size limit for images #1605

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
4 changes: 3 additions & 1 deletion src/main/java/ai/elimu/util/ImageHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
public class ImageHelper {

public static final int MINIMUM_WIDTH = 640;


public static final int MAX_MB = 4194304; // 4MB
tomaszsmy marked this conversation as resolved.
Show resolved Hide resolved

private static Logger logger = LogManager.getLogger();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.ByteArrayMultipartFileEditor;

import static ai.elimu.util.ImageHelper.MAX_MB;
tomaszsmy marked this conversation as resolved.
Show resolved Hide resolved

@Controller
@RequestMapping("/content/multimedia/image/create")
public class ImageCreateController {
Expand Down Expand Up @@ -95,7 +97,7 @@ public String handleSubmit(
} else {
String originalFileName = multipartFile.getOriginalFilename();
logger.info("originalFileName: " + originalFileName);

byte[] headerBytes = Arrays.copyOfRange(bytes, 0, 6);
byte[] gifHeader87a = {71, 73, 70, 56, 55, 97}; // "GIF87a"
byte[] gifHeader89a = {71, 73, 70, 56, 57, 97}; // "GIF89a"
Expand Down Expand Up @@ -133,6 +135,11 @@ public String handleSubmit(
}
}
}

if (bytes.length > MAX_MB) {
tomaszsmy marked this conversation as resolved.
Show resolved Hide resolved
result.rejectValue("bytes", "image.too.big");
tomaszsmy marked this conversation as resolved.
Show resolved Hide resolved
}

tomaszsmy marked this conversation as resolved.
Show resolved Hide resolved
}
} catch (IOException e) {
logger.error(e);
Expand Down
1 change: 1 addition & 0 deletions src/main/webapp/WEB-INF/i18n/errors_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ typeMismatch.thumbnail=Valid thumbnail format: PNG
typeMismatch.java.lang.Integer={0} must be a number
formatHint.Integer=Number (e.g. "5000")
image.too.small=The image width must be at least 640px
image.too.big=The image size is too big
tomaszsmy marked this conversation as resolved.
Show resolved Hide resolved
emoji.unicode.version=Only emojis up to Unicode version 9
WordSpace=Spaces are not allowed