Skip to content

Commit

Permalink
fix: don't operate on non image or video links
Browse files Browse the repository at this point in the history
  • Loading branch information
shaksternano committed May 20, 2024
1 parent 3c4ab6e commit 951de54
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ import java.net.URI

private const val DEFAULT_TENOR_API_KEY: String = "LIVDSRZULELA"

suspend fun retrieveTenorUrlOrDefault(url: String, getGif: Boolean): UrlInfo {
val tenorGifUrl = if (getGif)
retrieveTenorMediaUrl(url, TenorMediaType.GIF_LARGE)
else null
val tenorUrl = if (getGif) tenorGifUrl
else retrieveTenorMediaUrl(url, TenorMediaType.MP4_NORMAL)
suspend fun retrieveTenorMediaUrl(url: String, getGif: Boolean): UrlInfo? {
val mediaType =
if (getGif) TenorMediaType.GIF_LARGE
else TenorMediaType.MP4_NORMAL
val mediaUrl = retrieveTenorMediaUrl(url, mediaType) ?: return null
return UrlInfo(
url = tenorUrl ?: url,
gifv = tenorGifUrl == null && isTenorUrl(url)
url = mediaUrl,
gifv = !getGif,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import io.github.shaksternano.borgar.core.io.task.MediaProcessingTask
import io.github.shaksternano.borgar.core.io.task.TranscodeTask
import io.github.shaksternano.borgar.core.logger
import io.github.shaksternano.borgar.core.util.asSingletonList
import io.github.shaksternano.borgar.core.util.retrieveTenorUrlOrDefault
import io.github.shaksternano.borgar.core.util.retrieveTenorMediaUrl
import io.github.shaksternano.borgar.messaging.entity.getContent
import io.github.shaksternano.borgar.messaging.event.CommandEvent
import io.github.shaksternano.borgar.messaging.util.getUrlsExceptSelf
Expand Down Expand Up @@ -159,7 +159,7 @@ private data class FileExecutable(
val embed = messageIntersection.getEmbeds().firstOrNull { it.url == url }
val embedContent = embed?.getContent(getGif)
if (embedContent != null) return embedContent
return retrieveTenorUrlOrDefault(url, getGif)
return retrieveTenorMediaUrl(url, getGif) ?: UrlInfo(url)
}

override fun then(after: Executable): Executable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import io.github.shaksternano.borgar.core.graphics.drawable.ImageDrawable
import io.github.shaksternano.borgar.core.io.DataSource
import io.github.shaksternano.borgar.core.io.UrlInfo
import io.github.shaksternano.borgar.core.io.useHttpClient
import io.github.shaksternano.borgar.core.util.*
import io.github.shaksternano.borgar.core.util.ChannelEnvironment
import io.github.shaksternano.borgar.core.util.TenorMediaType
import io.github.shaksternano.borgar.core.util.getUrls
import io.github.shaksternano.borgar.core.util.retrieveTenorMediaUrl
import io.github.shaksternano.borgar.messaging.BotManager
import io.github.shaksternano.borgar.messaging.MessagingPlatform
import io.github.shaksternano.borgar.messaging.command.CommandMessageIntersection
Expand Down Expand Up @@ -48,8 +51,12 @@ suspend fun CommandMessageIntersection.getUrls(getGif: Boolean): List<UrlInfo> =
}
)
addAll(
content.getUrls().map {
retrieveTenorUrlOrDefault(it, getGif)
content.getUrls().mapNotNull {
if (isImageOrVideo(it)) {
UrlInfo(it)
} else {
retrieveTenorMediaUrl(it, getGif)
}
}
)
}
Expand All @@ -69,18 +76,13 @@ private suspend fun CommandMessageIntersection.getUrlDrawables(): Map<String, Dr
return content.getUrls()
.associateBy { it }
.mapNotNull { entry ->
var checkContentType = true
val url = embeds[entry.key]?.getContent(false)?.url
?: retrieveTenorMediaUrl(entry.key, TenorMediaType.MP4_NORMAL)
?: retrieveTenorMediaUrl(entry.key, TenorMediaType.MP4_NORMAL).also {
checkContentType = false
}
?: entry.key
val isImageOrVideo = useHttpClient { client ->
runCatching {
val contentType = client.head(url).contentType()
contentType?.let {
it.match(ContentType.Image.Any) || it.match(ContentType.Video.Any)
} ?: false
}.getOrDefault(false)
}
if (!isImageOrVideo) {
if (checkContentType && !isImageOrVideo(url)) {
return@mapNotNull null
}
val dataSource = DataSource.fromUrl(url)
Expand All @@ -90,6 +92,15 @@ private suspend fun CommandMessageIntersection.getUrlDrawables(): Map<String, Dr
}.associate { it }
}

private suspend fun isImageOrVideo(url: String): Boolean {
val contentType = runCatching {
useHttpClient { client ->
client.head(url).contentType()
}
}.getOrNull() ?: return false
return contentType.match(ContentType.Image.Any) || contentType.match(ContentType.Video.Any)
}

suspend fun CommandMessageIntersection.getEmojiUrls(): Map<String, String> {
val emojiUrls = mutableMapOf<String, String>()

Expand Down

0 comments on commit 951de54

Please sign in to comment.