image

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package image 提供反搜图片的统一原始读取入口:本地路径、HTTP(S) URL 与 stdin 流。只按 magic bytes 接受 JPEG/PNG/WEBP,保留原始字节,并在流式读取 中强制 8 MiB 上限;文件名与响应 Content-Type 只用于诊断,不作为信任依据。

Index

Constants

View Source
const (
	StageInput    = "input"
	StageDownload = "download"
	StageStatus   = "status"
	StageFormat   = "format"
	StageSize     = "size"
	StageCancel   = "cancel"
)

错误 stage,用于区分输入、下载、状态、格式、大小与取消错误;调用方不得 把失败回退成文本搜索。

View Source
const MaxSize = 8 << 20

MaxSize 是图片字节上限:恰好 8 MiB 合法,读到第 8 MiB 后的第一个字节 立即返回明确错误。

Variables

This section is empty.

Functions

func DetectHeaderSize

func DetectHeaderSize() int

DetectHeaderSize 返回识别全部支持格式所需的最小 magic 字节数(WEBP 12 字节)。

Types

type Error

type Error struct {
	Stage   string
	Code    string
	Message string
}

Error 是带稳定 stage/code 的图片错误。

func (*Error) Error

func (e *Error) Error() string

type Format

type Format int

Format 是支持的图片格式。

const (
	// Unknown 表示无法识别为 JPEG/PNG/WEBP。
	Unknown Format = iota
	// JPEG 对应 FF D8 FF magic。
	JPEG
	// PNG 对应 89 50 4E 47 0D 0A 1A 0A magic。
	PNG
	// WEBP 对应 RIFF....WEBP magic。
	WEBP
)

func DetectFormat

func DetectFormat(header []byte) Format

DetectFormat 按开头字节判定图片格式;至少需要对应格式的完整 magic。

func (Format) String

func (f Format) String() string

type Image

type Image struct {
	Bytes    []byte
	Format   Format
	Filename string
	SHA256   [32]byte
}

Image 是已验证的原始图片字节与派生元数据。

func ReadFile

func ReadFile(path string) (*Image, error)

ReadFile 读取本地普通文件并校验;路径必须是常规文件。

func ReadStream

func ReadStream(reader io.Reader, sourceName string) (*Image, error)

ReadStream 从任意 reader(stdin、内存等)流式读取并校验。

func ReadURL

func ReadURL(ctx context.Context, client *http.Client, rawURL string) (*Image, error)

ReadURL 下载并校验 HTTP(S) 图片;允许私网地址(SDK 文档提示服务端嵌入者 自行施加网络边界)。初始 URL 与每次重定向都重新校验 scheme,最终响应必须 是 2xx,body 流式受 8 MiB 限制。client 复用调用方注入的 transport(含全局 代理配置)。

Jump to

Keyboard shortcuts

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