Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions drivers/onedrive/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,11 @@ func (d *Onedrive) GetDirectUploadTools() []string {
}

// GetDirectUploadInfo returns the direct upload info for OneDrive
func (d *Onedrive) GetDirectUploadInfo(ctx context.Context, _ string, dstDir model.Obj, fileName string, _ int64) (any, error) {
func (d *Onedrive) GetDirectUploadInfo(ctx context.Context, _ string, dstDir model.Obj, fileName string, fileSize int64) (any, error) {
if !d.EnableDirectUpload {
return nil, errs.NotImplement
}
return d.getDirectUploadInfo(ctx, path.Join(dstDir.GetPath(), fileName))
return d.getDirectUploadInfo(ctx, path.Join(dstDir.GetPath(), fileName), fileSize)
}

var _ driver.Driver = (*Onedrive)(nil)
59 changes: 37 additions & 22 deletions drivers/onedrive/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,29 +317,44 @@ func (d *Onedrive) getDrive(ctx context.Context) (*DriveResp, error) {
return &resp, nil
}

func (d *Onedrive) getDirectUploadInfo(ctx context.Context, path string) (*model.HttpDirectUploadInfo, error) {
// Create upload session
url := d.GetMetaUrl(false, path) + "/createUploadSession"
metadata := map[string]any{
"item": map[string]any{
"@microsoft.graph.conflictBehavior": "rename",
},
}
func (d *Onedrive) getDirectUploadInfo(ctx context.Context, path string, fileSize int64) (*model.HttpDirectUploadInfo, error) {
// Reference: drivers/onedrive/driver.go#L215C25-L215C36 (39dcf9b)
const smallFileLimit int64 = 4 * 1024 * 1024

res, err := d.Request(url, http.MethodPost, func(req *resty.Request) {
req.SetBody(metadata).SetContext(ctx)
}, nil)
if err != nil {
return nil, err
}
if fileSize <= smallFileLimit {
// Small file: Same behavior as upSmall()
// in drivers/onedrive/util.go#L189 (39dcf9b)
uploadUrl := d.GetMetaUrl(false, path) + "/content"
return &model.HttpDirectUploadInfo{
UploadURL: uploadUrl,
ChunkSize: 0,
Method: http.MethodPut,
}, nil
} else {
// Large file: Same behavior as upBig()
// in drivers/onedrive/util.go#L235 (39dcf9b)
url := d.GetMetaUrl(false, path) + "/createUploadSession"
metadata := map[string]any{
"item": map[string]any{
"@microsoft.graph.conflictBehavior": "rename",
},
}

uploadUrl := jsoniter.Get(res, "uploadUrl").ToString()
if uploadUrl == "" {
return nil, fmt.Errorf("failed to get upload URL from response")
res, err := d.Request(url, http.MethodPost, func(req *resty.Request) {
req.SetBody(metadata).SetContext(ctx)
}, nil)
if err != nil {
return nil, err
}

uploadUrl := jsoniter.Get(res, "uploadUrl").ToString()
if uploadUrl == "" {
return nil, fmt.Errorf("failed to get upload URL from response")
}
return &model.HttpDirectUploadInfo{
UploadURL: uploadUrl,
ChunkSize: d.ChunkSize * 1024 * 1024, // Convert MB to bytes
Method: http.MethodPut,
}, nil
}
return &model.HttpDirectUploadInfo{
UploadURL: uploadUrl,
ChunkSize: d.ChunkSize * 1024 * 1024, // Convert MB to bytes
Method: "PUT",
}, nil
}