common

package
v4.2.6 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2025 License: AGPL-3.0 Imports: 25 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var SecretKey []byte

SecretKey 用于JWT签名的密钥

Functions

func CanAccess

func CanAccess(user *model.User, meta *model.Meta, reqPath string, password string) bool

CanAccess 检查用户是否可以访问指定路径

参数:

  • user: 用户对象
  • meta: 元数据对象
  • reqPath: 请求路径
  • password: 访问密码

返回:

  • bool: 如果用户可以访问返回true,否则返回false

func CanWrite

func CanWrite(meta *model.Meta, path string) bool

CanWrite 检查指定元数据和路径是否具有写入权限

参数:

  • meta: 元数据对象
  • path: 请求路径

返回:

  • bool: 如果有写入权限返回true,否则返回false

func ContentWithValue added in v4.2.3

func ContentWithValue(ctx context.Context, keyAndValue ...any) context.Context

func ErrorResp

func ErrorResp(c *gin.Context, err error, code int, l ...bool)

ErrorResp 返回错误响应 封装了ErrorWithDataResp函数,不包含额外数据

参数:

  • c: Gin上下文
  • err: 错误信息
  • code: 错误码
  • l: 可选参数,是否记录日志,默认为不记录

func ErrorStrResp

func ErrorStrResp(c *gin.Context, str string, code int, l ...bool)

ErrorStrResp 返回字符串错误响应 与ErrorResp类似,但接受字符串而非error类型

参数:

  • c: Gin上下文
  • str: 错误消息字符串
  • code: 错误码
  • l: 可选参数,是否记录日志,默认为不记录

func ErrorWithDataResp

func ErrorWithDataResp(c *gin.Context, err error, code int, data any, l ...bool)

ErrorWithDataResp 返回带数据的错误响应 如果指定记录日志,会根据调试模式决定日志详细程度

参数:

  • c: Gin上下文
  • err: 错误信息
  • code: 错误码
  • data: 额外数据
  • l: 可选参数,是否记录日志,默认为不记录

func GenerateDownProxyURL added in v4.2.6

func GenerateDownProxyURL(storage *model.Storage, reqPath string) string

GenerateDownProxyURL 生成下载代理URL 如果存储配置了下载代理URL,则生成代理URL

参数:

  • storage: 存储对象
  • reqPath: 请求路径

返回:

  • string: 生成的代理URL

func GenerateToken

func GenerateToken(user *model.User) (tokenString string, err error)

GenerateToken 为指定用户生成JWT令牌

参数:

  • user: 用户对象

返回:

  • tokenString: 生成的令牌字符串
  • err: 错误信息

func GetApiURL added in v4.2.5

func GetApiURL(ctx context.Context) string

func GetApiURLFromRequest added in v4.2.5

func GetApiURLFromRequest(r *http.Request) string

GetApiURLFromRequest 根据请求信息生成API的完整URL 如果配置中的SiteURL已经是完整URL,则直接使用 否则根据请求信息构建完整URL 参数:

  • r: HTTP请求,可以为nil

返回:

  • 完整的API URL,不带尾部斜杠

func GetEtag

func GetEtag(file model.Obj, size int64) string

GetEtag 获取文件的ETag值 优先使用文件哈希,如果没有则使用修改时间和大小组合

参数:

  • file: 文件对象
  • size: 文件大小

返回:

  • string: ETag值

func GinWithValue added in v4.2.3

func GinWithValue(c *gin.Context, keyAndValue ...any)

func InvalidateToken

func InvalidateToken(tokenString string) error

InvalidateToken 使令牌失效

参数:

  • tokenString: 令牌字符串

返回:

  • error: 错误信息

func IsApply

func IsApply(metaPath, reqPath string, applySub bool) bool

IsApply 检查元数据是否应用于请求路径

参数:

  • metaPath: 元数据路径
  • reqPath: 请求路径
  • applySub: 是否应用于子路径

返回:

  • bool: 如果元数据应用于请求路径返回true,否则返回false

func IsStorageSignEnabled

func IsStorageSignEnabled(rawPath string) bool

IsStorageSignEnabled 检查指定路径的存储是否启用了签名

参数:

  • rawPath: 原始路径

返回:

  • bool: 如果存储启用了签名返回true,否则返回false

func IsTokenInvalidated

func IsTokenInvalidated(tokenString string) bool

IsTokenInvalidated 检查令牌是否已被注销

参数:

  • tokenString: 令牌字符串

返回:

  • bool: 如果令牌已被注销返回true,否则返回false

func Pluralize

func Pluralize(count int, singular, plural string) string

Pluralize 根据数量返回单数或复数形式的字符串

