Skip to content

Commit 38ef8c4

Browse files
committed
Refactor conversion logic to use mime type scoped value
1 parent 888a05b commit 38ef8c4

File tree

2 files changed

+38
-27
lines changed

2 files changed

+38
-27
lines changed

src/main/java/com/github/stickerifier/stickerify/bot/Stickerify.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@
1010
import static java.util.HashSet.newHashSet;
1111
import static java.util.concurrent.Executors.newThreadPerTaskExecutor;
1212

13-
import com.github.stickerifier.stickerify.exception.BaseException;
1413
import com.github.stickerifier.stickerify.exception.CorruptedFileException;
1514
import com.github.stickerifier.stickerify.exception.FileOperationException;
16-
import com.github.stickerifier.stickerify.exception.MediaException;
1715
import com.github.stickerifier.stickerify.exception.TelegramApiException;
1816
import com.github.stickerifier.stickerify.logger.StructuredLogger;
1917
import com.github.stickerifier.stickerify.media.MediaHelper;
@@ -148,10 +146,10 @@ private void answerFile(TelegramRequest request, String fileId) {
148146

149147
execute(answerWithFile);
150148
}
151-
} catch (TelegramApiException | MediaException e) {
152-
processFailure(request, e, fileId);
153149
} catch (InterruptedException e) {
154150
Thread.currentThread().interrupt();
151+
} catch (Exception e) {
152+
processFailure(request, e, fileId);
155153
} finally {
156154
deleteTempFiles(pathsToDelete);
157155
}
@@ -171,7 +169,7 @@ private File retrieveFile(String fileId) throws TelegramApiException, FileOperat
171169
}
172170
}
173171

174-
private void processFailure(TelegramRequest request, BaseException e, String fileId) {
172+
private void processFailure(TelegramRequest request, Exception e, String fileId) {
175173
if (e instanceof TelegramApiException telegramException) {
176174
processTelegramFailure(telegramException, false);
177175
}

src/main/java/com/github/stickerifier/stickerify/media/MediaHelper.java

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.stickerifier.stickerify.media;
22

3+
import static com.github.stickerifier.stickerify.logger.StructuredLogger.MIME_TYPE;
34
import static com.github.stickerifier.stickerify.media.MediaConstraints.MATROSKA_FORMAT;
45
import static com.github.stickerifier.stickerify.media.MediaConstraints.MAX_ANIMATION_DURATION_SECONDS;
56
import static com.github.stickerifier.stickerify.media.MediaConstraints.MAX_ANIMATION_FILE_SIZE;
@@ -58,13 +59,44 @@ public final class MediaHelper {
5859
* Based on the type of passed-in file, it converts it into the proper media.
5960
* If no conversion was needed, {@code null} is returned.
6061
*
62+
* @param inputFile the file to convert
63+
* @return a resized and converted file
64+
* @throws Exception either if the file is not supported, if the conversion failed,
65+
* or if the current thread is interrupted while converting a video file
66+
*/
67+
public static @Nullable File convert(File inputFile) throws Exception {
68+
var mimeType = detectMimeType(inputFile);
69+
return ScopedValue.where(MIME_TYPE, mimeType).call(() -> performConversion(inputFile));
70+
}
71+
72+
/**
73+
* Analyzes the file to detect its media type.
74+
*
75+
* @param file the file sent to the bot
76+
* @return the MIME type of the passed-in file
77+
* @throws MediaException if the file could not be read
78+
*/
79+
private static String detectMimeType(File file) throws MediaException {
80+
try {
81+
var mimeType = TIKA.detect(file);
82+
LOGGER.at(Level.DEBUG).log("MIME type successfully detected");
83+
84+
return mimeType;
85+
} catch (IOException e) {
86+
LOGGER.at(Level.ERROR).addKeyValue("file_name", file.getName()).log("Unable to retrieve MIME type");
87+
throw new MediaException(e);
88+
}
89+
}
90+
91+
/**
6192
* @param inputFile the file to convert
6293
* @return a resized and converted file
6394
* @throws MediaException if the file is not supported or if the conversion failed
6495
* @throws InterruptedException if the current thread is interrupted while converting a video file
96+
* @see MediaHelper#convert(File)
6597
*/
66-
public static @Nullable File convert(File inputFile) throws MediaException, InterruptedException {
67-
var mimeType = detectMimeType(inputFile);
98+
private static @Nullable File performConversion(File inputFile) throws MediaException, InterruptedException {
99+
var mimeType = MIME_TYPE.get();
68100

69101
try {
70102
if (isSupportedVideo(mimeType)) {
@@ -90,32 +122,13 @@ public final class MediaHelper {
90122
return convertToWebp(inputFile);
91123
}
92124
} catch (MediaException e) {
93-
LOGGER.at(Level.WARN).setCause(e).addKeyValue("mime_type", mimeType).log("The file with {} MIME type could not be converted", mimeType);
125+
LOGGER.at(Level.WARN).setCause(e).log("The file could not be converted");
94126
throw e;
95127
}
96128

97129
throw new MediaException("The file with {} MIME type is not supported", mimeType);
98130
}
99131

100-
/**
101-
* Analyzes the file to detect its media type.
102-
*
103-
* @param file the file sent to the bot
104-
* @return the MIME type of the passed-in file
105-
* @throws MediaException if the file could not be read
106-
*/
107-
private static String detectMimeType(File file) throws MediaException {
108-
try {
109-
var mimeType = TIKA.detect(file);
110-
LOGGER.at(Level.DEBUG).addKeyValue("mime_type", mimeType).log("MIME type successfully detected");
111-
112-
return mimeType;
113-
} catch (IOException e) {
114-
LOGGER.at(Level.ERROR).addKeyValue("file_name", file.getName()).log("Unable to retrieve MIME type");
115-
throw new MediaException(e);
116-
}
117-
}
118-
119132
/**
120133
* Checks if the MIME type corresponds to one of the supported video formats.
121134
*

0 commit comments

Comments
 (0)