From 6697fb7d36b3bce6ab4ceb0aa5640b71637c76e9 Mon Sep 17 00:00:00 2001 From: Sydonian <794346190@qq.com> Date: Thu, 11 Apr 2024 17:15:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=8C=85=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 2 +- pkgs/actor/actor.go | 6 +++--- pkgs/event/executor.go | 6 +++--- pkgs/ipfs/ipfs.go | 8 ++++---- pkgs/iterator/fuse.go | 18 ++++++++++++++++++ sdks/storage/object.go | 2 ++ sdks/storage/utils.go | 4 ++-- utils/http/http.go | 8 ++++++-- utils/{io => io2}/binary.go | 2 +- utils/{io => io2}/chunked_split.go | 2 +- utils/{io => io2}/chunked_split_test.go | 2 +- utils/{io => io2}/clone.go | 2 +- utils/{io => io2}/io.go | 2 +- utils/{io => io2}/io_test.go | 2 +- utils/{io => io2}/join.go | 6 +++--- utils/{io => io2}/length.go | 6 +++--- utils/{io => io2}/zero.go | 2 +- utils/{math => math2}/math.go | 2 +- utils/{sync => sync2}/counter_cond.go | 2 +- utils/sync2/sync2.go | 20 ++++++++++++++++++++ 20 files changed, 74 insertions(+), 30 deletions(-) create mode 100644 pkgs/iterator/fuse.go rename utils/{io => io2}/binary.go (99%) rename utils/{io => io2}/chunked_split.go (99%) rename utils/{io => io2}/chunked_split_test.go (99%) rename utils/{io => io2}/clone.go (98%) rename utils/{io => io2}/io.go (99%) rename utils/{io => io2}/io_test.go (99%) rename utils/{io => io2}/join.go (92%) rename utils/{io => io2}/length.go (88%) rename utils/{io => io2}/zero.go (95%) rename utils/{math => math2}/math.go (93%) rename utils/{sync => sync2}/counter_cond.go (97%) create mode 100644 utils/sync2/sync2.go diff --git a/go.mod b/go.mod index c8621aa..8207adb 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( github.com/json-iterator/go v1.1.12 github.com/magefile/mage v1.15.0 github.com/mitchellh/mapstructure v1.5.0 + github.com/modern-go/reflect2 v1.0.2 github.com/otiai10/copy v1.12.0 github.com/samber/lo v1.36.0 github.com/sirupsen/logrus v1.9.2 @@ -41,7 +42,6 @@ require ( github.com/minio/sha256-simd v1.0.0 // indirect github.com/mitchellh/go-homedir v1.1.0 // indirect github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect - github.com/modern-go/reflect2 v1.0.2 // indirect github.com/mr-tron/base58 v1.2.0 // indirect github.com/multiformats/go-base32 v0.1.0 // indirect github.com/multiformats/go-base36 v0.2.0 // indirect diff --git a/pkgs/actor/actor.go b/pkgs/actor/actor.go index 08e6f93..2df0b71 100644 --- a/pkgs/actor/actor.go +++ b/pkgs/actor/actor.go @@ -8,7 +8,7 @@ import ( "github.com/zyedidia/generic/list" "gitlink.org.cn/cloudream/common/pkgs/future" - mysync "gitlink.org.cn/cloudream/common/utils/sync" + "gitlink.org.cn/cloudream/common/utils/sync2" ) type CommandFn func() @@ -16,7 +16,7 @@ type CommandFn func() type CommandChannel struct { cmds *list.List[CommandFn] cmdsLock sync.Mutex - cmdsCounter *mysync.CounterCond + cmdsCounter *sync2.CounterCond chanReceive chan CommandFn chanReceiveDone atomic.Bool @@ -28,7 +28,7 @@ type CommandChannel struct { func NewCommandChannel() *CommandChannel { return &CommandChannel{ cmds: list.New[CommandFn](), - cmdsCounter: mysync.NewCounterCond(0), + cmdsCounter: sync2.NewCounterCond(0), cmdChan: make(chan CommandFn), } } diff --git a/pkgs/event/executor.go b/pkgs/event/executor.go index afd8a82..3536826 100644 --- a/pkgs/event/executor.go +++ b/pkgs/event/executor.go @@ -4,7 +4,7 @@ import ( "sync" "github.com/zyedidia/generic/list" - mysync "gitlink.org.cn/cloudream/common/utils/sync" + "gitlink.org.cn/cloudream/common/utils/sync2" ) type ExecuteOption struct { @@ -26,7 +26,7 @@ type postedEvent[TArgs any] struct { type Executor[TArgs any] struct { events *list.List[postedEvent[TArgs]] locker sync.Mutex - eventCond *mysync.CounterCond + eventCond *sync2.CounterCond execArgs TArgs } @@ -34,7 +34,7 @@ func NewExecutor[TArgs any](args TArgs) Executor[TArgs] { return Executor[TArgs]{ events: list.New[postedEvent[TArgs]](), locker: sync.Mutex{}, - eventCond: mysync.NewCounterCond(0), + eventCond: sync2.NewCounterCond(0), execArgs: args, } diff --git a/pkgs/ipfs/ipfs.go b/pkgs/ipfs/ipfs.go index 1822ce9..3f4e09e 100644 --- a/pkgs/ipfs/ipfs.go +++ b/pkgs/ipfs/ipfs.go @@ -6,12 +6,12 @@ import ( "io" shell "github.com/ipfs/go-ipfs-api" - myio "gitlink.org.cn/cloudream/common/utils/io" + "gitlink.org.cn/cloudream/common/utils/io2" ) type ReadOption struct { - Offset int64 // 从指定位置开始读取,为-1时代表不设置,从头开始读 - Length int64 // 读取长度,为-1时代表不设置,读取Offset之后的所有内容 + Offset int64 `json:"offset,string"` // 从指定位置开始读取,为-1时代表不设置,从头开始读 + Length int64 `json:"length,string"` // 读取长度,为-1时代表不设置,读取Offset之后的所有内容 } type Client struct { @@ -35,7 +35,7 @@ func (fs *Client) IsUp() bool { return fs.shell.IsUp() } -func (fs *Client) CreateFileStream() (myio.PromiseWriteCloser[string], error) { +func (fs *Client) CreateFileStream() (io2.PromiseWriteCloser[string], error) { pr, pw := io.Pipe() ipfsWriter := ipfsWriter{ diff --git a/pkgs/iterator/fuse.go b/pkgs/iterator/fuse.go new file mode 100644 index 0000000..7174ce4 --- /dev/null +++ b/pkgs/iterator/fuse.go @@ -0,0 +1,18 @@ +package iterator + +type fuseError[T any] struct { + err error +} + +func (i *fuseError[T]) MoveNext() (T, error) { + var ret T + return ret, i.err +} +func (i *fuseError[T]) Close() { + +} +func FuseError[T any](err error) Iterator[T] { + return &fuseError[T]{ + err: err, + } +} diff --git a/sdks/storage/object.go b/sdks/storage/object.go index 0af5f41..827558b 100644 --- a/sdks/storage/object.go +++ b/sdks/storage/object.go @@ -100,6 +100,8 @@ const ObjectDownloadPath = "/object/download" type ObjectDownload struct { UserID UserID `form:"userID" json:"userID" binding:"required"` ObjectID ObjectID `form:"objectID" json:"objectID" binding:"required"` + Offset int64 `form:"offset" json:"offset"` + Length *int64 `form:"length" json:"length"` } type DownloadingObject struct { Path string diff --git a/sdks/storage/utils.go b/sdks/storage/utils.go index fdbf25b..b8f0fa0 100644 --- a/sdks/storage/utils.go +++ b/sdks/storage/utils.go @@ -8,7 +8,7 @@ import ( "strings" myhttp "gitlink.org.cn/cloudream/common/utils/http" - "gitlink.org.cn/cloudream/common/utils/math" + "gitlink.org.cn/cloudream/common/utils/math2" "gitlink.org.cn/cloudream/common/utils/serder" ) @@ -34,5 +34,5 @@ func ParseJSONResponse[TBody any](resp *http.Response) (TBody, error) { } strCont := string(cont) - return ret, fmt.Errorf("unknow response content type: %s, status: %d, body(prefix): %s", contType, resp.StatusCode, strCont[:math.Min(len(strCont), 200)]) + return ret, fmt.Errorf("unknow response content type: %s, status: %d, body(prefix): %s", contType, resp.StatusCode, strCont[:math2.Min(len(strCont), 200)]) } diff --git a/utils/http/http.go b/utils/http/http.go index 94f5241..59d5d97 100644 --- a/utils/http/http.go +++ b/utils/http/http.go @@ -7,11 +7,12 @@ import ( "mime" "mime/multipart" "net/http" + "net/textproto" ul "net/url" "strings" "gitlink.org.cn/cloudream/common/pkgs/iterator" - "gitlink.org.cn/cloudream/common/utils/math" + "gitlink.org.cn/cloudream/common/utils/math2" "gitlink.org.cn/cloudream/common/utils/serder" ) @@ -129,13 +130,14 @@ func ParseJSONResponse[TBody any](resp *http.Response) (TBody, error) { } strCont := string(cont) - return ret, fmt.Errorf("unknow response content type: %s, status: %d, body(prefix): %s", contType, resp.StatusCode, strCont[:math.Min(len(strCont), 200)]) + return ret, fmt.Errorf("unknow response content type: %s, status: %d, body(prefix): %s", contType, resp.StatusCode, strCont[:math2.Min(len(strCont), 200)]) } type MultiPartFile struct { FieldName string FileName string File io.ReadCloser + Header textproto.MIMEHeader } type multiPartFileIterator struct { @@ -157,6 +159,7 @@ func (m *multiPartFileIterator) MoveNext() (*MultiPartFile, error) { FieldName: f.FormName(), FileName: fileName, File: f, + Header: f.Header, }, nil } @@ -179,6 +182,7 @@ func (m *multiPartFileIterator) MoveNext() (*MultiPartFile, error) { FieldName: part.FormName(), FileName: fileName, File: part, + Header: part.Header, }, nil } } diff --git a/utils/io/binary.go b/utils/io2/binary.go similarity index 99% rename from utils/io/binary.go rename to utils/io2/binary.go index d2c28c6..d356ed5 100644 --- a/utils/io/binary.go +++ b/utils/io2/binary.go @@ -1,4 +1,4 @@ -package io +package io2 import ( "bufio" diff --git a/utils/io/chunked_split.go b/utils/io2/chunked_split.go similarity index 99% rename from utils/io/chunked_split.go rename to utils/io2/chunked_split.go index ba5a7e1..b6606c5 100644 --- a/utils/io/chunked_split.go +++ b/utils/io2/chunked_split.go @@ -1,4 +1,4 @@ -package io +package io2 import ( "fmt" diff --git a/utils/io/chunked_split_test.go b/utils/io2/chunked_split_test.go similarity index 99% rename from utils/io/chunked_split_test.go rename to utils/io2/chunked_split_test.go index 8be2d3f..437c67b 100644 --- a/utils/io/chunked_split_test.go +++ b/utils/io2/chunked_split_test.go @@ -1,4 +1,4 @@ -package io +package io2 import ( "bytes" diff --git a/utils/io/clone.go b/utils/io2/clone.go similarity index 98% rename from utils/io/clone.go rename to utils/io2/clone.go index b1cf556..3faacaf 100644 --- a/utils/io/clone.go +++ b/utils/io2/clone.go @@ -1,4 +1,4 @@ -package io +package io2 import ( "io" diff --git a/utils/io/io.go b/utils/io2/io.go similarity index 99% rename from utils/io/io.go rename to utils/io2/io.go index 186c70b..89a95e8 100644 --- a/utils/io/io.go +++ b/utils/io2/io.go @@ -1,4 +1,4 @@ -package io +package io2 import "io" diff --git a/utils/io/io_test.go b/utils/io2/io_test.go similarity index 99% rename from utils/io/io_test.go rename to utils/io2/io_test.go index a4eab09..279dbaf 100644 --- a/utils/io/io_test.go +++ b/utils/io2/io_test.go @@ -1,4 +1,4 @@ -package io +package io2 import ( "bytes" diff --git a/utils/io/join.go b/utils/io2/join.go similarity index 92% rename from utils/io/join.go rename to utils/io2/join.go index cbd1a37..c7d46ef 100644 --- a/utils/io/join.go +++ b/utils/io2/join.go @@ -1,10 +1,10 @@ -package io +package io2 import ( "io" "gitlink.org.cn/cloudream/common/utils/lo2" - "gitlink.org.cn/cloudream/common/utils/math" + "gitlink.org.cn/cloudream/common/utils/math2" ) func Join(strs []io.Reader) io.ReadCloser { @@ -69,7 +69,7 @@ func (s *chunkedJoin) Read(buf []byte) (int, error) { return 0, io.EOF } - bufLen := math.Min(math.Min(s.chunkSize, len(buf)), s.chunkSize-s.currentRead) + bufLen := math2.Min(math2.Min(s.chunkSize, len(buf)), s.chunkSize-s.currentRead) rd, err := s.inputs[s.currentInput].Read(buf[:bufLen]) if err == nil { s.currentRead += rd diff --git a/utils/io/length.go b/utils/io2/length.go similarity index 88% rename from utils/io/length.go rename to utils/io2/length.go index d86781c..9cb0696 100644 --- a/utils/io/length.go +++ b/utils/io2/length.go @@ -1,9 +1,9 @@ -package io +package io2 import ( "io" - "gitlink.org.cn/cloudream/common/utils/math" + "gitlink.org.cn/cloudream/common/utils/math2" ) type lengthStream struct { @@ -19,7 +19,7 @@ func (s *lengthStream) Read(buf []byte) (int, error) { return 0, s.err } - bufLen := math.Min(s.length-s.readLength, int64(len(buf))) + bufLen := math2.Min(s.length-s.readLength, int64(len(buf))) rd, err := s.src.Read(buf[:bufLen]) if err == nil { s.readLength += int64(rd) diff --git a/utils/io/zero.go b/utils/io2/zero.go similarity index 95% rename from utils/io/zero.go rename to utils/io2/zero.go index ddd3a3f..39bea3a 100644 --- a/utils/io/zero.go +++ b/utils/io2/zero.go @@ -1,4 +1,4 @@ -package io +package io2 import "io" diff --git a/utils/math/math.go b/utils/math2/math.go similarity index 93% rename from utils/math/math.go rename to utils/math2/math.go index ca04819..2ee7c4f 100644 --- a/utils/math/math.go +++ b/utils/math2/math.go @@ -1,4 +1,4 @@ -package math +package math2 import "golang.org/x/exp/constraints" diff --git a/utils/sync/counter_cond.go b/utils/sync2/counter_cond.go similarity index 97% rename from utils/sync/counter_cond.go rename to utils/sync2/counter_cond.go index e0995d2..8da29e2 100644 --- a/utils/sync/counter_cond.go +++ b/utils/sync2/counter_cond.go @@ -1,4 +1,4 @@ -package sync +package sync2 import "sync" diff --git a/utils/sync2/sync2.go b/utils/sync2/sync2.go new file mode 100644 index 0000000..c6ab32f --- /dev/null +++ b/utils/sync2/sync2.go @@ -0,0 +1,20 @@ +package sync2 + +import "sync" + +func ParallelDo[T any](args []T, fn func(val T, index int) error) error { + var err error + var wg sync.WaitGroup + wg.Add(len(args)) + for i, arg := range args { + go func(arg T, index int) { + defer wg.Done() + + if e := fn(arg, index); e != nil { + err = e + } + }(arg, i) + } + wg.Wait() + return err +}