参数:

  • count: 数量
  • singular: 单数形式
  • plural: 复数形式

返回:

  • 根据count选择的字符串

func Proxy

func Proxy(w http.ResponseWriter, r *http.Request, link *model.Link, file model.Obj) error

Proxy 处理文件代理请求 根据link中的不同信息,采用不同的代理方式: 1. 使用MFile直接提供文件 2. 使用RangeReadCloser处理范围请求 3. 使用分块下载处理大文件 4. 使用透明代理转发请求

参数:

  • w: HTTP响应写入器
  • r: HTTP请求
  • link: 链接信息
  • file: 文件对象

返回:

  • error: 错误信息

func ProxyRange

func ProxyRange(ctx context.Context, link *model.Link, size int64) *model.Link

ProxyRange 为链接设置范围读取器 如果链接已经有MFile,则不需要设置 如果链接的RangeReadCloser为NoProxyRange,则设置为nil

参数:

  • link: 链接对象
  • size: 文件大小

返回:

  • *model.Link: 修改后的链接对象

func ShouldProxy

func ShouldProxy(storage driver.Driver, filename string) bool

ShouldProxy 判断是否应该代理指定的文件 代理条件: 1. 存储配置必须代理 2. 存储启用了Web代理 3. 文件扩展名在代理类型列表中

参数:

  • storage: 存储驱动
  • filename: 文件名

返回:

  • bool: 如果应该代理返回true,否则返回false

func Sign

func Sign(obj model.Obj, parent string, encrypt bool) string

Sign 为对象生成签名 目录对象不生成签名 非加密对象,在未开启全局签名的情况下不生成签名

参数:

  • obj: 需要签名的对象
  • parent: 父路径
  • encrypt: 是否是加密对象

返回:

  • 签名字符串,如果不需要签名则返回空字符串

func SuccessResp

func SuccessResp(c *gin.Context, data ...any)

SuccessResp 返回成功响应 默认消息为"success"

参数:

  • c: Gin上下文
  • data: 可选参数,响应数据

func SuccessWithMsgResp

func SuccessWithMsgResp(c *gin.Context, msg string, data ...any)

SuccessWithMsgResp 返回带自定义消息的成功响应

参数:

  • c: Gin上下文
  • msg: 成功消息
  • data: 可选参数,响应数据

Types

type InterceptResponseWriter

type InterceptResponseWriter struct {
	http.ResponseWriter
	io.Writer
}

InterceptResponseWriter 拦截响应写入的ResponseWriter 允许将响应内容写入到指定的Writer

func (*InterceptResponseWriter) Write

func (iw *InterceptResponseWriter) Write(p []byte) (int, error)

Write 实现ResponseWriter接口的Write方法 将内容写入到指定的Writer而非原始ResponseWriter

参数:

  • p: 要写入的字节切片

返回:

  • int: 写入的字节数
  • error: 错误信息

type PageResp

type PageResp struct {
	Content any   `json:"content"` // 分页内容
	Total   int64 `json:"total"`   // 总记录数
}

PageResp 分页响应结构体,用于返回分页数据

type Resp

type Resp[T any] struct {
	Code    int    `json:"code"`    // 状态码,200表示成功
	Message string `json:"message"` // 响应消息
	Data    T      `json:"data"`    // 响应数据
}

Resp 通用响应结构体,用于API返回数据 使用泛型支持不同类型的数据 参数:

  • T: 数据类型参数,可以是任意类型

type UserClaims

type UserClaims struct {
	Username string `json:"username"` // 用户名
	PwdTS    int64  `json:"pwd_ts"`   // 密码时间戳(用于在密码更改时使token失效)
	jwt.RegisteredClaims
}

UserClaims JWT中包含的用户信息

func ParseToken

func ParseToken(tokenString string) (*UserClaims, error)

ParseToken 解析JWT令牌

参数:

  • tokenString: 令牌字符串

返回:

  • *UserClaims: 解析出的用户声明
  • error: 错误信息

type WrittenResponseWriter

type WrittenResponseWriter struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

WrittenResponseWriter 跟踪是否已经写入内容的ResponseWriter

func (*WrittenResponseWriter) IsWritten

func (ww *WrittenResponseWriter) IsWritten() bool

IsWritten 检查是否已经写入内容

返回:

  • bool: 如果已经写入内容返回true,否则返回false

func (*WrittenResponseWriter) Write

func (ww *WrittenResponseWriter) Write(p []byte) (int, error)

Write 实现ResponseWriter接口的Write方法 跟踪是否已经写入内容

参数:

  • p: 要写入的字节切片

返回:

  • int: 写入的字节数
  • error: 错误信息

Jump to

Keyboard shortcuts

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