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 ;
@@ -60,12 +61,41 @@ public final class MediaHelper {
6061 *
6162 * @param inputFile the file to convert
6263 * @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+ return ScopedValue .where (MIME_TYPE , detectMimeType (inputFile )).call (() -> performConversion (inputFile , MIME_TYPE .get ()));
69+ }
70+
71+ /**
72+ * Analyzes the file to detect its media type.
73+ *
74+ * @param file the file sent to the bot
75+ * @return the MIME type of the passed-in file
76+ * @throws MediaException if the file could not be read
77+ */
78+ private static String detectMimeType (File file ) throws MediaException {
79+ try {
80+ var mimeType = TIKA .detect (file );
81+ LOGGER .at (Level .DEBUG ).log ("MIME type successfully detected" );
82+
83+ return mimeType ;
84+ } catch (IOException e ) {
85+ LOGGER .at (Level .ERROR ).addKeyValue ("file_name" , file .getName ()).log ("Unable to retrieve MIME type" );
86+ throw new MediaException (e );
87+ }
88+ }
89+
90+ /**
91+ * @param inputFile the file to convert
92+ * @param mimeType the MIME type of the file
93+ * @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 );
68-
98+ private static @ Nullable File performConversion (File inputFile , String mimeType ) throws MediaException , InterruptedException {
6999 try {
70100 if (isSupportedVideo (mimeType )) {
71101 if (isVideoCompliant (inputFile )) {
@@ -90,32 +120,13 @@ public final class MediaHelper {
90120 return convertToWebp (inputFile );
91121 }
92122 } 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 );
123+ LOGGER .at (Level .WARN ).setCause (e ).log ("The file could not be converted" );
94124 throw e ;
95125 }
96126
97127 throw new MediaException ("The file with {} MIME type is not supported" , mimeType );
98128 }
99129
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-
119130 /**
120131 * Checks if the MIME type corresponds to one of the supported video formats.
121132 *
0 commit comments