add huibo ivyz4y27
This commit is contained in:
27
internal/shared/pdfvalidate/pdfvalidate.go
Normal file
27
internal/shared/pdfvalidate/pdfvalidate.go
Normal file
@@ -0,0 +1,27 @@
|
||||
// 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
|
||||
}
|
||||
Reference in New Issue
Block a user