Skip to content

Commit cd1b9b4

Browse files
Code review comments
1 parent 4e76cf3 commit cd1b9b4

File tree

3 files changed

+31
-27
lines changed

3 files changed

+31
-27
lines changed

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,10 @@ private static boolean isVideoCompliant(File file) throws FileOperationException
147147
return isSizeCompliant(videoInfo.width(), videoInfo.height())
148148
&& videoInfo.frameRate() <= MAX_VIDEO_FRAMES
149149
&& VP9_CODEC.equals(videoInfo.codec())
150-
&& Float.parseFloat(formatInfo.duration()) <= MAX_VIDEO_DURATION_SECONDS
150+
&& formatInfo.duration() <= MAX_VIDEO_DURATION_SECONDS
151151
&& mediaInfo.audio() == null
152152
&& formatInfo.format().startsWith(MATROSKA_FORMAT)
153-
&& Long.parseLong(formatInfo.size()) <= MAX_VIDEO_FILE_SIZE;
153+
&& formatInfo.size() <= MAX_VIDEO_FILE_SIZE;
154154
}
155155

156156
/**
@@ -173,8 +173,7 @@ static MultimediaInfo retrieveMultimediaInfo(File file) throws CorruptedFileExce
173173
};
174174

175175
try {
176-
var lines = ProcessHelper.executeCommand(command);
177-
var body = String.join("\n", lines);
176+
var body = ProcessHelper.executeCommand(command);
178177

179178
return GSON.fromJson(body, MultimediaInfo.class);
180179
} catch (ProcessException | JsonSyntaxException e) {
@@ -183,20 +182,23 @@ static MultimediaInfo retrieveMultimediaInfo(File file) throws CorruptedFileExce
183182
}
184183

185184
record MultimediaInfo(List<StreamInfo> streams, @Nullable FormatInfo format) {
186-
@Nullable StreamInfo audio() {
185+
@Nullable
186+
StreamInfo audio() {
187187
return streams.stream()
188188
.filter(s -> s.type == CodecType.AUDIO)
189189
.findFirst()
190190
.orElse(null);
191191
}
192192

193-
@Nullable StreamInfo video() {
193+
@Nullable
194+
StreamInfo video() {
194195
return streams.stream()
195196
.filter(s -> s.type == CodecType.VIDEO)
196197
.findFirst()
197198
.orElse(null);
198199
}
199200
}
201+
200202
record StreamInfo(@SerializedName("codec_name") String codec, @SerializedName("codec_type") CodecType type, int width, int height, @SerializedName("avg_frame_rate") String frameRateRatio) {
201203
float frameRate() {
202204
if (frameRateRatio.contains("/")) {
@@ -207,11 +209,13 @@ float frameRate() {
207209
}
208210
}
209211
}
212+
210213
private enum CodecType {
211214
@SerializedName("video") VIDEO,
212215
@SerializedName("audio") AUDIO
213216
}
214-
record FormatInfo(@SerializedName("format_name") String format, @Nullable String duration, String size) {}
217+
218+
record FormatInfo(@SerializedName("format_name") String format, @Nullable Float duration, long size) {}
215219

216220
/**
217221
* Checks if the file is a {@code gzip} archive, then it reads its content and verifies if it's a valid JSON.
@@ -301,14 +305,14 @@ private static boolean isImageCompliant(File image, String mimeType) throws Corr
301305
return false;
302306
}
303307

304-
var videoInfo = mediaInfo.video();
305-
if (videoInfo == null) {
308+
var imageInfo = mediaInfo.video();
309+
if (imageInfo == null) {
306310
return false;
307311
}
308312

309313
return ("image/png".equals(mimeType) || "image/webp".equals(mimeType))
310-
&& isSizeCompliant(videoInfo.width(), videoInfo.height())
311-
&& Long.parseLong(formatInfo.size()) <= MAX_IMAGE_FILE_SIZE;
314+
&& isSizeCompliant(imageInfo.width(), imageInfo.height())
315+
&& formatInfo.size() <= MAX_IMAGE_FILE_SIZE;
312316
}
313317

314318
/**

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
import org.slf4j.LoggerFactory;
88

99
import java.io.IOException;
10-
import java.util.ArrayList;
11-
import java.util.List;
10+
import java.util.StringJoiner;
1211
import java.util.concurrent.Semaphore;
1312
import java.util.concurrent.TimeUnit;
1413

@@ -23,7 +22,7 @@ public final class ProcessHelper {
2322
* environment variable (defaults to 4).
2423
*
2524
* @param command the command to be executed
26-
* @return the merged stdout/stderr of the command, split by lines
25+
* @return the merged stdout/stderr of the command
2726
* @throws ProcessException either if:
2827
* <ul>
2928
* <li>the command was unsuccessful
@@ -33,12 +32,12 @@ public final class ProcessHelper {
3332
* </ul>
3433
* @throws InterruptedException if the current thread is interrupted while waiting for the command to finish
3534
*/
36-
public static List<String> executeCommand(final String... command) throws ProcessException, InterruptedException {
35+
public static String executeCommand(final String... command) throws ProcessException, InterruptedException {
3736
SEMAPHORE.acquire();
3837
try {
3938
var process = new ProcessBuilder(command).redirectErrorStream(true).start();
4039

41-
var output = new ArrayList<String>(64);
40+
var output = new StringJoiner("\n");
4241
var readerThread = Thread.ofVirtual().start(() -> {
4342
try (var reader = process.inputReader(UTF_8)) {
4443
reader.lines().forEach(output::add);
@@ -51,17 +50,16 @@ public static List<String> executeCommand(final String... command) throws Proces
5150
if (!finished) {
5251
process.destroyForcibly();
5352
readerThread.join();
54-
throw new ProcessException("The command {} timed out after 1m\n{}", command[0], String.join("\n", output));
53+
throw new ProcessException("The command {} timed out after 1m\n{}", command[0], output.toString());
5554
}
5655

5756
readerThread.join();
5857
var exitCode = process.exitValue();
5958
if (exitCode != 0) {
60-
var lines = String.join("\n", output);
61-
throw new ProcessException("The command {} exited with code {}\n{}", command[0], exitCode, lines);
59+
throw new ProcessException("The command {} exited with code {}\n{}", command[0], exitCode, output.toString());
6260
}
6361

64-
return output;
62+
return output.toString();
6563
} catch (IOException e) {
6664
throw new ProcessException(e);
6765
} finally {

src/test/java/com/github/stickerifier/stickerify/media/MediaHelperTest.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
2929

3030
import java.io.File;
31-
import java.nio.file.Files;
3231
import java.util.concurrent.ConcurrentHashMap;
3332
import java.util.concurrent.atomic.AtomicInteger;
3433
import java.util.stream.IntStream;
@@ -47,15 +46,18 @@ void resizeRectangularImage() throws Exception {
4746
}
4847

4948
private static void assertImageConsistency(File image, int expectedWidth, int expectedHeight) throws Exception {
50-
var imageInfo = MediaHelper.retrieveMultimediaInfo(image).video();
49+
var mediaInfo = MediaHelper.retrieveMultimediaInfo(image);
50+
var imageInfo = mediaInfo.video();
5151
assertNotNull(imageInfo);
52+
var formatInfo = mediaInfo.format();
53+
assertNotNull(formatInfo);
5254
var actualExtension = getExtension(image);
5355

5456
assertAll("Image validation failed",
5557
() -> assertThat("image's extension must be webp", actualExtension, is(equalTo(".webp"))),
5658
() -> assertThat("image's width is not correct", imageInfo.width(), is(equalTo(expectedWidth))),
5759
() -> assertThat("image's height is not correct", imageInfo.height(), is(equalTo(expectedHeight))),
58-
() -> assertThat("image size should not exceed 512 KB", Files.size(image.toPath()), is(lessThanOrEqualTo(MAX_IMAGE_FILE_SIZE)))
60+
() -> assertThat("image size should not exceed 512 KB", formatInfo.size(), is(lessThanOrEqualTo(MAX_IMAGE_FILE_SIZE)))
5961
);
6062
}
6163

@@ -138,8 +140,8 @@ void resizeSvgImage() throws Exception {
138140
}
139141

140142
private static void assumeSvgSupport() throws Exception {
141-
var lines = ProcessHelper.executeCommand("ffmpeg", "-v", "quiet", "-hide_banner", "-decoders");
142-
var supportsSvg = lines.stream().anyMatch(line -> line.contains("(codec svg)"));
143+
var decoders = ProcessHelper.executeCommand("ffmpeg", "-v", "quiet", "-hide_banner", "-decoders");
144+
var supportsSvg = decoders.contains("(codec svg)");
143145
assumeTrue(supportsSvg, "FFmpeg was not compiled with SVG support");
144146
}
145147

@@ -168,10 +170,10 @@ private static void assertVideoConsistency(File video, int expectedWidth, int ex
168170
() -> assertThat("video's height is not correct", videoInfo.height(), is(equalTo(expectedHeight))),
169171
() -> assertThat("video's frame rate is not correct", videoInfo.frameRate(), is(equalTo(expectedFrameRate))),
170172
() -> assertThat("video must be encoded with the VP9 codec", videoInfo.codec(), is(equalTo(VP9_CODEC))),
171-
() -> assertThat("video's duration is not correct", Float.parseFloat(formatInfo.duration()), is(equalTo(expectedDuration))),
173+
() -> assertThat("video's duration is not correct", formatInfo.duration(), is(equalTo(expectedDuration))),
172174
() -> assertThat("video's format must be matroska", formatInfo.format(), startsWith(MATROSKA_FORMAT)),
173175
() -> assertThat("video must have no audio stream", mediaInfo.audio(), is(nullValue())),
174-
() -> assertThat("video size should not exceed 256 KB", Files.size(video.toPath()), is(lessThanOrEqualTo(MAX_VIDEO_FILE_SIZE)))
176+
() -> assertThat("video size should not exceed 256 KB", formatInfo.size(), is(lessThanOrEqualTo(MAX_VIDEO_FILE_SIZE)))
175177
);
176178
}
177179

0 commit comments

Comments
 (0)