|
|
@ -11,6 +11,7 @@ import (
|
|
|
|
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx"
|
|
|
|
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx"
|
|
|
|
"github.com/cloudreve/Cloudreve/v3/pkg/request"
|
|
|
|
"github.com/cloudreve/Cloudreve/v3/pkg/request"
|
|
|
|
"github.com/cloudreve/Cloudreve/v3/pkg/serializer"
|
|
|
|
"github.com/cloudreve/Cloudreve/v3/pkg/serializer"
|
|
|
|
|
|
|
|
"github.com/cloudreve/Cloudreve/v3/pkg/util"
|
|
|
|
"github.com/gofrs/uuid"
|
|
|
|
"github.com/gofrs/uuid"
|
|
|
|
"io"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http"
|
|
|
@ -26,7 +27,7 @@ const (
|
|
|
|
chunkRetrySleep = time.Duration(5) * time.Second
|
|
|
|
chunkRetrySleep = time.Duration(5) * time.Second
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// Client to operate remote slave server
|
|
|
|
// Client to operate uploading to remote slave server
|
|
|
|
type Client interface {
|
|
|
|
type Client interface {
|
|
|
|
// CreateUploadSession creates remote upload session
|
|
|
|
// CreateUploadSession creates remote upload session
|
|
|
|
CreateUploadSession(ctx context.Context, session *serializer.UploadSession, ttl int64) error
|
|
|
|
CreateUploadSession(ctx context.Context, session *serializer.UploadSession, ttl int64) error
|
|
|
@ -34,6 +35,8 @@ type Client interface {
|
|
|
|
GetUploadURL(ttl int64, sessionID string) (string, string, error)
|
|
|
|
GetUploadURL(ttl int64, sessionID string) (string, string, error)
|
|
|
|
// Upload uploads file to remote server
|
|
|
|
// Upload uploads file to remote server
|
|
|
|
Upload(ctx context.Context, file fsctx.FileHeader) error
|
|
|
|
Upload(ctx context.Context, file fsctx.FileHeader) error
|
|
|
|
|
|
|
|
// DeleteUploadSession deletes remote upload session
|
|
|
|
|
|
|
|
DeleteUploadSession(ctx context.Context, sessionID string) error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// NewClient creates new Client from given policy
|
|
|
|
// NewClient creates new Client from given policy
|
|
|
@ -84,7 +87,7 @@ func (c *remoteClient) Upload(ctx context.Context, file fsctx.FileHeader) error
|
|
|
|
|
|
|
|
|
|
|
|
overwrite := fileInfo.Mode&fsctx.Overwrite == fsctx.Overwrite
|
|
|
|
overwrite := fileInfo.Mode&fsctx.Overwrite == fsctx.Overwrite
|
|
|
|
|
|
|
|
|
|
|
|
// Upload chunks
|
|
|
|
// Initial chunk groups
|
|
|
|
chunks := chunk.NewChunkGroup(file, c.policy.OptionsSerialized.ChunkSize, &backoff.ConstantBackoff{
|
|
|
|
chunks := chunk.NewChunkGroup(file, c.policy.OptionsSerialized.ChunkSize, &backoff.ConstantBackoff{
|
|
|
|
Max: model.GetIntSetting("onedrive_chunk_retries", 1),
|
|
|
|
Max: model.GetIntSetting("onedrive_chunk_retries", 1),
|
|
|
|
Sleep: chunkRetrySleep,
|
|
|
|
Sleep: chunkRetrySleep,
|
|
|
@ -94,9 +97,13 @@ func (c *remoteClient) Upload(ctx context.Context, file fsctx.FileHeader) error
|
|
|
|
return c.uploadChunk(ctx, session.Key, current.Index(), content, overwrite, current.Length())
|
|
|
|
return c.uploadChunk(ctx, session.Key, current.Index(), content, overwrite, current.Length())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// upload chunks
|
|
|
|
for chunks.Next() {
|
|
|
|
for chunks.Next() {
|
|
|
|
if err := chunks.Process(uploadFunc); err != nil {
|
|
|
|
if err := chunks.Process(uploadFunc); err != nil {
|
|
|
|
// TODO 删除上传会话
|
|
|
|
if err := c.DeleteUploadSession(ctx, session.Key); err != nil {
|
|
|
|
|
|
|
|
util.Log().Warning("failed to delete upload session: %s", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return fmt.Errorf("failed to upload chunk #%d: %w", chunks.Index(), err)
|
|
|
|
return fmt.Errorf("failed to upload chunk #%d: %w", chunks.Index(), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -104,6 +111,24 @@ func (c *remoteClient) Upload(ctx context.Context, file fsctx.FileHeader) error
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (c *remoteClient) DeleteUploadSession(ctx context.Context, sessionID string) error {
|
|
|
|
|
|
|
|
resp, err := c.httpClient.Request(
|
|
|
|
|
|
|
|
"DELETE",
|
|
|
|
|
|
|
|
"upload/"+sessionID,
|
|
|
|
|
|
|
|
nil,
|
|
|
|
|
|
|
|
request.WithContext(ctx),
|
|
|
|
|
|
|
|
).CheckHTTPResponse(200).DecodeResponse()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if resp.Code != 0 {
|
|
|
|
|
|
|
|
return serializer.NewErrorFromResponse(resp)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *remoteClient) CreateUploadSession(ctx context.Context, session *serializer.UploadSession, ttl int64) error {
|
|
|
|
func (c *remoteClient) CreateUploadSession(ctx context.Context, session *serializer.UploadSession, ttl int64) error {
|
|
|
|
reqBodyEncoded, err := json.Marshal(map[string]interface{}{
|
|
|
|
reqBodyEncoded, err := json.Marshal(map[string]interface{}{
|
|
|
|
"session": session,
|
|
|
|
"session": session,
|
|
|
|