Files
tyapi-server/internal/shared/pdfvalidate/pdfvalidate.go
2026-04-28 12:25:41 +08:00

28 lines
882 B
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 pdfvalidate 对「已解码的 PDF 二进制」做格式与尺寸校验(与 multipart 发往数据源的字节一致)
package pdfvalidate
import (
"bytes"
"errors"
"fmt"
)
// MaxAuthorizePDFBytes 授权类 PDF 大小上限(与汇博等对接约定一致)
const MaxAuthorizePDFBytes = 500 * 1024
var pdfMagic = []byte("%PDF-")
// ValidateDecodedPDFBinary 仅校验已通过 Base64 解码得到的原始字节非空、长度、PDF 魔数头部。
func ValidateDecodedPDFBinary(raw []byte) error {
if len(raw) == 0 {
return errors.New("授权书文件不能为空")
}
if len(raw) > MaxAuthorizePDFBytes {
return fmt.Errorf("授权书文件不能超过500KB当前大小: %d字节", len(raw))
}
if len(raw) < len(pdfMagic) || !bytes.Equal(raw[:len(pdfMagic)], pdfMagic) {
return errors.New("授权书文件必须为PDF格式")
}
return nil
}