Skip to content
Draft
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
37 changes: 29 additions & 8 deletions drivers/onedrive/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/url"
"path"
"sync"
"time"

"github.com/OpenListTeam/OpenList/v4/drivers/base"
"github.com/OpenListTeam/OpenList/v4/internal/driver"
Expand Down Expand Up @@ -105,22 +106,42 @@ func (d *Onedrive) List(ctx context.Context, dir model.Obj, args model.ListArgs)
}

func (d *Onedrive) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) {
f, err := d.GetFile(file.GetPath())
if err != nil {
return nil, err
}
if f.File == nil {
return nil, errs.NotFile
var u string
var err error
var duration time.Duration
if d.CreateShareLink && args.Redirect {
duration = 365 * 24 * time.Hour // cache 1 year
u, err = d.createLink(file.GetPath())
if err != nil {
return nil, err
}
} else {
f, err := d.GetFile(file.GetPath())
if err != nil {
return nil, err
}
if f.File == nil {
return nil, errs.NotFile
}
u = f.Url
}
u := f.Url
if d.CustomHost != "" {
_u, err := url.Parse(f.Url)
_u, err := url.Parse(u)
if err != nil {
return nil, err
}
_u.Host = d.CustomHost
u = _u.String()
}
if d.Addition.LinkExpireSeconds > 0 {
duration = time.Duration(d.Addition.LinkExpireSeconds) * time.Second
}
if duration > 0 {
return &model.Link{
URL: u,
Expiration: &duration,
}, nil
}
return &model.Link{
URL: u,
}, nil
Expand Down
2 changes: 2 additions & 0 deletions drivers/onedrive/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type Addition struct {
CustomHost string `json:"custom_host" help:"Custom host for onedrive download link"`
DisableDiskUsage bool `json:"disable_disk_usage" default:"false"`
EnableDirectUpload bool `json:"enable_direct_upload" default:"false" help:"Enable direct upload from client to OneDrive"`
CreateShareLink bool `json:"create_share_link" default:"false" help:"Create share link for file download"`
LinkExpireSeconds int64 `json:"link_expire_seconds" type:"number" default:"0" help:"Link cache duration in seconds, 0 means no cache"`
}

var config = driver.Config{
Expand Down
78 changes: 78 additions & 0 deletions drivers/onedrive/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"fmt"
"io"
"net/http"
"net/url"
stdpath "path"
"strings"
"time"

"github.com/OpenListTeam/OpenList/v4/drivers/base"
Expand Down Expand Up @@ -185,6 +187,82 @@ func (d *Onedrive) GetFile(path string) (*File, error) {
return &file, err
}

func (d *Onedrive) createLink(path string) (string, error) {
api := d.GetMetaUrl(false, path) + "/createLink"
data := base.Json{
"type": "view",
"scope": "anonymous",
}
var resp struct {
Link struct {
WebUrl string `json:"webUrl"`
} `json:"link"`
}
_, err := d.Request(api, http.MethodPost, func(req *resty.Request) {
req.SetBody(data)
}, &resp)
if err != nil {
return "", err
}
if resp.Link.WebUrl == "" {
return "", fmt.Errorf("createLink returned empty webUrl")
}

p, err := url.Parse(resp.Link.WebUrl)
if err != nil {
return "", err
}
// Do some transformations
q := url.Values{}
parts := splitSegs(p.Path)
if p.Host == "1drv.ms" {
// For personal
// https://1drv.ms/t/c/{user}/{share} ->
// https://my.microsoftpersonalcontent.com/personal/{user}/_layouts/15/download.aspx?share={share}
if len(parts) < 2 {
return "", fmt.Errorf("invalid onedrive short link: %s", resp.Link.WebUrl)
}
// take last two segments as user and share when available
user := parts[len(parts)-2]
share := parts[len(parts)-1]
p.Host = "my.microsoftpersonalcontent.com"
p.Path = fmt.Sprintf("/personal/%s/_layouts/15/download.aspx", user)
q.Set("share", share)
} else {
// https://{tenant}-my.sharepoint.com/:u:/g/personal/{user_email}/{share}
// https://{tenant}-my.sharepoint.com/personal/{user_email}/_layouts/15/download.aspx?share={share}
// Find the "personal" segment and extract user and share after it.
idx := -1
for i, s := range parts {
if s == "personal" {
idx = i
break
}
}
if idx == -1 || idx+2 >= len(parts) {
return "", fmt.Errorf("invalid onedrive sharepoint link: %s", resp.Link.WebUrl)
}
user := parts[idx+1]
share := parts[idx+2]
p.Path = fmt.Sprintf("/personal/%s/_layouts/15/download.aspx", user)
q.Set("share", share)
}
p.RawQuery = q.Encode()
return p.String(), nil
}

// splitSegs splits path into non-empty segments.
func splitSegs(path string) []string {
raw := strings.Split(path, "/")
segs := make([]string, 0, len(raw))
for _, s := range raw {
if s != "" {
segs = append(segs, s)
}
}
return segs
}

func (d *Onedrive) upSmall(ctx context.Context, dstDir model.Obj, stream model.FileStreamer) error {
filepath := stdpath.Join(dstDir.GetPath(), stream.GetName())
// 1. upload new file
Expand Down