Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,12 @@
*
* @author Roberto Cella
*/
public class Stickerify {
public record Stickerify(TelegramBot bot, Executor executor) {

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

private final TelegramBot bot;
private final Executor executor;

/**
* Instantiate the bot processing requests with virtual threads.
*
Expand All @@ -70,7 +67,7 @@ public Stickerify() {
*
* @see Stickerify
*/
Stickerify(TelegramBot bot, Executor executor) {
public Stickerify(TelegramBot bot, Executor executor) {
this.bot = bot;
this.executor = executor;

Expand Down Expand Up @@ -220,7 +217,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.atInfo().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 @@ -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())) {
LOGGER.atInfo().log("Unable to delete file {}", file.toPath());
}
} 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 @@ -13,10 +13,12 @@
import mockwebserver3.MockWebServer;
import mockwebserver3.RecordedRequest;
import mockwebserver3.junit5.StartStop;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

import java.net.URLEncoder;

@Tag("telegramApi")
@ClearTempFiles
class StickerifyTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

@Tag("log")
class ExceptionHighlighterTest {

private static final String LOG_MESSAGE = "Received request";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

@Tag("log")
class MessageHighlighterTest {

private static final String LOG_MESSAGE = "Received request";
Expand Down
Loading
Loading