Help us shape the future of Go! Take the 2026 Go Developer Survey

preview

package
v0.32.41 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 31, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// MaxImagePixels 总像素上限(30M 像素 ≈ 解码后 120MB RGBA 峰值,覆盖所有常规摄影分辨率)
	MaxImagePixels int64 = 30_000_000
	// MaxImageDimension 单边上限(全景图 sanity,防极端长宽触发下游缩放算法异常)
	MaxImageDimension = 20000
)

解压炸弹守卫(2026-10-08 加固):恶意 PNG/JPEG 可用极小压缩体积声明超大 像素矩阵,image.Decode 按头声明分配 RGBA——50MB 压缩包可膨胀成数 GB 内存。 image.DecodeConfig 只读文件头即得宽高,超限直接拒绝,不做全量解码。

Variables

This section is empty.

Functions

func InitService

func InitService(cfg *Config) error

InitService 初始化预览服务

func SetService added in v0.32.24

func SetService(s *Service)

SetService 注入预览服务单例(装配期由 bootstrap.InitService 间接设置, 测试期可注入桩或置 nil 以覆盖「引擎不可用」路径)。

Types

type AudioGenerator

type AudioGenerator struct {
	// contains filtered or unexported fields
}

AudioGenerator 音频预览生成器

func NewAudioGenerator

func NewAudioGenerator(cfg *Config) *AudioGenerator

NewAudioGenerator 创建音频生成器

func (*AudioGenerator) Generate

func (g *AudioGenerator) Generate(ctx context.Context, filePath string, ext string) (*PreviewData, error)

Generate 生成音频预览

func (*AudioGenerator) GenerateThumbnail

func (g *AudioGenerator) GenerateThumbnail(ctx context.Context, filePath string, width, height int) (string, error)

GenerateThumbnail 生成音频波形图(可选)

func (*AudioGenerator) SupportedTypes

func (g *AudioGenerator) SupportedTypes() []string

SupportedTypes 支持的文件类型

type AudioInfo

type AudioInfo struct {
	Duration   int // 秒
	BitRate    int
	SampleRate int
}

AudioInfo 音频信息

type CodeGenerator

type CodeGenerator struct {
	// contains filtered or unexported fields
}

CodeGenerator 代码预览生成器

func NewCodeGenerator

func NewCodeGenerator(cfg *Config) *CodeGenerator

NewCodeGenerator 创建代码生成器

func (*CodeGenerator) Generate

func (g *CodeGenerator) Generate(ctx context.Context, filePath string, ext string) (*PreviewData, error)

Generate 生成代码预览

func (*CodeGenerator) GenerateThumbnail

func (g *CodeGenerator) GenerateThumbnail(ctx context.Context, filePath string, width, height int) (string, error)

GenerateThumbnail 代码不需要缩略图

func (*CodeGenerator) GetLanguage

func (g *CodeGenerator) GetLanguage(ext string) string

GetLanguage 根据扩展名获取语言类型(用于前端高亮)

func (*CodeGenerator) SupportedTypes

func (g *CodeGenerator) SupportedTypes() []string

SupportedTypes 支持的文件类型

type Config

type Config struct {
	EnablePreview    bool   `mapstructure:"enable_preview"`
	ThumbnailWidth   int    `mapstructure:"thumbnail_width"`
	ThumbnailHeight  int    `mapstructure:"thumbnail_height"`
	MaxFileSize      int64  `mapstructure:"max_preview_size"` // 最大预览文件大小
	PreviewCachePath string `mapstructure:"preview_cache_path"`
	LibreOfficePath  string `mapstructure:"libreoffice_path"` // LibreOffice可执行文件路径
	FFmpegPath       string `mapstructure:"ffmpeg_path"`      // FFmpeg可执行文件路径
}

Config 预览配置

type Generator

type Generator interface {
	// Generate 生成预览
	Generate(ctx context.Context, filePath string, ext string) (*PreviewData, error)

	// SupportedTypes 支持的文件类型
	SupportedTypes() []string

	// GenerateThumbnail 生成缩略图
	GenerateThumbnail(ctx context.Context, filePath string, width, height int) (string, error)
}

Generator 预览生成器接口

type ImageGenerator

type ImageGenerator struct {
	// contains filtered or unexported fields
}

ImageGenerator 图片预览生成器

func NewImageGenerator

func NewImageGenerator(cfg *Config) *ImageGenerator

NewImageGenerator 创建图片生成器

func (*ImageGenerator) Generate

func (g *ImageGenerator) Generate(ctx context.Context, filePath string, ext string) (*PreviewData, error)

Generate 生成图片预览

