Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alist-org
GitHub Repository: alist-org/alist
Path: blob/main/internal/driver/utils.go
1560 views
1
package driver
2
3
import (
4
"context"
5
"github.com/alist-org/alist/v3/internal/model"
6
"github.com/alist-org/alist/v3/internal/stream"
7
"io"
8
)
9
10
type UpdateProgress = model.UpdateProgress
11
12
type Progress struct {
13
Total int64
14
Done int64
15
up UpdateProgress
16
}
17
18
func (p *Progress) Write(b []byte) (n int, err error) {
19
n = len(b)
20
p.Done += int64(n)
21
p.up(float64(p.Done) / float64(p.Total) * 100)
22
return
23
}
24
25
func NewProgress(total int64, up UpdateProgress) *Progress {
26
return &Progress{
27
Total: total,
28
up: up,
29
}
30
}
31
32
type RateLimitReader = stream.RateLimitReader
33
34
type RateLimitWriter = stream.RateLimitWriter
35
36
type RateLimitFile = stream.RateLimitFile
37
38
func NewLimitedUploadStream(ctx context.Context, r io.Reader) *RateLimitReader {
39
return &RateLimitReader{
40
Reader: r,
41
Limiter: stream.ServerUploadLimit,
42
Ctx: ctx,
43
}
44
}
45
46
func NewLimitedUploadFile(ctx context.Context, f model.File) *RateLimitFile {
47
return &RateLimitFile{
48
File: f,
49
Limiter: stream.ServerUploadLimit,
50
Ctx: ctx,
51
}
52
}
53
54
func ServerUploadLimitWaitN(ctx context.Context, n int) error {
55
return stream.ServerUploadLimit.WaitN(ctx, n)
56
}
57
58
type ReaderWithCtx = stream.ReaderWithCtx
59
60
type ReaderUpdatingProgress = stream.ReaderUpdatingProgress
61
62
type SimpleReaderWithSize = stream.SimpleReaderWithSize
63
64