uploader

package
v0.0.0-...-7f85f33 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2022 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Overview

Copyright (c) 2020 tickstep.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright (c) 2020 tickstep.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright (c) 2020 tickstep.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright (c) 2020 tickstep.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright (c) 2020 tickstep.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright (c) 2020 tickstep.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright (c) 2020 tickstep.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright (c) 2020 tickstep.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

View Source
const (
	// BufioReadSize bufio 缓冲区大小, 用于上传时读取文件
	BufioReadSize = int(64 * converter.KB) // 64KB
)

Variables

This section is empty.

Functions

This section is empty.

Types

type BlockState

type BlockState struct {
	ID         int            `json:"id"`
	Range      transfer.Range `json:"range"`
	UploadDone bool           `json:"upload_done"`
}

BlockState 文件区块信息

func SplitBlock

func SplitBlock(fileSize, blockSize int64) (blockList []*BlockState)

SplitBlock 文件分块

type CheckFunc

type CheckFunc func(resp *http.Response, uploadErr error)

CheckFunc 上传完成的检测函数

type InstanceState

type InstanceState struct {
	BlockList []*BlockState `json:"block_list"`
}

InstanceState 上传断点续传信息

type MultiError

type MultiError struct {
	Err error
	// IsRetry 是否重试,
	Terminated bool
}

MultiError 多线程上传的错误

func (*MultiError) Error

func (me *MultiError) Error() string

type MultiUpload

type MultiUpload interface {
	Precreate() (perr error)
	UploadFile(ctx context.Context, partseq int, partOffset int64, partEnd int64, readerlen64 rio.ReaderLen64) (uploadDone bool, terr error)
	CommitFile() (cerr error)
}

MultiUpload 支持多线程的上传, 可用于断点续传

type MultiUploader

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

MultiUploader 多线程上传

func NewMultiUploader

func NewMultiUploader(uploadUrl, commitUrl, uploadFileId, xRequestId string, multiUpload MultiUpload, file rio.ReaderAtLen64, config *MultiUploaderConfig) *MultiUploader

NewMultiUploader 初始化上传

func (*MultiUploader) Cancel

func (muer *MultiUploader) Cancel()

Cancel 取消上传

func (*MultiUploader) Execute

func (muer *MultiUploader) Execute()

Execute 执行上传

func (*MultiUploader) InstanceState

func (muer *MultiUploader) InstanceState() *InstanceState

InstanceState 返回断点续传信息

func (*MultiUploader) OnCancel

func (muer *MultiUploader) OnCancel(onCancelEvent requester.Event)

OnCancel 设置取消上传事件

func (*MultiUploader) OnError

func (muer *MultiUploader) OnError(onErrorEvent requester.EventOnError)

OnError 设置上传发生错误事件

func (*MultiUploader) OnExecute

func (muer *MultiUploader) OnExecute(onExecuteEvent requester.Event)

OnExecute 设置开始上传事件

func (*MultiUploader) OnFinish

func (muer *MultiUploader) OnFinish(onFinishEvent requester.Event)

OnFinish 设置结束上传事件

func (*MultiUploader) OnSuccess

func (muer *MultiUploader) OnSuccess(onSuccessEvent requester.Event)

OnSuccess 设置成功上传事件

func (*MultiUploader) OnUploadStatusEvent

func (muer *MultiUploader) OnUploadStatusEvent(f UploadStatusFunc)

OnUploadStatusEvent 设置上传状态事件

func (*MultiUploader) SetInstanceState

func (muer *MultiUploader) SetInstanceState(is *InstanceState)

SetInstanceState 设置InstanceState, 断点续传信息

type MultiUploaderConfig

type MultiUploaderConfig struct {
	Parallel  int   // 上传并发量
	BlockSize int64 // 上传分块
	MaxRate   int64 // 限制最大上传速度
}

MultiUploaderConfig 多线程上传配置

type Readed64

type Readed64 interface {
	rio.ReaderLen64
	Readed() int64
}

Readed64 增加获取已读取数据量, 用于统计速度

func NewReaded64

func NewReaded64(rl rio.ReaderLen64) Readed64

NewReaded64 实现Readed64接口

type SplitUnit

type SplitUnit interface {
	Readed64
	io.Seeker
	Range() transfer.Range
	Left() int64
}

SplitUnit 将 io.ReaderAt 分割单元

func NewBufioSplitUnit

func NewBufioSplitUnit(readerAt io.ReaderAt, readRange transfer.Range, speedsStat *speeds.Speeds, rateLimit *speeds.RateLimit) SplitUnit

NewBufioSplitUnit io.ReaderAt实现SplitUnit接口, 有Buffer支持

type Status

type Status interface {
	TotalSize() int64           // 总大小
	Uploaded() int64            // 已上传数据
	SpeedsPerSecond() int64     // 每秒的上传速度
	TimeElapsed() time.Duration // 上传时间
}

Status 上传状态接口

type UploadStatus

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

UploadStatus 上传状态

func (*UploadStatus) SpeedsPerSecond

func (us *UploadStatus) SpeedsPerSecond() int64

SpeedsPerSecond 返回每秒的上传速度

func (*UploadStatus) TimeElapsed

func (us *UploadStatus) TimeElapsed() time.Duration

TimeElapsed 返回上传时间

func (*UploadStatus) TotalSize

func (us *UploadStatus) TotalSize() int64

TotalSize 返回总大小

func (*UploadStatus) Uploaded

func (us *UploadStatus) Uploaded() int64

Uploaded 返回已上传数据

type UploadStatusFunc

type UploadStatusFunc func(status Status, updateChan <-chan struct{})

type Uploader

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

Uploader 上传

func NewUploader

func NewUploader(url string, readerlen64 rio.ReaderLen64) (uploader *Uploader)

NewUploader 返回 uploader 对象, url: 上传地址, readerlen64: 实现 rio.ReaderLen64 接口的对象, 例如文件

func (*Uploader) Execute

func (u *Uploader) Execute()

Execute 执行上传, 收到返回值信号则为上传结束

func (*Uploader) GetStatusChan

func (u *Uploader) GetStatusChan() <-chan Status

GetStatusChan 获取上传状态

func (*Uploader) OnExecute

func (u *Uploader) OnExecute(fn func())

OnExecute 任务开始时触发的事件

func (*Uploader) OnFinish

func (u *Uploader) OnFinish(fn func())

OnFinish 任务完成时触发的事件

func (*Uploader) SetCheckFunc

func (u *Uploader) SetCheckFunc(checkFunc CheckFunc)

SetCheckFunc 设置上传完成的检测函数

func (*Uploader) SetClient

func (u *Uploader) SetClient(c *requester.HTTPClient)

SetClient 设置http客户端

func (*Uploader) SetContentType

func (u *Uploader) SetContentType(contentType string)

SetContentType 设置Content-Type

Jump to

Keyboard shortcuts

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