mirror of
https://github.com/cloudreve/cloudreve.git
synced 2025-10-29 15:47:45 +08:00
Feat: truncate file if uploaded chunk is overlapped
This commit is contained in:
@ -136,8 +136,20 @@ func (handler Driver) Put(ctx context.Context, file fsctx.FileHeader) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if uint64(stat.Size()) != fileInfo.AppendStart {
|
||||
if uint64(stat.Size()) < fileInfo.AppendStart {
|
||||
return errors.New("未上传完成的文件分片与预期大小不一致")
|
||||
} else if uint64(stat.Size()) > fileInfo.AppendStart {
|
||||
out.Close()
|
||||
if err := handler.Truncate(ctx, dst, fileInfo.AppendStart); err != nil {
|
||||
return fmt.Errorf("覆盖分片时发生错误: %w", err)
|
||||
}
|
||||
|
||||
out, err = os.OpenFile(dst, os.O_APPEND|os.O_CREATE|os.O_WRONLY, Perm)
|
||||
defer out.Close()
|
||||
if err != nil {
|
||||
util.Log().Warning("无法打开或创建文件,%s", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,18 +1,26 @@
|
||||
package remote
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
model "github.com/cloudreve/Cloudreve/v3/models"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/auth"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/request"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/serializer"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
basePath = "/api/v3/slave"
|
||||
)
|
||||
|
||||
// Client to operate remote slave server
|
||||
type Client interface {
|
||||
CreateUploadSession(session *serializer.UploadSession, ttl int64) error
|
||||
CreateUploadSession(ctx context.Context, session *serializer.UploadSession, ttl int64) error
|
||||
GetUploadURL(ttl int64, sessionID string) (string, string, error)
|
||||
}
|
||||
|
||||
// NewClient creates new Client from given policy
|
||||
@ -23,7 +31,7 @@ func NewClient(policy *model.Policy) (Client, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
base, _ := url.Parse("/api/v3/slave")
|
||||
base, _ := url.Parse(basePath)
|
||||
signTTL := model.GetIntSetting("slave_api_timeout", 60)
|
||||
|
||||
return &remoteClient{
|
||||
@ -43,7 +51,7 @@ type remoteClient struct {
|
||||
httpClient request.Client
|
||||
}
|
||||
|
||||
func (c *remoteClient) CreateUploadSession(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{}{
|
||||
"session": session,
|
||||
"ttl": ttl,
|
||||
@ -57,6 +65,7 @@ func (c *remoteClient) CreateUploadSession(session *serializer.UploadSession, tt
|
||||
"PUT",
|
||||
"upload",
|
||||
bodyReader,
|
||||
request.WithContext(ctx),
|
||||
).CheckHTTPResponse(200).DecodeResponse()
|
||||
if err != nil {
|
||||
return err
|
||||
@ -68,3 +77,22 @@ func (c *remoteClient) CreateUploadSession(session *serializer.UploadSession, tt
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *remoteClient) GetUploadURL(ttl int64, sessionID string) (string, string, error) {
|
||||
base, err := url.Parse(c.policy.BaseURL)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
base.Path = path.Join(base.Path, "upload", sessionID)
|
||||
|
||||
req, err := http.NewRequest("POST", base.String(), nil)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
req.Header["X-Cr-Overwrite"] = []string{"true"}
|
||||
|
||||
req = auth.SignRequest(c.authInstance, req, ttl)
|
||||
return req.URL.String(), req.Header["Authorization"][0], nil
|
||||
}
|
||||
|
||||
@ -323,29 +323,23 @@ func (handler Driver) Source(
|
||||
|
||||
// Token 获取上传策略和认证Token
|
||||
func (handler Driver) Token(ctx context.Context, ttl int64, uploadSession *serializer.UploadSession, file fsctx.FileHeader) (*serializer.UploadCredential, error) {
|
||||
if err := handler.client.CreateUploadSession(uploadSession, ttl); err != nil {
|
||||
// 在从机端创建上传会话
|
||||
if err := handler.client.CreateUploadSession(ctx, uploadSession, ttl); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 获取上传地址
|
||||
uploadURL, sign, err := handler.client.GetUploadURL(ttl, uploadSession.Key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &serializer.UploadCredential{
|
||||
SessionID: uploadSession.Key,
|
||||
ChunkSize: handler.Policy.OptionsSerialized.ChunkSize,
|
||||
SessionID: uploadSession.Key,
|
||||
ChunkSize: handler.Policy.OptionsSerialized.ChunkSize,
|
||||
UploadURLs: []string{uploadURL},
|
||||
Credential: sign,
|
||||
}, nil
|
||||
//// 生成回调地址
|
||||
//siteURL := model.GetSiteURL()
|
||||
//apiBaseURI, _ := url.Parse("/api/v3/callback/remote/" + uploadSession.Key)
|
||||
//apiURL := siteURL.ResolveReference(apiBaseURI)
|
||||
//
|
||||
//// 生成上传策略
|
||||
//policy := serializer.UploadPolicy{
|
||||
// SavePath: handler.Policy.DirNameRule,
|
||||
// FileName: handler.Policy.FileNameRule,
|
||||
// AutoRename: handler.Policy.AutoRename,
|
||||
// MaxSize: handler.Policy.MaxSize,
|
||||
// AllowedExtension: handler.Policy.OptionsSerialized.FileType,
|
||||
// CallbackURL: apiURL.String(),
|
||||
//}
|
||||
//return handler.getUploadCredential(ctx, policy, ttl)
|
||||
}
|
||||
|
||||
func (handler Driver) getUploadCredential(ctx context.Context, policy serializer.UploadPolicy, TTL int64) (serializer.UploadCredential, error) {
|
||||
|
||||
Reference in New Issue
Block a user