11package com .github .stickerifier .stickerify .media ;
22
3- import static com .github .stickerifier .stickerify .logger .StructuredLogger .MIME_TYPE ;
3+ import static com .github .stickerifier .stickerify .logger .StructuredLogger .FILE_PATH_LOG_KEY ;
4+ import static com .github .stickerifier .stickerify .logger .StructuredLogger .MIME_TYPE_VALUE ;
5+ import static com .github .stickerifier .stickerify .logger .StructuredLogger .STICKER_LOG_KEY ;
46import static com .github .stickerifier .stickerify .media .MediaConstraints .MATROSKA_FORMAT ;
57import static com .github .stickerifier .stickerify .media .MediaConstraints .MAX_ANIMATION_DURATION_SECONDS ;
68import static com .github .stickerifier .stickerify .media .MediaConstraints .MAX_ANIMATION_FILE_SIZE ;
1416import static java .nio .charset .StandardCharsets .ISO_8859_1 ;
1517import static java .nio .charset .StandardCharsets .UTF_8 ;
1618
17- import com .github .stickerifier .stickerify .exception .CorruptedFileException ;
1819import com .github .stickerifier .stickerify .exception .FileOperationException ;
1920import com .github .stickerifier .stickerify .exception .MediaException ;
2021import com .github .stickerifier .stickerify .exception .ProcessException ;
@@ -67,7 +68,7 @@ public final class MediaHelper {
6768 public static @ Nullable File convert (File inputFile ) throws Exception {
6869 var mimeType = detectMimeType (inputFile );
6970
70- return ScopedValue .where (MIME_TYPE , mimeType ).call (() -> performConversion (inputFile , mimeType ));
71+ return ScopedValue .where (MIME_TYPE_VALUE , mimeType ).call (() -> performConversion (inputFile , mimeType ));
7172 }
7273
7374 /**
@@ -81,7 +82,7 @@ private static String detectMimeType(File file) throws MediaException {
8182 try {
8283 return TIKA .detect (file );
8384 } catch (IOException e ) {
84- LOGGER .at (Level .ERROR ).setCause (e ).addKeyValue ("file_name" , file .getName ()).log ("Unable to retrieve MIME type" );
85+ LOGGER .at (Level .ERROR ).setCause (e ).addKeyValue (FILE_PATH_LOG_KEY , file .getPath ()).log ("Unable to retrieve MIME type" );
8586 throw new MediaException (e );
8687 }
8788 }
@@ -144,10 +145,10 @@ private static boolean isSupportedVideo(String mimeType) {
144145 *
145146 * @param file the file to check
146147 * @return {@code true} if the file is compliant
147- * @throws FileOperationException if an error occurred retrieving the size of the file
148+ * @throws MediaException if an error occurred retrieving video information
148149 * @throws InterruptedException if the current thread is interrupted while retrieving file info
149150 */
150- private static boolean isVideoCompliant (File file ) throws FileOperationException , CorruptedFileException , InterruptedException {
151+ private static boolean isVideoCompliant (File file ) throws MediaException , InterruptedException {
151152 var mediaInfo = retrieveMultimediaInfo (file );
152153
153154 var formatInfo = mediaInfo .format ();
@@ -178,10 +179,10 @@ private static boolean isVideoCompliant(File file) throws FileOperationException
178179 *
179180 * @param file the video to check
180181 * @return passed-in video's multimedia information
181- * @throws CorruptedFileException if an error occurred retrieving file information
182+ * @throws MediaException if an error occurred retrieving file information
182183 * @throws InterruptedException if the current thread is interrupted while retrieving file info
183184 */
184- static MultimediaInfo retrieveMultimediaInfo (File file ) throws CorruptedFileException , InterruptedException {
185+ static MultimediaInfo retrieveMultimediaInfo (File file ) throws MediaException , InterruptedException {
185186 var command = new String [] {
186187 "ffprobe" ,
187188 "-hide_banner" ,
@@ -197,7 +198,7 @@ static MultimediaInfo retrieveMultimediaInfo(File file) throws CorruptedFileExce
197198
198199 return GSON .fromJson (output , MultimediaInfo .class );
199200 } catch (ProcessException | JsonSyntaxException e ) {
200- throw new CorruptedFileException ( "The file could not be processed successfully " , e );
201+ throw new MediaException ( "Unable to retrieve media information " , e );
201202 }
202203 }
203204
@@ -253,7 +254,7 @@ private static boolean isAnimatedStickerCompliant(File file, String mimeType) th
253254 try (var gzipInputStream = new GZIPInputStream (new FileInputStream (file ))) {
254255 uncompressedContent = new String (gzipInputStream .readAllBytes (), UTF_8 );
255256 } catch (IOException e ) {
256- LOGGER .at (Level .ERROR ).setCause (e ).addKeyValue ("file_name" , file .getName ()).log ("Unable to retrieve gzip content" );
257+ LOGGER .at (Level .ERROR ).setCause (e ).addKeyValue (FILE_PATH_LOG_KEY , file .getPath ()).log ("Unable to retrieve gzip content" );
257258 }
258259
259260 try {
@@ -267,7 +268,7 @@ private static boolean isAnimatedStickerCompliant(File file, String mimeType) th
267268 }
268269 }
269270
270- LOGGER .at (Level .WARN ).addKeyValue ("sticker" , sticker ).log ("The animated sticker doesn't meet Telegram's requirements" );
271+ LOGGER .at (Level .WARN ).addKeyValue (STICKER_LOG_KEY , sticker ).log ("The animated sticker doesn't meet Telegram's requirements" );
271272 } catch (JsonSyntaxException _) {
272273 LOGGER .at (Level .INFO ).log ("The archive isn't an animated sticker" );
273274 }
@@ -354,10 +355,10 @@ private static boolean isAnimatedWebp(File file) {
354355 * @param image the image to check
355356 * @param mimeType the MIME type of the file
356357 * @return {@code true} if the file is compliant
357- * @throws CorruptedFileException if an error occurred retrieving image information
358+ * @throws MediaException if an error occurred retrieving image information
358359 * @throws InterruptedException if the current thread is interrupted while retrieving file info
359360 */
360- private static boolean isImageCompliant (File image , String mimeType ) throws CorruptedFileException , InterruptedException {
361+ private static boolean isImageCompliant (File image , String mimeType ) throws MediaException , InterruptedException {
361362 var mediaInfo = retrieveMultimediaInfo (image );
362363
363364 var formatInfo = mediaInfo .format ();
@@ -448,7 +449,7 @@ private static File createTempFile(String fileExtension) throws FileOperationExc
448449 private static void deleteFile (File file ) throws FileOperationException {
449450 try {
450451 if (!Files .deleteIfExists (file .toPath ())) {
451- LOGGER .at (Level .INFO ).addKeyValue ("file_path" , file .toPath ()).log ("Unable to delete file" );
452+ LOGGER .at (Level .INFO ).addKeyValue (FILE_PATH_LOG_KEY , file .toPath ()).log ("Unable to delete file" );
452453 }
453454 } catch (IOException e ) {
454455 throw new FileOperationException ("An error occurred deleting the file" , e );
@@ -493,11 +494,10 @@ private static File convertToWebm(File file) throws MediaException, InterruptedE
493494 }
494495 throw new MediaException ("FFmpeg two-pass conversion failed" , e );
495496 } finally {
496- var logFileName = logPrefix + "-0.log" ;
497497 try {
498- deleteFile (new File (logFileName ));
498+ deleteFile (new File (logPrefix + "-0.log" ));
499499 } catch (FileOperationException e ) {
500- LOGGER .at (Level .WARN ).setCause (e ).addKeyValue ( "file_name" , logFileName ). log ("Could not delete log file" );
500+ LOGGER .at (Level .WARN ).setCause (e ).log ("Could not delete log file" );
501501 }
502502 }
503503
0 commit comments