Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,9 @@ private <T extends BaseRequest<T, R>, R extends BaseResponse> R execute(BaseRequ
private static void deleteTempFiles(Set<Path> pathsToDelete) {
for (var path : pathsToDelete) {
try {
Files.deleteIfExists(path);
if (!Files.deleteIfExists(path)) {
LOGGER.atWarn().log("Unable to delete temp file {}", path);
}
} catch (IOException e) {
LOGGER.atError().setCause(e).log("An error occurred trying to delete temp file {}", path);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ public FileOperationException(Throwable cause) {
public FileOperationException(String message, Throwable cause) {
super(message, cause);
}

public FileOperationException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public final class MediaHelper {

private static final Logger LOGGER = LoggerFactory.getLogger(MediaHelper.class);

private static final Tika TIKA = new Tika();
private static final Gson GSON = new Gson();
static final ProcessLocator FFMPEG_LOCATOR = new PathLocator();
private static final int PRESERVE_ASPECT_RATIO = -2;
Expand Down Expand Up @@ -102,7 +103,7 @@ private static String detectMimeType(File file) {
String mimeType = null;

try {
mimeType = new Tika().detect(file);
mimeType = TIKA.detect(file);

LOGGER.atDebug().log("The file has {} MIME type", mimeType);
} catch (IOException _) {
Expand Down Expand Up @@ -204,8 +205,8 @@ private static boolean isFileSizeLowerThan(File file, long threshold) throws Fil
* @return the image, if supported by {@link ImageIO}
*/
private static ImmutableImage toImage(File file) {
try {
return ImmutableImage.loader().fromFile(file);
try (var inputStream = new FileInputStream(file)) {
return ImmutableImage.loader().fromStream(inputStream);
} catch (IOException _) {
return null;
}
Expand Down Expand Up @@ -318,7 +319,9 @@ private static File createTempFile(String fileExtension) throws FileOperationExc
*/
private static void deleteFile(File file) throws FileOperationException {
try {
Files.deleteIfExists(file.toPath());
if (!Files.deleteIfExists(file.toPath())) {
throw new FileOperationException("An error occurred deleting the file");
}
} catch (IOException e) {
throw new FileOperationException("An error occurred deleting the file", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.sksamuel.scrimage.ImmutableImage;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import ws.schild.jave.EncoderException;
Expand All @@ -37,6 +38,7 @@
class MediaHelperTest {

@Test
@Tag("Image")
void resizeRectangularImage() throws Exception {
var jpgImage = loadResource("big.jpg");
var result = MediaHelper.convert(jpgImage);
Expand All @@ -61,6 +63,7 @@ private static String getExtension(File file) {
}

@Test
@Tag("Image")
void resizeSmallImage() throws Exception {
var pngImage = loadResource("small_image.png");
var result = MediaHelper.convert(pngImage);
Expand All @@ -69,6 +72,7 @@ void resizeSmallImage() throws Exception {
}

@Test
@Tag("Image")
void noImageConversionNeeded() throws Exception {
var pngImage = loadResource("valid.png");
var result = MediaHelper.convert(pngImage);
Expand All @@ -77,6 +81,7 @@ void noImageConversionNeeded() throws Exception {
}

@Test
@Tag("Image")
void resizeStaticWebpImage() throws Exception {
var webpImage = loadResource("static.webp");
var result = MediaHelper.convert(webpImage);
Expand All @@ -85,13 +90,7 @@ void resizeStaticWebpImage() throws Exception {
}

@Test
void resizeAnimatedWebpImage() {
var webpImage = loadResource("animated.webp");

assertThrows(MediaException.class, () -> MediaHelper.convert(webpImage));
}

@Test
@Tag("Image")
void resizeFaviconImage() throws Exception {
var faviconImage = loadResource("favicon.ico");
var result = MediaHelper.convert(faviconImage);
Expand All @@ -100,6 +99,7 @@ void resizeFaviconImage() throws Exception {
}

@Test
@Tag("Image")
void resizeTiffImage() throws Exception {
var tiffImage = loadResource("valid.tiff");
var result = MediaHelper.convert(tiffImage);
Expand All @@ -108,6 +108,7 @@ void resizeTiffImage() throws Exception {
}

@Test
@Tag("Image")
void resizePsdImage() throws Exception {
var psdImage = loadResource("valid.psd");
var result = MediaHelper.convert(psdImage);
Expand All @@ -116,6 +117,7 @@ void resizePsdImage() throws Exception {
}

@Test
@Tag("Image")
void resizeDetailedImage() throws Exception {
var detailedImage = loadResource("detailed.jpg");
var result = MediaHelper.convert(detailedImage);
Expand All @@ -124,6 +126,7 @@ void resizeDetailedImage() throws Exception {
}

@Test
@Tag("Image")
void resizeSvgImage() throws Exception {
var svgImage = loadResource("valid.svg");
var result = MediaHelper.convert(svgImage);
Expand All @@ -132,6 +135,7 @@ void resizeSvgImage() throws Exception {
}

@Test
@Tag("Video")
void convertLongMovVideo() throws Exception {
var movVideo = loadResource("long.mov");
var result = MediaHelper.convert(movVideo);
Expand Down Expand Up @@ -160,6 +164,7 @@ private static void assertVideoConsistency(File result, int expectedWidth, int e
}

@Test
@Tag("Video")
void convertMp4WithAudio() throws Exception {
var mp4Video = loadResource("video_with_audio.mp4");
var result = MediaHelper.convert(mp4Video);
Expand All @@ -168,6 +173,7 @@ void convertMp4WithAudio() throws Exception {
}

@Test
@Tag("Video")
void convertM4vWithAudio() throws Exception {
var m4vVideo = loadResource("video_with_audio.m4v");
var result = MediaHelper.convert(m4vVideo);
Expand All @@ -176,6 +182,7 @@ void convertM4vWithAudio() throws Exception {
}

@Test
@Tag("Video")
void convertShortAndLowFpsVideo() throws Exception {
var webmVideo = loadResource("short_low_fps.webm");
var result = MediaHelper.convert(webmVideo);
Expand All @@ -184,6 +191,7 @@ void convertShortAndLowFpsVideo() throws Exception {
}

@Test
@Tag("Video")
void resizeSmallWebmVideo() throws Exception {
var webmVideo = loadResource("small_video_sticker.webm");
var result = MediaHelper.convert(webmVideo);
Expand All @@ -192,6 +200,7 @@ void resizeSmallWebmVideo() throws Exception {
}

@Test
@Tag("Video")
void convertVerticalWebmVideo() throws Exception {
var webmVideo = loadResource("vertical_video_sticker.webm");
var result = MediaHelper.convert(webmVideo);
Expand All @@ -200,6 +209,7 @@ void convertVerticalWebmVideo() throws Exception {
}

@Test
@Tag("Video")
void convertGifVideo() throws Exception {
var gifVideo = loadResource("valid.gif");
var result = MediaHelper.convert(gifVideo);
Expand All @@ -208,6 +218,7 @@ void convertGifVideo() throws Exception {
}

@Test
@Tag("Video")
void convertAviVideo() throws Exception {
var aviVideo = loadResource("valid.avi");
var result = MediaHelper.convert(aviVideo);
Expand All @@ -216,6 +227,7 @@ void convertAviVideo() throws Exception {
}

@Test
@Tag("Video")
void noVideoConversionNeeded() throws Exception {
var webmVideo = loadResource("no_conversion_needed.webm");
var result = MediaHelper.convert(webmVideo);
Expand All @@ -224,6 +236,15 @@ void noVideoConversionNeeded() throws Exception {
}

@Test
@Tag("Video")
void resizeAnimatedWebpVideo() {
var webpVideo = loadResource("animated.webp");

assertThrows(MediaException.class, () -> MediaHelper.convert(webpVideo));
}

@Test
@Tag("AnimatedSticker")
void noAnimatedStickerConversionNeeded() throws Exception {
var animatedSticker = loadResource("animated_sticker.tgs");
var result = MediaHelper.convert(animatedSticker);
Expand All @@ -232,6 +253,7 @@ void noAnimatedStickerConversionNeeded() throws Exception {
}

@Test
@Tag("AnimatedSticker")
void noLowFpsAnimatedStickerConversionNeeded() throws Exception {
var animatedSticker = loadResource("low_fps_animated_sticker.tgs");
var result = MediaHelper.convert(animatedSticker);
Expand All @@ -240,20 +262,23 @@ void noLowFpsAnimatedStickerConversionNeeded() throws Exception {
}

@Test
@Tag("AnimatedSticker")
void nonCompliantAnimatedSticker() {
var animatedSticker = loadResource("non_compliant_animated_sticker.tgs");

assertThrows(MediaException.class, () -> MediaHelper.convert(animatedSticker));
}

@Test
@Tag("AnimatedSticker")
void unsupportedGzipArchive() {
var archive = loadResource("unsupported_archive.gz");

assertThrows(MediaException.class, () -> MediaHelper.convert(archive));
}

@Test
@Tag("UnsupportedFile")
void unsupportedFile() {
var document = loadResource("document.txt");

Expand All @@ -266,6 +291,7 @@ void unsupportedFile() {
class ConcurrencyTest {

@Test
@Tag("Video")
@DisplayName("mov videos")
void concurrentMovVideoConversions() {
var movVideo = loadResource("long.mov");
Expand Down Expand Up @@ -296,6 +322,7 @@ private static void executeConcurrentConversionsOf(File inputFile) {
}

@Test
@Tag("Video")
@DisplayName("mp4 videos")
void concurrentMp4VideoConversions() {
var mp4Video = loadResource("video_with_audio.mp4");
Expand All @@ -304,6 +331,7 @@ void concurrentMp4VideoConversions() {
}

@Test
@Tag("Video")
@DisplayName("m4v videos")
void concurrentM4vVideoConversions() {
var m4vVideo = loadResource("video_with_audio.m4v");
Expand All @@ -312,6 +340,7 @@ void concurrentM4vVideoConversions() {
}

@Test
@Tag("Video")
@DisplayName("webm videos")
void concurrentWebmVideoConversions() {
var webmVideo = loadResource("small_video_sticker.webm");
Expand All @@ -320,6 +349,7 @@ void concurrentWebmVideoConversions() {
}

@Test
@Tag("Video")
@DisplayName("avi videos")
void concurrentAviVideoConversions() {
var aviVideo = loadResource("valid.avi");
Expand All @@ -328,6 +358,7 @@ void concurrentAviVideoConversions() {
}

@Test
@Tag("Video")
@DisplayName("gif videos")
void concurrentGifVideoConversions() {
var gifVideo = loadResource("valid.gif");
Expand All @@ -336,6 +367,7 @@ void concurrentGifVideoConversions() {
}

@Test
@Tag("Image")
@DisplayName("webp images")
void concurrentWebpImageConversions() {
var webpImage = loadResource("static.webp");
Expand All @@ -344,6 +376,7 @@ void concurrentWebpImageConversions() {
}

@Test
@Tag("Image")
@DisplayName("jpg images")
void concurrentJpgImageConversions() {
var jpgImage = loadResource("big.jpg");
Expand All @@ -352,6 +385,7 @@ void concurrentJpgImageConversions() {
}

@Test
@Tag("Image")
@DisplayName("png images")
void concurrentPngImageConversions() {
var pngImage = loadResource("big.png");
Expand All @@ -360,6 +394,7 @@ void concurrentPngImageConversions() {
}

@Test
@Tag("Image")
@DisplayName("ico images")
void concurrentFaviconImageConversions() {
var faviconImage = loadResource("favicon.ico");
Expand All @@ -368,6 +403,7 @@ void concurrentFaviconImageConversions() {
}

@Test
@Tag("Image")
@DisplayName("tiff images")
void concurrentTiffImageConversions() {
var tiffImage = loadResource("valid.tiff");
Expand All @@ -376,6 +412,7 @@ void concurrentTiffImageConversions() {
}

@Test
@Tag("Image")
@DisplayName("psd images")
void concurrentPsdImageConversions() {
var psdImage = loadResource("valid.psd");
Expand All @@ -384,6 +421,7 @@ void concurrentPsdImageConversions() {
}

@Test
@Tag("Image")
@DisplayName("svg images")
void concurrentSvgImageConversions() {
var psdImage = loadResource("valid.svg");
Expand Down
Loading