spark-server/internal/pkg/media/limits.go
DESKTOP-L8OUL9F\gfl0034 715ace7ac6 feat(square): 广场 Bot/RSS 发布与媒体校验
支持广场机器人发帖与媒体类型检查,并补充相关迁移与 i18n。
2026-09-24 23:07:06 +08:00

78 lines
2.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Package media holds shared media size/resolution policy (no I/O).
// Client App compresses user uploads; bots must compress server-side via infrastructure/media.
package media
// Default limits aligned with App frontend (image ≤1080p, video ≤720p ≤100MB, avatar ≤200×200).
const (
DefaultImageMaxEdge = 1080
DefaultVideoMaxEdge = 720
DefaultAvatarMaxEdge = 200
DefaultImageMaxBytes = 10 << 20 // 10 MiB (square/moment image purpose)
DefaultVideoMaxBytes = 100 << 20 // 100 MiB
DefaultAvatarMaxBytes = 2 << 20 // 2 MiB (upload purpose hard cap)
DefaultJPEGQuality = 85
DefaultVideoCRF = 23
)
// Limits is the resolution/size policy shared by user checks and bot compress.
type Limits struct {
ImageMaxEdge int
VideoMaxEdge int
AvatarMaxEdge int
ImageMaxBytes int64
VideoMaxBytes int64
AvatarMaxBytes int64
}
// DefaultLimits returns client-aligned defaults.
func DefaultLimits() Limits {
return Limits{
ImageMaxEdge: DefaultImageMaxEdge,
VideoMaxEdge: DefaultVideoMaxEdge,
AvatarMaxEdge: DefaultAvatarMaxEdge,
ImageMaxBytes: DefaultImageMaxBytes,
VideoMaxBytes: DefaultVideoMaxBytes,
AvatarMaxBytes: DefaultAvatarMaxBytes,
}
}
// Normalize fills zero fields with defaults.
func (l Limits) Normalize() Limits {
d := DefaultLimits()
if l.ImageMaxEdge <= 0 {
l.ImageMaxEdge = d.ImageMaxEdge
}
if l.VideoMaxEdge <= 0 {
l.VideoMaxEdge = d.VideoMaxEdge
}
if l.AvatarMaxEdge <= 0 {
l.AvatarMaxEdge = d.AvatarMaxEdge
}
if l.ImageMaxBytes <= 0 {
l.ImageMaxBytes = d.ImageMaxBytes
}
if l.VideoMaxBytes <= 0 {
l.VideoMaxBytes = d.VideoMaxBytes
}
if l.AvatarMaxBytes <= 0 {
l.AvatarMaxBytes = d.AvatarMaxBytes
}
return l
}
// LongestEdge returns max(w, h).
func LongestEdge(w, h int) int {
if w > h {
return w
}
return h
}
// NeedsResize reports whether the frame exceeds maxEdge (already-smaller is kept as-is).
func NeedsResize(w, h, maxEdge int) bool {
if maxEdge <= 0 || w <= 0 || h <= 0 {
return false
}
return LongestEdge(w, h) > maxEdge
}