func (*ImageGenerator) GenerateThumbnail

func (g *ImageGenerator) GenerateThumbnail(ctx context.Context, filePath string, targetWidth, targetHeight int) (string, error)

GenerateThumbnail 生成缩略图(Generator 接口方法;独立调用路径同样走守卫解码)

func (*ImageGenerator) SupportedTypes

func (g *ImageGenerator) SupportedTypes() []string

SupportedTypes 支持的文件类型

type PreviewData

type PreviewData struct {
	Type        PreviewType `json:"type"`         // 预览类型
	Thumbnail   string      `json:"thumbnail"`    // 缩略图URL
	PreviewURL  string      `json:"preview_url"`  // 预览URL
	Width       int         `json:"width"`        // 宽度(图片/视频)
	Height      int         `json:"height"`       // 高度(图片/视频)
	Duration    int         `json:"duration"`     // 时长(音视频,秒)
	PageCount   int         `json:"page_count"`   // 页数(PDF/Office)
	FileSize    int64       `json:"file_size"`    // 文件大小
	MimeType    string      `json:"mime_type"`    // MIME类型
	Extension   string      `json:"extension"`    // 文件扩展名
	TextContent string      `json:"text_content"` // 文本内容(代码预览)
}

PreviewData 预览数据

type PreviewType

type PreviewType string

PreviewType 预览类型

const (
	PreviewTypeImage   PreviewType = "image"   // 图片
	PreviewTypePDF     PreviewType = "pdf"     // PDF
	PreviewTypeVideo   PreviewType = "video"   // 视频
	PreviewTypeAudio   PreviewType = "audio"   // 音频
	PreviewTypeOffice  PreviewType = "office"  // Office文档
	PreviewTypeCode    PreviewType = "code"    // 代码
	PreviewTypeText    PreviewType = "text"    // 纯文本
	PreviewTypeArchive PreviewType = "archive" // 压缩包
	PreviewTypeUnknown PreviewType = "unknown" // 未知类型
)

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service 预览服务

func GetService

func GetService() *Service

GetService 获取预览服务实例。

2026-10-28(真机实证:fnOS 4.0.7,每次启动报 "failed to create preview cache directory: mkdir ./data/previews: permission denied"):此前 svc==nil 时会 **静默**用默认配置(缓存目录 "./data/previews",按进程 CWD 解析)重试初始化—— 重试在 NAS 包/容器/systemd 部署下必然失败(缓存目录不可建),于是调用方拿到 nil 却毫无线索;更糟的是 MaxFileSize() 的 nil 接收者分支返回 0(=不限), 把 2026-10-08 加固的「预览大小上限」静默关掉 ⇒ 任意大文件仍会全量落临时盘 (磁盘耗尽 DoS 面回归)。

改为:不再自动重试;未注入即返回 nil,由调用方 fail-fast 报明确错误 (与仓内 getShareService() 对残废实例的处理同构)。

func NewService

func NewService(cfg *Config) *Service

NewService 创建预览服务

func (*Service) GeneratePreview

func (s *Service) GeneratePreview(ctx context.Context, filePath string, ext string) (*PreviewData, error)

GeneratePreview 生成预览

func (*Service) MaxFileSize added in v0.14.9

func (s *Service) MaxFileSize() int64

MaxFileSize 返回预览大小上限(字节,0=不限)。调用方应在打开对象前用 DB 记录的文件大小预判——否则任意大文件会先被全量拉到临时盘再由生成器 拒绝,构成磁盘耗尽攻击面(2026-10-08 加固)。

func (*Service) RegisterGenerator

func (s *Service) RegisterGenerator(previewType PreviewType, generator Generator)

RegisterGenerator 注册生成器

type VideoGenerator

type VideoGenerator struct {
	// contains filtered or unexported fields
}

VideoGenerator 视频预览生成器

func NewVideoGenerator

func NewVideoGenerator(cfg *Config) *VideoGenerator

NewVideoGenerator 创建视频生成器

func (*VideoGenerator) Generate

func (g *VideoGenerator) Generate(ctx context.Context, filePath string, ext string) (*PreviewData, error)

Generate 生成视频预览

func (*VideoGenerator) GenerateThumbnail

func (g *VideoGenerator) GenerateThumbnail(ctx context.Context, filePath string, targetWidth, targetHeight int) (string, error)

GenerateThumbnail 生成视频缩略图

func (*VideoGenerator) SupportedTypes

func (g *VideoGenerator) SupportedTypes() []string

SupportedTypes 支持的文件类型

type VideoInfo

type VideoInfo struct {
	Width    int
	Height   int
	Duration int // 秒
}

VideoInfo 视频信息

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL