Skip to content

Commit 76f779e

Browse files
Strengthen file processing (#396)
Co-authored-by: Enrico Martelli <[email protected]>
1 parent 2854bb4 commit 76f779e

File tree

8 files changed

+1673
-1542
lines changed

8 files changed

+1673
-1542
lines changed

qodana.baseline.json

Lines changed: 1575 additions & 1517 deletions
Large diffs are not rendered by default.

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.github.stickerifier.stickerify.telegram.model.TelegramRequest;
2121
import com.pengrad.telegrambot.ExceptionHandler;
2222
import com.pengrad.telegrambot.TelegramBot;
23+
import com.pengrad.telegrambot.TelegramException;
2324
import com.pengrad.telegrambot.UpdatesListener;
2425
import com.pengrad.telegrambot.model.LinkPreviewOptions;
2526
import com.pengrad.telegrambot.model.Update;
@@ -47,15 +48,12 @@
4748
*
4849
* @author Roberto Cella
4950
*/
50-
public class Stickerify {
51+
public record Stickerify(TelegramBot bot, Executor executor) implements ExceptionHandler {
5152

5253
private static final Logger LOGGER = LoggerFactory.getLogger(Stickerify.class);
5354
private static final String BOT_TOKEN = System.getenv("STICKERIFY_TOKEN");
5455
private static final ThreadFactory VIRTUAL_THREAD_FACTORY = Thread.ofVirtual().name("Virtual-", 0).factory();
5556

56-
private final TelegramBot bot;
57-
private final Executor executor;
58-
5957
/**
6058
* Instantiate the bot processing requests with virtual threads.
6159
*
@@ -70,13 +68,13 @@ public Stickerify() {
7068
*
7169
* @see Stickerify
7270
*/
73-
Stickerify(TelegramBot bot, Executor executor) {
74-
this.bot = bot;
75-
this.executor = executor;
76-
77-
ExceptionHandler exceptionHandler = e -> LOGGER.atError().log("There was an unexpected failure: {}", e.getMessage());
71+
public Stickerify {
72+
bot.setUpdatesListener(this::handleUpdates, this, new GetUpdates().timeout(50));
73+
}
7874

79-
bot.setUpdatesListener(this::handleUpdates, exceptionHandler, new GetUpdates().timeout(50));
75+
@Override
76+
public void onException(TelegramException e) {
77+
LOGGER.atError().log("There was an unexpected failure: {}", e.getMessage());
8078
}
8179

8280
private int handleUpdates(List<Update> updates) {
@@ -220,7 +218,9 @@ private <T extends BaseRequest<T, R>, R extends BaseResponse> R execute(BaseRequ
220218
private static void deleteTempFiles(Set<Path> pathsToDelete) {
221219
for (var path : pathsToDelete) {
222220
try {
223-
Files.deleteIfExists(path);
221+
if (!Files.deleteIfExists(path)) {
222+
LOGGER.atInfo().log("Unable to delete temp file {}", path);
223+
}
224224
} catch (IOException e) {
225225
LOGGER.atError().setCause(e).log("An error occurred trying to delete temp file {}", path);
226226
}

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public final class MediaHelper {
5050

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

53+
private static final Tika TIKA = new Tika();
5354
private static final Gson GSON = new Gson();
5455
static final ProcessLocator FFMPEG_LOCATOR = new PathLocator();
5556
private static final int PRESERVE_ASPECT_RATIO = -2;
@@ -102,7 +103,7 @@ private static String detectMimeType(File file) {
102103
String mimeType = null;
103104

104105
try {
105-
mimeType = new Tika().detect(file);
106+
mimeType = TIKA.detect(file);
106107

107108
LOGGER.atDebug().log("The file has {} MIME type", mimeType);
108109
} catch (IOException _) {
@@ -204,8 +205,8 @@ private static boolean isFileSizeLowerThan(File file, long threshold) throws Fil
204205
* @return the image, if supported by {@link ImageIO}
205206
*/
206207
private static ImmutableImage toImage(File file) {
207-
try {
208-
return ImmutableImage.loader().fromFile(file);
208+
try (var inputStream = new FileInputStream(file)) {
209+
return ImmutableImage.loader().fromStream(inputStream);
209210
} catch (IOException _) {
210211
return null;
211212
}
@@ -318,7 +319,9 @@ private static File createTempFile(String fileExtension) throws FileOperationExc
318319
*/
319320
private static void deleteFile(File file) throws FileOperationException {
320321
try {
321-
Files.deleteIfExists(file.toPath());
322+
if (!Files.deleteIfExists(file.toPath())) {
323+
LOGGER.atInfo().log("Unable to delete file {}", file.toPath());
324+
}
322325
} catch (IOException e) {
323326
throw new FileOperationException("An error occurred deleting the file", e);
324327
}

src/test/java/com/github/stickerifier/stickerify/bot/StickerifyTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@
88
import static org.junit.jupiter.api.Assertions.assertNotNull;
99

1010
import com.github.stickerifier.stickerify.junit.ClearTempFiles;
11+
import com.github.stickerifier.stickerify.junit.Tags;
1112
import com.github.stickerifier.stickerify.telegram.Answer;
1213
import com.pengrad.telegrambot.TelegramBot;
1314
import mockwebserver3.MockWebServer;
1415
import mockwebserver3.RecordedRequest;
1516
import mockwebserver3.junit5.StartStop;
17+
import org.junit.jupiter.api.Tag;
1618
import org.junit.jupiter.api.Test;
1719

1820
import java.net.URLEncoder;
1921

22+
@Tag(Tags.TELEGRAM_API)
2023
@ClearTempFiles
2124
class StickerifyTest {
2225

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.github.stickerifier.stickerify.junit;
2+
3+
public final class Tags {
4+
public static final String ANIMATED_STICKER = "AnimatedSticker";
5+
public static final String CONCURRENT = "Concurrent";
6+
public static final String IMAGE = "Image";
7+
public static final String LOG = "Log";
8+
public static final String MEDIA = "Media";
9+
public static final String TELEGRAM_API = "TelegramApi";
10+
public static final String UNSUPPORTED_FILE = "UnsupportedFile";
11+
public static final String VIDEO = "Video";
12+
13+
private Tags() {
14+
throw new UnsupportedOperationException();
15+
}
16+
}

src/test/java/com/github/stickerifier/stickerify/logger/ExceptionHighlighterTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@
99
import static org.hamcrest.Matchers.equalTo;
1010
import static org.hamcrest.Matchers.is;
1111

12+
import com.github.stickerifier.stickerify.junit.Tags;
1213
import org.junit.jupiter.api.BeforeEach;
1314
import org.junit.jupiter.api.DisplayName;
15+
import org.junit.jupiter.api.Tag;
1416
import org.junit.jupiter.api.Test;
1517

18+
@Tag(Tags.LOG)
1619
class ExceptionHighlighterTest {
1720

1821
private static final String LOG_MESSAGE = "Received request";

src/test/java/com/github/stickerifier/stickerify/logger/MessageHighlighterTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@
88
import static org.hamcrest.Matchers.equalTo;
99
import static org.hamcrest.Matchers.is;
1010

11+
import com.github.stickerifier.stickerify.junit.Tags;
1112
import org.junit.jupiter.api.BeforeEach;
1213
import org.junit.jupiter.api.DisplayName;
14+
import org.junit.jupiter.api.Tag;
1315
import org.junit.jupiter.api.Test;
1416

17+
@Tag(Tags.LOG)
1518
class MessageHighlighterTest {
1619

1720
private static final String LOG_MESSAGE = "Received request";

0 commit comments

Comments
 (0)