[FEAT] add capability to select the frmat of thumbnail generate
This commit is contained in:
parent
05a51ad87a
commit
401bd8318a
@ -157,13 +157,8 @@ public class DataResource {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Data createNewData(final long tmpUID, final String originalFileName, final String sha512)
|
protected String getMimeType(final String extension) throws IOException {
|
||||||
throws IOException {
|
return switch (extension.toLowerCase()) {
|
||||||
// determine mime type:
|
|
||||||
Data injectedData = new Data();
|
|
||||||
String mimeType = "";
|
|
||||||
final String extension = originalFileName.substring(originalFileName.lastIndexOf('.') + 1);
|
|
||||||
mimeType = switch (extension.toLowerCase()) {
|
|
||||||
case "jpg", "jpeg" -> "image/jpeg";
|
case "jpg", "jpeg" -> "image/jpeg";
|
||||||
case "png" -> "image/png";
|
case "png" -> "image/png";
|
||||||
case "webp" -> "image/webp";
|
case "webp" -> "image/webp";
|
||||||
@ -172,6 +167,15 @@ public class DataResource {
|
|||||||
case "webm" -> "video/webm";
|
case "webm" -> "video/webm";
|
||||||
default -> throw new IOException("Can not find the mime type of data input: '" + extension + "'");
|
default -> throw new IOException("Can not find the mime type of data input: '" + extension + "'");
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public Data createNewData(final long tmpUID, final String originalFileName, final String sha512)
|
||||||
|
throws IOException {
|
||||||
|
// determine mime type:
|
||||||
|
Data injectedData = new Data();
|
||||||
|
String mimeType = "";
|
||||||
|
final String extension = originalFileName.substring(originalFileName.lastIndexOf('.') + 1);
|
||||||
|
mimeType = getMimeType(extension);
|
||||||
injectedData.mimeType = mimeType;
|
injectedData.mimeType = mimeType;
|
||||||
injectedData.sha512 = sha512;
|
injectedData.sha512 = sha512;
|
||||||
final String tmpPath = getTmpFileInData(tmpUID);
|
final String tmpPath = getTmpFileInData(tmpUID);
|
||||||
@ -369,10 +373,9 @@ public class DataResource {
|
|||||||
}
|
}
|
||||||
LOGGER.info("input size image: {}x{} type={}", inputImage.getWidth(), inputImage.getHeight(),
|
LOGGER.info("input size image: {}x{} type={}", inputImage.getWidth(), inputImage.getHeight(),
|
||||||
inputImage.getType());
|
inputImage.getType());
|
||||||
final int scaledWidth = 250;
|
final int scaledWidth = ConfigBaseVariable.getThumbnailWidth();
|
||||||
final int scaledHeight = (int) ((float) inputImage.getHeight() / (float) inputImage.getWidth()
|
final int scaledHeight = (int) ((float) inputImage.getHeight() / (float) inputImage.getWidth()
|
||||||
* scaledWidth);
|
* scaledWidth);
|
||||||
|
|
||||||
// creates output image
|
// creates output image
|
||||||
final BufferedImage outputImage = new BufferedImage(scaledWidth, scaledHeight, inputImage.getType());
|
final BufferedImage outputImage = new BufferedImage(scaledWidth, scaledHeight, inputImage.getType());
|
||||||
|
|
||||||
@ -381,16 +384,10 @@ public class DataResource {
|
|||||||
LOGGER.info("output size image: {}x{}", scaledWidth, scaledHeight);
|
LOGGER.info("output size image: {}x{}", scaledWidth, scaledHeight);
|
||||||
g2d.drawImage(inputImage, 0, 0, scaledWidth, scaledHeight, null);
|
g2d.drawImage(inputImage, 0, 0, scaledWidth, scaledHeight, null);
|
||||||
g2d.dispose();
|
g2d.dispose();
|
||||||
for (final String data : ImageIO.getWriterFormatNames()) {
|
|
||||||
LOGGER.info("availlable format: {}", data);
|
|
||||||
}
|
|
||||||
// create the output stream:
|
// create the output stream:
|
||||||
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
try {
|
try {
|
||||||
// TODO: check how to remove buffer file !!! here, it is not needed at all...
|
ImageIO.write(outputImage, ConfigBaseVariable.getThumbnailFormat(), baos);
|
||||||
//ImageIO.write(outputImage, "JPEG", baos);
|
|
||||||
//ImageIO.write(outputImage, "png", baos);
|
|
||||||
ImageIO.write(outputImage, "WebP", baos);
|
|
||||||
} catch (final IOException e) {
|
} catch (final IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return Response.status(500).entity("Internal Error: resize fail: " + e.getMessage()).type("text/plain")
|
return Response.status(500).entity("Internal Error: resize fail: " + e.getMessage()).type("text/plain")
|
||||||
@ -398,12 +395,20 @@ public class DataResource {
|
|||||||
}
|
}
|
||||||
final byte[] imageData = baos.toByteArray();
|
final byte[] imageData = baos.toByteArray();
|
||||||
LOGGER.info("output length {}", imageData.length);
|
LOGGER.info("output length {}", imageData.length);
|
||||||
// Response.ok(new ByteArrayInputStream(imageData)).build();
|
if (imageData.length == 0) {
|
||||||
|
LOGGER.error("Fail to convert image... Availlable format:");
|
||||||
|
for (final String data : ImageIO.getWriterFormatNames()) {
|
||||||
|
LOGGER.error(" - {}", data);
|
||||||
|
}
|
||||||
|
}
|
||||||
final Response.ResponseBuilder out = Response.ok(imageData).header(HttpHeaders.CONTENT_LENGTH,
|
final Response.ResponseBuilder out = Response.ok(imageData).header(HttpHeaders.CONTENT_LENGTH,
|
||||||
imageData.length);
|
imageData.length);
|
||||||
//out.type("image/jpeg");
|
try {
|
||||||
out.type("image/webp");
|
out.type(getMimeType(ConfigBaseVariable.getThumbnailFormat()));
|
||||||
//out.type("image/png");
|
} catch (final IOException ex) {
|
||||||
|
throw new FailException(Response.Status.INTERNAL_SERVER_ERROR,
|
||||||
|
"Fail to convert mime type of " + ConfigBaseVariable.getThumbnailFormat(), ex);
|
||||||
|
}
|
||||||
// TODO: move this in a decorator !!!
|
// TODO: move this in a decorator !!!
|
||||||
final CacheControl cc = new CacheControl();
|
final CacheControl cc = new CacheControl();
|
||||||
cc.setMaxAge(3600);
|
cc.setMaxAge(3600);
|
||||||
|
@ -18,6 +18,8 @@ public class ConfigBaseVariable {
|
|||||||
static public String eMailFrom;
|
static public String eMailFrom;
|
||||||
static public String eMailLogin;
|
static public String eMailLogin;
|
||||||
static public String eMailPassword;
|
static public String eMailPassword;
|
||||||
|
static public String thumbnailFormat;
|
||||||
|
static public String thumbnailWidth;
|
||||||
static public Class<?>[] dbInterfacesClasses;
|
static public Class<?>[] dbInterfacesClasses;
|
||||||
|
|
||||||
// For test only
|
// For test only
|
||||||
@ -39,6 +41,8 @@ public class ConfigBaseVariable {
|
|||||||
eMailFrom = System.getenv("EMAIL_FROM");
|
eMailFrom = System.getenv("EMAIL_FROM");
|
||||||
eMailLogin = System.getenv("EMAIL_LOGIN");
|
eMailLogin = System.getenv("EMAIL_LOGIN");
|
||||||
eMailPassword = System.getenv("EMAIL_PASSWORD");
|
eMailPassword = System.getenv("EMAIL_PASSWORD");
|
||||||
|
thumbnailFormat = System.getenv("THUMBNAIL_FORMAT");
|
||||||
|
thumbnailWidth = System.getenv("THUMBNAIL_WIDTH");
|
||||||
dbInterfacesClasses = new Class<?>[0];
|
dbInterfacesClasses = new Class<?>[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -160,4 +164,18 @@ public class ConfigBaseVariable {
|
|||||||
public static void setBbInterfacesClasses(final Class<?>[] data) {
|
public static void setBbInterfacesClasses(final Class<?>[] data) {
|
||||||
dbInterfacesClasses = data;
|
dbInterfacesClasses = data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String getThumbnailFormat() {
|
||||||
|
if (thumbnailFormat == null || thumbnailFormat.isEmpty()) {
|
||||||
|
return "png";
|
||||||
|
}
|
||||||
|
return thumbnailFormat;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int getThumbnailWidth() {
|
||||||
|
if (thumbnailWidth == null || thumbnailWidth.isEmpty()) {
|
||||||
|
return 256;
|
||||||
|
}
|
||||||
|
return Integer.parseInt(thumbnailWidth);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user