2026-06-01 23:08:58 +08:00
|
|
|
import type { ImageTranscode, VideoTranscode } from '@shumai/dtos'
|
2026-05-19 16:05:09 +08:00
|
|
|
|
|
|
|
|
type Transcode = ImageTranscode | VideoTranscode
|
|
|
|
|
|
2026-08-30 17:16:20 +08:00
|
|
|
const WEB_IMAGE_EXTENSION_REGEX = /\.(png|jpe?g|webp|gif|svg|avif|bmp|ico)$/i
|
|
|
|
|
|
|
|
|
|
export function isImageFileName(filename?: string | null): boolean {
|
|
|
|
|
if (!filename) return false
|
|
|
|
|
return WEB_IMAGE_EXTENSION_REGEX.test(filename)
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-19 16:05:09 +08:00
|
|
|
export function getBestTranscode(
|
|
|
|
|
transcodes: Transcode[] | undefined,
|
|
|
|
|
screenWidth: number,
|
|
|
|
|
): Transcode | null {
|
|
|
|
|
if (!transcodes || transcodes.length === 0) {
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Sort by width descending to easily find largest available
|
2026-08-07 22:40:06 +08:00
|
|
|
const sorted = [...transcodes].sort((a, b) => {
|
2026-05-19 16:05:09 +08:00
|
|
|
const wA = a.width ?? 0
|
|
|
|
|
const wB = b.width ?? 0
|
2026-08-07 22:40:06 +08:00
|
|
|
return wB - wA // Descending width
|
2026-05-19 16:05:09 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Find smallest width that is >= screenWidth
|
|
|
|
|
const suitable = sorted.filter((t) => (t.width ?? 0) >= screenWidth)
|
|
|
|
|
|
|
|
|
|
if (suitable.length > 0) {
|
|
|
|
|
// Sort suitable by width ASCENDING to find "smallest fit".
|
|
|
|
|
suitable.sort((a, b) => {
|
|
|
|
|
const wA = a.width ?? 0
|
|
|
|
|
const wB = b.width ?? 0
|
2026-08-07 22:40:06 +08:00
|
|
|
return wA - wB // Ascending
|
2026-05-19 16:05:09 +08:00
|
|
|
})
|
|
|
|
|
return suitable[0]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If no candidate >= screenWidth, simply return the largest available (first in original sorted)
|
|
|
|
|
return sorted[0]
|
|
|
|
|
}
|
2026-10-01 13:27:50 +08:00
|
|
|
|
|
|
|
|
export function getVideoResolutionLabel(
|
|
|
|
|
transcode?: {
|
|
|
|
|
width?: number
|
|
|
|
|
height?: number
|
|
|
|
|
resolution?: string
|
|
|
|
|
} | null,
|
|
|
|
|
): string {
|
|
|
|
|
if (!transcode) return ''
|
|
|
|
|
|
|
|
|
|
if (transcode.resolution) {
|
|
|
|
|
return transcode.resolution
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const width = transcode.width ?? 0
|
|
|
|
|
const height = transcode.height ?? 0
|
|
|
|
|
|
|
|
|
|
if (width > 0 && height > 0) {
|
|
|
|
|
const longSide = Math.max(width, height)
|
|
|
|
|
const shortSide = Math.min(width, height)
|
2026-10-11 21:32:28 +08:00
|
|
|
|
|
|
|
|
if (longSide >= 3840 || shortSide >= 2160) return '2160p'
|
|
|
|
|
if (longSide >= 2560 || shortSide >= 1440) return '1440p'
|
|
|
|
|
if (longSide >= 1920 || shortSide >= 1080) return '1080p'
|
|
|
|
|
if (longSide >= 1280 || shortSide >= 720) return '720p'
|
|
|
|
|
if (longSide >= 854 || shortSide >= 480) return '480p'
|
|
|
|
|
if (longSide >= 640 || shortSide >= 360) return '360p'
|
|
|
|
|
if (longSide >= 320 || shortSide >= 180) return '180p'
|
|
|
|
|
|
2026-10-01 13:27:50 +08:00
|
|
|
return `${shortSide}p`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (height > 0) {
|
|
|
|
|
return `${height}p`
|
|
|
|
|
}
|
|
|
|
|
if (width > 0) {
|
|
|
|
|
return `${width}p`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ''
|
|
|
|
|
}
|