tus_client

package module
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2025 License: MulanPSL-2.0 Imports: 16 Imported by: 0

README

说明

Tus v1.0.0 协议客户端实现,参考自:tus resumable upload protocol

使用

go get gitee.com/ivfzhou/tus_client/v2@latest
client := tus_client.NewClient("your_host")

// 并行分片上传
fileId, err := client.MultipleUploadFromFile(ctx, "your_file_path")

// 下载
err := client.DownloadToFile(ctx, "fileId", "to_file_path")

Documentation

Index

Constants

View Source
const (
	Level_Debug = iota + 1
	Level_Info
	Level_Warning
	Level_Error
	Level_Silent
)

Variables

This section is empty.

Functions

This section is empty.

Types

type DeleteRequest

type DeleteRequest struct {
	// TusResumable 客户端使用Tus协议版本,默认1.0.0
	TusResumable string
	// Location 文件标识
	Location string
}

type DeleteResult

type DeleteResult struct {
	// HTTPStatus 响应码
	HTTPStatus int
	// TusResumable 服务器运行的Tus协议版本
	TusResumable string
}

type GetRequest

type GetRequest struct {
	// TusResumable 客户端使用Tus协议版本,默认1.0.0
	TusResumable string
	// Location 文件标识
	Location string
}

type GetResult

type GetResult struct {
	// HTTPStatus 响应码
	HTTPStatus int
	// TusResumable 服务器运行的Tus协议版本
	TusResumable string
	// Body 文件数据IO流
	Body io.ReadCloser
	// ContentLength 文件大小
	ContentLength int
}

type HeadRequest

type HeadRequest struct {
	// TusResumable 客户端使用Tus协议版本,默认1.0.0
	TusResumable string
	// Location 文件标识
	Location string
}

type HeadResult

type HeadResult struct {
	// HTTPStatus 响应码
	HTTPStatus int
	// TusResumable 服务器运行的Tus协议版本
	TusResumable string
	// UploadOffset 文件大小偏移量
	UploadOffset int
	// UploadLength 文件大小
	UploadLength int
	// UploadMetadata 文件元信息
	UploadMetadata map[string]string
	// UploadDeferLength 文件大小在完成上传后确定
	UploadDeferLength bool
	// UploadConcat 文件的分片信息
	UploadConcat []string
}

type Logger

type Logger interface {
	Printf(ctx context.Context, level int, format string, args ...any)
}

type Option

type Option func(*Options)

func WithChunkSize

func WithChunkSize(chunkSize int) Option

func WithHTTPClient

func WithHTTPClient(c *http.Client) Option

func WithLogLevel

func WithLogLevel(level int) Option

func WithLogger

func WithLogger(logger Logger) Option

func WithSchema

func WithSchema(schema string) Option

type Options

type Options struct {
	// contains filtered or unexported fields
}

type OptionsResult

type OptionsResult struct {
	// HTTPStatus 响应码
	HTTPStatus int
	// TusExtension 服务器支持的扩展
	TusExtension []string
	// TusResumable 服务器运行的Tus协议版本
	TusResumable string
	// TusVersion 服务器支持的协议版本
	TusVersion []string
	// TusMaxSize 服务器允许的单次最大文件上传大小
	TusMaxSize int
	// TusChecksumAlgorithm 服务器支持的文件校验和算法
	TusChecksumAlgorithm []string
}

type PatchByIORequest

type PatchByIORequest struct {
	// Location 文件标识
	Location string
	// TusResumable 客户端使用Tus协议版本,默认1.0.0
	TusResumable string
	// UploadOffset 文件大小偏移量
	UploadOffset int
	// Body 文件数据
	Body io.ReadCloser
	// BodySize 大小
	BodySize int
	// UploadChecksum 数据校验和
	UploadChecksum string
	// UploadChecksumAlgorithm 校验和算法
	UploadChecksumAlgorithm string
}

type PatchRequest

