11package com .github .stickerifier .stickerify .media ;
22
3+ import static com .github .stickerifier .stickerify .logger .StructuredLogger .MIME_TYPE ;
34import static com .github .stickerifier .stickerify .media .MediaConstraints .MATROSKA_FORMAT ;
45import static com .github .stickerifier .stickerify .media .MediaConstraints .MAX_ANIMATION_DURATION_SECONDS ;
56import 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