Commit 0eb9ef24 authored by Martin Lowe's avatar Martin Lowe 🇨🇦
Browse files

Add FileTooLarge exception and handling

parent 0efa2385
/*
* Copyright (C) 2019 Eclipse Foundation and others.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipsefoundation.react.resources.mapper;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
import org.eclipsefoundation.core.model.Error;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.undertow.server.handlers.form.MultiPartParserDefinition.FileTooLargeException;
/**
*
* @author Martin Lowe
*/
@Provider
public class FileTooLargeExceptionMapper implements ExceptionMapper<FileTooLargeException> {
private static final Logger LOGGER = LoggerFactory.getLogger(FileTooLargeExceptionMapper.class);
@Override
public Response toResponse(FileTooLargeException exception) {
LOGGER.error(exception.getMessage(), exception);
return new Error(Status.REQUEST_ENTITY_TOO_LARGE, "Could not process the given request: " + exception.getMessage()).asResponse();
}
}
......@@ -37,7 +37,6 @@ import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import javax.inject.Inject;
import javax.ws.rs.BadRequestException;
import javax.ws.rs.ServerErrorException;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
......@@ -59,6 +58,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.quarkus.runtime.Startup;
import io.undertow.server.handlers.form.MultiPartParserDefinition.FileTooLargeException;
/**
* Default implementation of the image service. Writes images to a file system location then provides a web-mounted path
......@@ -156,14 +156,14 @@ public class DefaultImageStoreService implements ImageStoreService {
}
if (format.isPresent() && format.get().equals(ImageStoreFormats.PRINT)) {
if (bytes.length > config.maxSizeInBytes().print()) {
throw new BadRequestException("Passed image is larger than allowed size of '"
throw new FileTooLargeException("Passed image is larger than allowed size of '"
+ config.maxSizeInBytes().print() + "' bytes");
}
// Any print files should not attempt compression (as they are meant to be big)
return getWebUrl(Files.write(p, bytes));
} else {
if (bytes.length > config.maxSizeInBytes().web()) {
throw new BadRequestException("Passed image is larger than allowed size of '"
throw new FileTooLargeException("Passed image is larger than allowed size of '"
+ config.maxSizeInBytes().web() + "' bytes");
}
// compress image and then compare max size
......@@ -171,8 +171,8 @@ public class DefaultImageStoreService implements ImageStoreService {
if (compressedImage.length > config.maxSizeInBytes().webPostCompression()
&& (!Files.exists(p) || !approximatelyMatch((long) compressedImage.length,
attrView.readAttributes().size(), 1000))) {
throw new BadRequestException("Passed image is larger than allowed size of '"
+ config.maxSizeInBytes().webPostCompression() + "' bytes");
throw new FileTooLargeException("Passed image is larger than allowed size of '"
+ config.maxSizeInBytes().webPostCompression() + "' bytes after compression");
}
// if enabled, update the EclipseDB on logo update when image name is numeric
// max size check is related to max blob size of 64kb
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment