Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Animated stickers validation - improve readability #167

Merged
merged 6 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public final class MediaConstraints {
static final String MATROSKA_FORMAT = "matroska";
static final long MAX_VIDEO_FILE_SIZE = 256_000L;
static final long MAX_ANIMATION_FILE_SIZE = 64_000L;
static final int ANIMATION_FRAMERATE = 60;
static final int MAX_ANIMATION_DURATION_SECONDS = 180;
static final int MAX_ANIMATION_FRAMERATE = 60;
static final float MAX_ANIMATION_DURATION_SECONDS = 3F;

private MediaConstraints() {
throw new UnsupportedOperationException();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.github.stickerifier.stickerify.media;

import static com.github.stickerifier.stickerify.media.MediaConstraints.ANIMATION_FRAMERATE;
import static com.github.stickerifier.stickerify.media.MediaConstraints.MATROSKA_FORMAT;
import static com.github.stickerifier.stickerify.media.MediaConstraints.MAX_ANIMATION_DURATION_SECONDS;
import static com.github.stickerifier.stickerify.media.MediaConstraints.MAX_ANIMATION_FILE_SIZE;
import static com.github.stickerifier.stickerify.media.MediaConstraints.MAX_ANIMATION_FRAMERATE;
import static com.github.stickerifier.stickerify.media.MediaConstraints.MAX_SIZE;
import static com.github.stickerifier.stickerify.media.MediaConstraints.MAX_VIDEO_DURATION_MILLIS;
import static com.github.stickerifier.stickerify.media.MediaConstraints.MAX_VIDEO_FILE_SIZE;
Expand Down Expand Up @@ -133,13 +133,11 @@ private static boolean isAnimatedStickerCompliant(File file, String mimeType) th
return false;
}

private record AnimationDetails(
@SerializedName("w") int width,
@SerializedName("h") int height,
@SerializedName("fr") int frameRate,
@SerializedName("ip") int start,
@SerializedName("op") int end
) {}
private record AnimationDetails(@SerializedName("w") int width, @SerializedName("h") int height, @SerializedName("fr") int frameRate, @SerializedName("ip") float start, @SerializedName("op") float end) {
private float duration() {
return (end() - start()) / frameRate();
}
}

/**
* Checks if passed-in animation is already compliant with Telegram's requisites.
Expand All @@ -149,9 +147,10 @@ private record AnimationDetails(
* @return {@code true} if the animation is compliant
*/
private static boolean isAnimationCompliant(AnimationDetails animation) {
return animation != null && animation.frameRate() <= ANIMATION_FRAMERATE
&& animation.end() - animation.start() <= MAX_ANIMATION_DURATION_SECONDS
&& animation.width() == MAX_SIZE && animation.width() == animation.height();
return animation != null
&& animation.frameRate() <= MAX_ANIMATION_FRAMERATE
&& animation.duration() <= MAX_ANIMATION_DURATION_SECONDS
&& animation.width() == MAX_SIZE && animation.height() == MAX_SIZE;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public final class MockResponses {
id: 1
},
sticker: {
file_id: "animated_sticker.gz",
file_id: "animated_sticker.tgs",
file_size: 64000
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ void fileTooBig() throws Exception {
@Test
void fileAlreadyValid() throws Exception {
server.enqueue(MockResponses.ANIMATED_STICKER);
server.enqueue(MockResponses.fileInfo("animated_sticker.gz"));
server.enqueue(MockResponses.fileDownload(resources.loadResource("animated_sticker.gz")));
server.enqueue(MockResponses.fileInfo("animated_sticker.tgs"));
server.enqueue(MockResponses.fileDownload(resources.loadResource("animated_sticker.tgs")));

startBot();

Expand All @@ -121,10 +121,10 @@ void fileAlreadyValid() throws Exception {

var getFile = server.takeRequest();
assertEquals("/api/token/getFile", getFile.getPath());
assertEquals("file_id=animated_sticker.gz", getFile.getBody().readUtf8());
assertEquals("file_id=animated_sticker.tgs", getFile.getBody().readUtf8());

var download = server.takeRequest();
assertEquals("/files/token/animated_sticker.gz", download.getPath());
assertEquals("/files/token/animated_sticker.tgs", download.getPath());

var sendMessage = server.takeRequest();
assertEquals("/api/token/sendMessage", sendMessage.getPath());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ void noVideoConversionNeeded() throws Exception {

@Test
void noAnimatedStickerConversionNeeded() throws Exception {
var animatedSticker = resources.loadResource("animated_sticker.gz");
var animatedSticker = resources.loadResource("animated_sticker.tgs");
var result = MediaHelper.convert(animatedSticker);

assertThat(result, is(nullValue()));
Expand Down