Documentation
¶
Overview ¶
Package anonymous 实现匿名取件 service(仿 vastsa/PigeonBox UX)。
核心流程:
- 上传方:上传文件后,调用 GenerateCode 获取 6 位取件码(建立 pickup_code → share_code 映射)
- 取件方:输入 6 位码 + 可选密码,按码取文件
- 系统:校验(DB 为准)→ 扣减次数(DB 原子)→ 返回下载信息
设计要点(DB 为唯一真相源):
- Redis 仅存 pickup_code → share_code 映射 + 展示信息(文件名等)
- 过期时间、剩余次数、密码哈希全部以 file_codes 表为准
- 取件码字符表去掉易混淆字符 0/O/1/I/L
Index ¶
- Variables
- type AnonymousShareParams
- type BlockedError
- type CodeMeta
- type Service
- func (s *Service) Cancel(ctx context.Context, code string) error
- func (s *Service) CreateAnonymousShare(ctx context.Context, p AnonymousShareParams) (string, error)
- func (s *Service) GenerateCode(ctx context.Context, meta CodeMeta, expireAt time.Time) (string, error)
- func (s *Service) Peek(ctx context.Context, code string) (*CodeMeta, *model.FileCode, error)
- func (s *Service) Retrieve(ctx context.Context, code, password string) (*CodeMeta, error)
Constants ¶
This section is empty.
Variables ¶
var ( ErrCodeNotFound = errors.New("pickup code not found") ErrCodeExpired = errors.New("pickup code expired") ErrCodeExhausted = errors.New("pickup code exhausted") ErrPasswordWrong = errors.New("password wrong") ErrNotReady = errors.New("share upload incomplete") // 登记占位未回填物理文件 )
错误哨兵
Functions ¶
This section is empty.
Types ¶
type BlockedError ¶
type BlockedError struct{ Status string }
BlockedError 分享处于管控拒绝态(治理状态机)。 handler 侧 errors.As 后按 ErrCode 透传(20012 blocked / 20013 pending_review)。
func (*BlockedError) ErrCode ¶
func (e *BlockedError) ErrCode() int
func (*BlockedError) Error ¶
func (e *BlockedError) Error() string
type CodeMeta ¶
type CodeMeta struct {
FileName string
FileSize int64
ContentType string
RequireAuth bool // 仅展示"是否需要密码"
}
CodeMeta 取件码展示信息(仅存于 Redis,真实状态查 DB)
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service 匿名取件 service。 Redis 仅存映射 + 展示信息;过期/次数/密码等真实状态全部以 file_codes 表为准。
func NewService ¶
func NewService(rdb *redis.Client, fileCodeRepo *dao.FileCodeRepository) *Service
NewService 创建 service。fileCodeRepo 为 nil 时内部自建(Retrieve 需查 DB)。 rdb nil 归一化:typed-nil 接口会骗过"Redis 可用"守卫(!= nil 判真)。 rdb 为 nil(redis.host 未配置)时进入单机内存模式:映射/展示信息存进程内 TTL KV,匿名取件全功能可用(单进程语义等价;重启丢失、不跨副本共享)。
func (*Service) CreateAnonymousShare ¶
CreateAnonymousShare 端到端:建 file_codes 记录 + 生成取件码。 供 gen handler 调用,避免 handler 直接操作 DAO;密码在此 bcrypt 哈希后存 DB。 返回 6 位取件码。
func (*Service) GenerateCode ¶
func (s *Service) GenerateCode(ctx context.Context, meta CodeMeta, expireAt time.Time) (string, error)
GenerateCode 生成 6 位取件码,建立 pickup_code → share_code 映射。 expireAt 决定 Redis key 的 TTL(应与 DB 记录过期时间对齐)。