Skip to content

Commit 04f820f

Browse files
More nits and fixes
1 parent 534561d commit 04f820f

File tree

5 files changed

+38
-16
lines changed

5 files changed

+38
-16
lines changed

src/main/java/com/github/stickerifier/stickerify/exception/TelegramApiException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public TelegramApiException(String requestMethod, String description) {
99
}
1010

1111
/**
12-
* @return the description of the error received by the api call
12+
* @return the description of the error received by the API call
1313
*/
1414
public String getDescription() {
1515
return description;

src/main/java/com/github/stickerifier/stickerify/logger/HighlightHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
public final class HighlightHelper {
1313

1414
static final String START_GREEN = changeColorTo(BOLD + GREEN_FG);
15-
private static final Pattern MIME_TYPE_PATTERN = Pattern.compile(" (\\w+/[-+.\\w]+) ");
15+
private static final Pattern MIME_TYPE_PATTERN = Pattern.compile("(^|\\s)(\\w+/[-+.\\w]+)(?=\\s|$)");
1616

1717
static String changeColorTo(final String color) {
1818
return ESC_START + color + ESC_END;
@@ -33,7 +33,7 @@ static String greenHighlight(final String message, String previousColor) {
3333
static @Nullable String retrieveMimeType(final String message) {
3434
var matcher = MIME_TYPE_PATTERN.matcher(message);
3535

36-
return matcher.find() ? matcher.group(1) : null;
36+
return matcher.find() ? matcher.group(2) : null;
3737
}
3838

3939
static String replaceFirst(String message, String textToReplace, String replacement) {

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import java.io.FileInputStream;
3535
import java.io.IOException;
3636
import java.nio.file.Files;
37-
import java.util.List;
37+
import java.util.Set;
3838
import java.util.zip.GZIPInputStream;
3939

4040
public final class MediaHelper {
@@ -43,7 +43,7 @@ public final class MediaHelper {
4343

4444
private static final Tika TIKA = new Tika();
4545
private static final Gson GSON = new Gson();
46-
private static final List<String> SUPPORTED_VIDEOS = List.of("image/gif", "video/quicktime", "video/webm",
46+
private static final Set<String> SUPPORTED_VIDEOS = Set.of("image/gif", "video/quicktime", "video/webm",
4747
"video/mp4", "video/x-m4v", "application/x-matroska", "video/x-msvideo");
4848

4949
private static final int IMAGE_KEEP_ASPECT_RATIO = -1;
@@ -118,7 +118,7 @@ private static String detectMimeType(File file) throws MediaException {
118118
* @return {@code true} if the MIME type is supported
119119
*/
120120
private static boolean isSupportedVideo(String mimeType) {
121-
return SUPPORTED_VIDEOS.stream().anyMatch(format -> format.equals(mimeType));
121+
return SUPPORTED_VIDEOS.contains(mimeType);
122122
}
123123

124124
/**
@@ -274,12 +274,13 @@ private static File convertToWebp(File file) throws MediaException, InterruptedE
274274
var command = new String[] {
275275
"ffmpeg",
276276
"-y",
277+
"-hide_banner",
277278
"-v", "error",
278279
"-i", file.getAbsolutePath(),
279280
"-vf", "scale='if(gt(iw,ih),%1$d,%2$d)':'if(gt(iw,ih),%2$d,%1$d)'".formatted(MAX_SIDE_LENGTH, IMAGE_KEEP_ASPECT_RATIO),
280281
"-c:v", "libwebp",
281282
"-lossless", "1",
282-
"-q:v", "100",
283+
"-compression_level", "6",
283284
webpImage.getAbsolutePath()
284285
};
285286

@@ -355,6 +356,7 @@ private static File convertToWebm(File file) throws MediaException, InterruptedE
355356
var baseCommand = new String[] {
356357
"ffmpeg",
357358
"-y",
359+
"-hide_banner",
358360
"-v", "error",
359361
"-i", file.getAbsolutePath(),
360362
"-vf", "scale='if(gt(iw,ih),%1$d,%2$d)':'if(gt(iw,ih),%2$d,%1$d)',fps='min(%3$d,source_fps)'".formatted(MAX_SIDE_LENGTH, VIDEO_KEEP_ASPECT_RATIO, MAX_VIDEO_FRAMES),

src/main/java/com/github/stickerifier/stickerify/process/PathLocator.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public enum PathLocator implements ProcessLocator {
2121
PathLocator() {
2222
var logger = LoggerFactory.getLogger(PathLocator.class);
2323
var ffmpegLocation = System.getenv("FFMPEG_PATH");
24+
2425
try {
2526
if (ffmpegLocation == null || ffmpegLocation.isBlank()) {
2627
ffmpegLocation = ProcessHelper.executeCommand(OsConstants.FIND_EXECUTABLE, "ffmpeg").getFirst();
@@ -32,6 +33,7 @@ public enum PathLocator implements ProcessLocator {
3233
} catch (InterruptedException _) {
3334
Thread.currentThread().interrupt();
3435
}
36+
3537
this.ffmpegLocation = Objects.requireNonNull(ffmpegLocation);
3638
}
3739

src/main/java/com/github/stickerifier/stickerify/process/ProcessHelper.java

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@
33
import static java.nio.charset.StandardCharsets.UTF_8;
44

55
import com.github.stickerifier.stickerify.exception.ProcessException;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
68

79
import java.io.IOException;
8-
import java.util.LinkedList;
10+
import java.util.ArrayList;
911
import java.util.List;
1012
import java.util.concurrent.Semaphore;
13+
import java.util.concurrent.TimeUnit;
1114

1215
public final class ProcessHelper {
1316

17+
private static final Logger LOGGER = LoggerFactory.getLogger(ProcessHelper.class);
1418
private static final Semaphore SEMAPHORE = new Semaphore(getMaxConcurrentProcesses());
1519

1620
/**
@@ -23,6 +27,7 @@ public final class ProcessHelper {
2327
* @throws ProcessException either if:
2428
* <ul>
2529
* <li>the command was unsuccessful
30+
* <li>the waiting time elapsed
2631
* <li>an unexpected failure happened running the command
2732
* <li>an unexpected failure happened reading the output
2833
* </ul>
@@ -33,19 +38,28 @@ public static List<String> executeCommand(final String... command) throws Proces
3338
try {
3439
var process = new ProcessBuilder(command).redirectErrorStream(true).start();
3540

36-
var output = new LinkedList<String>();
37-
try (var reader = process.inputReader(UTF_8)) {
38-
reader.lines().forEach(output::add);
41+
var output = new ArrayList<String>(64);
42+
Thread.ofVirtual().start(() -> {
43+
try (var reader = process.inputReader(UTF_8)) {
44+
reader.lines().forEach(output::add);
45+
} catch (IOException e) {
46+
LOGGER.atError().setCause(e).log("Error while closing process output reader");
47+
}
48+
});
49+
50+
var finished = process.waitFor(1, TimeUnit.MINUTES);
51+
if (!finished) {
52+
process.destroyForcibly();
53+
throw new ProcessException("The command {} timed out after 1m\n{}", command[0], String.join("\n", output));
3954
}
4055

41-
var exitCode = process.waitFor();
56+
var exitCode = process.exitValue();
4257
if (exitCode != 0) {
43-
process.destroy();
4458
var lines = String.join("\n", output);
4559
throw new ProcessException("The command {} exited with code {}\n{}", command[0], exitCode, lines);
4660
}
4761

48-
return output;
62+
return List.copyOf(output);
4963
} catch (IOException e) {
5064
throw new ProcessException(e);
5165
} finally {
@@ -54,8 +68,12 @@ public static List<String> executeCommand(final String... command) throws Proces
5468
}
5569

5670
private static int getMaxConcurrentProcesses() {
57-
var value = System.getenv("CONCURRENT_PROCESSES");
58-
return value == null ? 4 : Integer.parseInt(value);
71+
var env = System.getenv("CONCURRENT_PROCESSES");
72+
var value = env == null ? 4 : Integer.parseInt(env);
73+
if (value <= 1) {
74+
throw new IllegalArgumentException("CONCURRENT_PROCESSES must be >= 1 (was " + env + ")");
75+
}
76+
return value;
5977
}
6078

6179
private ProcessHelper() {

0 commit comments

Comments
 (0)