Skip to content

Commit

Permalink
chore: optimize code style
Browse files Browse the repository at this point in the history
  • Loading branch information
guqing committed Sep 6, 2024
1 parent f408112 commit 36bb93e
Showing 1 changed file with 5 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,15 @@ private void generateThumbnail(Path tempImagePath) throws IOException {
if (file.length() > MAX_FILE_SIZE) {
throw new IOException("File size exceeds the limit: " + MAX_FILE_SIZE);
}
var formatNameOpt = getFormatName(file);
var formatName = formatNameOpt.orElse("jpg");
String formatName = getFormatName(file)
.orElseThrow(() -> new UnsupportedOperationException("Unknown format"));
if (isUnsupportedFormat(formatName)) {
throwUnsupportedException(formatName);
throw new UnsupportedOperationException("Unsupported image format for: " + formatName);
}

var img = ImageIO.read(file);
if (img == null) {
throwUnsupportedException(formatNameOpt.orElse("unknown"));
throw new UnsupportedOperationException("Cannot read image file: " + file);
}
var thumbnailFile = getThumbnailFile(formatName);
if (img.getWidth() <= size.getWidth()) {
Expand All @@ -93,10 +94,6 @@ private void generateThumbnail(Path tempImagePath) throws IOException {
ImageIO.write(thumbnail, formatName, thumbnailFile);
}

private static void throwUnsupportedException(String formatName) {
throw new UnsupportedOperationException("Unsupported image format for: " + formatName);
}

private static boolean isUnsupportedFormat(@NonNull String formatName) {
return UNSUPPORTED_FORMATS.contains(formatName.toLowerCase());
}
Expand Down

0 comments on commit 36bb93e

Please sign in to comment.