Allow serving HEVC for Telegram since it supports it

Related to #711
This commit is contained in:
dangered wolf 2024-03-11 20:36:39 -04:00
parent b7d8e0bb6c
commit 71c73e503c
No known key found for this signature in database
GPG key ID: 41E4D37680ED8B58
2 changed files with 7 additions and 5 deletions

View file

@ -1,5 +1,7 @@
import { Context } from "hono";
/* Help populate API response for media */
export const processMedia = (media: TweetMedia): APIPhoto | APIVideo | null => {
export const processMedia = (c: Context, media: TweetMedia): APIPhoto | APIVideo | null => {
if (media.type === 'photo') {
return {
type: 'photo',
@ -11,7 +13,7 @@ export const processMedia = (media: TweetMedia): APIPhoto | APIVideo | null => {
} else if (media.type === 'video' || media.type === 'animated_gif') {
/* Find the variant with the highest bitrate */
const bestVariant = media.video_info?.variants?.reduce?.((a, b) =>
!a.url.includes('/hevc/') && (a.bitrate ?? 0) > (b.bitrate ?? 0) ? a : b
(c.req.header('User-Agent')?.includes('Telegram') || !a.url.includes('/hevc/')) && (a.bitrate ?? 0) > (b.bitrate ?? 0) ? a : b
);
return {
url: bestVariant?.url || '',

View file

@ -171,7 +171,7 @@ export const buildAPITwitterStatus = async (
/* Populate status media */
mediaList.forEach(media => {
const mediaObject = processMedia(media);
const mediaObject = processMedia(c, media);
if (mediaObject) {
apiStatus.media.all = apiStatus.media?.all ?? [];
apiStatus.media?.all?.push(mediaObject);
@ -234,7 +234,7 @@ export const buildAPITwitterStatus = async (
if (card.media) {
if (card.media.videos) {
card.media.videos.forEach(video => {
const mediaObject = processMedia(video) as APIVideo;
const mediaObject = processMedia(c, video) as APIVideo;
if (mediaObject) {
apiStatus.media.all = apiStatus.media?.all ?? [];
apiStatus.media?.all?.push(mediaObject);
@ -245,7 +245,7 @@ export const buildAPITwitterStatus = async (
}
if (card.media.photos) {
card.media.photos.forEach(photo => {
const mediaObject = processMedia(photo) as APIPhoto;
const mediaObject = processMedia(c, photo) as APIPhoto;
if (mediaObject) {
apiStatus.media.all = apiStatus.media?.all ?? [];
apiStatus.media?.all?.push(mediaObject);