Media: Bring back old size picking function (#5512)

This commit is contained in:
zubiden 2025-01-21 18:22:10 +01:00 committed by Alexander Zinchuk
parent 92b65851f9
commit b8e9dae74f

View File

@ -103,8 +103,8 @@ const PING_WAKE_UP_WARNING_TIMEOUT = 1000; // 1 sec
const PING_DISCONNECT_DELAY = 60000; // 1 min const PING_DISCONNECT_DELAY = 60000; // 1 min
// All types // All types, sorted by size
const sizeTypes = ['u', 'v', 'w', 'y', 'd', 'x', 'c', 'm', 'b', 'a', 's', 'f'] as const; const sizeTypes = ['u', 'v', 'w', 'y', 'd', 'x', 'c', 'm', 'b', 'a', 's', 'f', 'i', 'j'] as const;
export type SizeType = typeof sizeTypes[number]; export type SizeType = typeof sizeTypes[number];
class TelegramClient { class TelegramClient {
@ -866,52 +866,26 @@ class TelegramClient {
}); });
} }
getThumb( pickFileSize(sizes: (Api.TypePhotoSize | Api.TypeVideoSize)[], sizeType?: SizeType) {
thumbs: (Api.TypePhotoSize | Api.TypeVideoSize)[], if (!sizes?.length) return undefined;
thumb?: string | Api.TypePhotoSize | Api.VideoSize if (!sizeType) {
) { const maxSize = sizes.reduce((max, current) => {
function sortThumb(thumb: Api.TypePhotoSize | Api.TypeVideoSize) { if (!('w' in current)) return max;
if (thumb instanceof Api.PhotoStrippedSize) { if (!max || !('w' in max)) return current;
return thumb.bytes.length; return max.w > current.w ? max : current;
} }, undefined as Api.TypePhotoSize | Api.TypeVideoSize | undefined);
if (thumb instanceof Api.PhotoCachedSize) { return maxSize;
return thumb.bytes.length;
}
if (thumb instanceof Api.PhotoSize) {
return thumb.size;
}
if (thumb instanceof Api.PhotoSizeProgressive) {
return Math.max(...thumb.sizes);
}
if (thumb instanceof Api.VideoSize) {
return thumb.size;
}
return 0;
} }
thumbs = thumbs.sort((a, b) => sortThumb(a) - sortThumb(b)); const indexOfSize = sizeTypes.indexOf(sizeType);
const correctThumbs = []; let size;
for (const t of thumbs) { for (let i = indexOfSize; i < sizeTypes.length; i++) {
if (!(t instanceof Api.PhotoPathSize)) { size = sizes.find((s) => 'type' in s && s.type === sizeTypes[i]);
correctThumbs.push(t); if (size) {
return size;
} }
} }
if (thumb == undefined) { return undefined;
return correctThumbs.pop();
} else if (typeof thumb == "string") {
for (const t of correctThumbs) {
if ("type" in t && t.type == thumb) {
return t;
}
}
} else if (
thumb instanceof Api.PhotoSize ||
thumb instanceof Api.PhotoCachedSize ||
thumb instanceof Api.PhotoStrippedSize ||
thumb instanceof Api.VideoSize
) {
return thumb;
}
} }
_downloadCachedPhotoSize(size: Api.PhotoCachedSize | Api.PhotoStrippedSize) { _downloadCachedPhotoSize(size: Api.PhotoCachedSize | Api.PhotoStrippedSize) {
@ -937,7 +911,7 @@ class TelegramClient {
const isVideoSize = args.sizeType === 'u' || args.sizeType === 'v'; const isVideoSize = args.sizeType === 'u' || args.sizeType === 'v';
const videoSizes = isVideoSize ? photo.videoSizes! : []; const videoSizes = isVideoSize ? photo.videoSizes! : [];
const size = this.getThumb([...videoSizes, ...photo.sizes], args.sizeType); const size = this.pickFileSize([...videoSizes, ...photo.sizes], args.sizeType);
if (!size if (!size
|| size instanceof Api.PhotoSizeEmpty || size instanceof Api.PhotoSizeEmpty
@ -985,7 +959,7 @@ class TelegramClient {
let size; let size;
if (args.sizeType) { if (args.sizeType) {
size = this.getThumb([...(doc.thumbs || []), ...(doc.videoThumbs || [])], args.sizeType); size = this.pickFileSize([...(doc.thumbs || []), ...(doc.videoThumbs || [])], args.sizeType);
if (!size && doc.mimeType.startsWith('video/')) { if (!size && doc.mimeType.startsWith('video/')) {
return undefined; return undefined;
} }