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

anonymous

package
v0.14.4 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

Overview

Package anonymous 实现匿名取件 service(仿 vastsa/PigeonBox UX)。

核心流程:

  1. 上传方:上传文件后,调用 GenerateCode 获取 6 位取件码(建立 pickup_code → share_code 映射)
  2. 取件方:输入 6 位码 + 可选密码,按码取文件
  3. 系统:校验(DB 为准)→ 扣减次数(DB 原子)→ 返回下载信息

设计要点(DB 为唯一真相源):

  • Redis 仅存 pickup_code → share_code 映射 + 展示信息(文件名等)
  • 过期时间、剩余次数、密码哈希全部以 file_codes 表为准
  • 取件码字符表去掉易混淆字符 0/O/1/I/L

Index

Constants

This section is empty.

Variables

View Source
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 AnonymousShareParams

type AnonymousShareParams struct {
	FilePath       string
	FileName       string
	FileSize       int64
	ContentType    string
	ExpireAt       *time.Time // 必填,决定 DB 过期时间 + Redis TTL
	MaxPickupCount int        // -1=无限, 0=默认无限, >0=限制
	Password       string     // 空=无密码
}

AnonymousShareParams 匿名分享端到端参数

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 {
	ShareCode   string // 真实 file_code
	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) Cancel

func (s *Service) Cancel(ctx context.Context, code string) error

Cancel 作废取件码(删 Redis 映射)

func (*Service) CreateAnonymousShare

func (s *Service) CreateAnonymousShare(ctx context.Context, p AnonymousShareParams) (string, error)

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 记录过期时间对齐)。

func (*Service) Peek

func (s *Service) Peek(ctx context.Context, code string) (*CodeMeta, *model.FileCode, error)

Peek 按取件码查询分享信息(不扣次数、不校验密码),仅供展示。 返回展示信息 CodeMeta + DB 记录(含剩余次数、过期时间等)。

func (*Service) Retrieve

func (s *Service) Retrieve(ctx context.Context, code, password string) (*CodeMeta, error)

Retrieve 按取件码取件(校验 + 扣减次数,DB 为准)。 返回展示信息 CodeMeta。每次成功调用扣减一次剩余次数。

Jump to

Keyboard shortcuts

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