type PatchRequest struct {
	// Location 文件标识
	Location string
	// TusResumable 客户端使用Tus协议版本,默认1.0.0
	TusResumable string
	// UploadOffset 文件大小偏移量
	UploadOffset int
	// Body 文件数据
	Body []byte
	// UploadChecksum 数据校验和
	UploadChecksum string
	// UploadChecksumAlgorithm 校验和算法
	UploadChecksumAlgorithm string
}

type PatchResult

type PatchResult struct {
	// HTTPStatus 响应码
	HTTPStatus int
	// TusResumable 服务器运行的Tus协议版本
	TusResumable string
	// UploadOffset 文件大小偏移量
	UploadOffset int
	// UploadExpires 文件过期时间
	UploadExpires time.Time
}

type PostRequest

type PostRequest struct {
	// TusResumable 客户端使用Tus协议版本,默认1.0.0
	TusResumable string
	// UploadMetadata 文件元信息
	UploadMetadata map[string]string
	// UploadLength 文件上传大小
	UploadLength int
	// UploadDeferLength 文件大小在完成上传后确定
	UploadDeferLength bool
	// Body 文件数据
	Body []byte
	// UploadConcat 分片上传
	UploadConcat string
}

type PostResult

type PostResult struct {
	// HTTPStatus 响应码
	HTTPStatus int
	// TusResumable 服务器运行的Tus协议版本
	TusResumable string
	// Location 文件标识
	Location string
	// UploadOffset 文件大小偏移量
	UploadOffset int
}

type TusClient

type TusClient interface {
	// Options 发送 HTTP OPTIONS 请求
	Options(context.Context) (*OptionsResult, error)

	// Post 发送 HTTP POST 请求
	Post(context.Context, *PostRequest) (*PostResult, error)

	// Head 发送 HTTP HEAD 请求
	Head(context.Context, *HeadRequest) (*HeadResult, error)

	// Patch 发送 HTTP PATCH 请求
	Patch(context.Context, *PatchRequest) (*PatchResult, error)

	// Delete 发送 HTTP DELETE 请求
	Delete(context.Context, *DeleteRequest) (*DeleteResult, error)

	// Get 发送 HTTP GET 请求
	Get(context.Context, *GetRequest) (*GetResult, error)

	// MultipleUploadFromFile 读取文件并发上传到服务器,返回文件在服务器标识
	MultipleUploadFromFile(ctx context.Context, filePath string) (location string, err error)

	// MultipleUploadFromReader 读取 IO 流直到 EOF,并发上传到服务器,返回文件在服务器标识
	MultipleUploadFromReader(ctx context.Context, r io.Reader) (location string, err error)

	// DownloadToFile 从服务器下载文件到本地
	DownloadToFile(ctx context.Context, location, dest string) error

	// DownloadToWriter 从服务器下载文件到指定的 IO 流中
	DownloadToWriter(ctx context.Context, location string, w io.Writer) error

	// UploadPart 上传分片
	UploadPart(ctx context.Context, data []byte) (location string, err error)

	// MergeParts 合并分片,切片顺序决定顺序合并顺序
	MergeParts(ctx context.Context, parts []string) (location string, err error)

	// DiscardParts 丢弃上传的分片
	DiscardParts(ctx context.Context, parts []string) error

	// UploadPartByIO 上传分片
	UploadPartByIO(ctx context.Context, data io.ReadCloser, length int) (location string, err error)

	// PatchByIO 发送 HTTP PATCH 请求
	PatchByIO(ctx context.Context, pr *PatchByIORequest) (*PatchResult, error)

	// UploadFile 上传文件
	UploadFile(ctx context.Context, data []byte) (location string, err error)

	// DeleteFile 删除文件
	DeleteFile(ctx context.Context, location string) error
}

func NewClient

func NewClient(host string, opts ...Option) TusClient

Source Files

  • api.go
  • impl.go
  • logger.go
  • options.go

Jump to

Keyboard shortcuts

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