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

preview

package
v0.14.5 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InitService

func InitService(cfg *Config) error

InitService 初始化预览服务

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 生成缩略图

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 获取预览服务实例

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) 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