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
32 changes: 23 additions & 9 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,24 +106,37 @@ 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 {
duration = 365 * 24 * time.Hour // cache 1 year
u, err = d.createLink(file.GetPath())
if err != nil {
return nil, err
}
} else {
duration = 5 * time.Minute // cache 5 min
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()
}
return &model.Link{
URL: u,
URL: u,
Expiration: &duration,
}, nil
}

Expand Down
1 change: 1 addition & 0 deletions drivers/onedrive/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ 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"`
}

var config = driver.Config{
Expand Down
57 changes: 57 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,61 @@ 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
}

p, err := url.Parse(resp.Link.WebUrl)
if err != nil {
return "", err
}
// Do some transformations
q := url.Values{}
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}
paths := strings.Split(p.Path, "/")
if len(paths) < 5 || paths[3] == "" || paths[4] == "" {
return "", fmt.Errorf("invalid onedrive short link")
}
user := paths[3]
share := paths[4]
p.Host = "my.microsoftpersonalcontent.com"
p.Path = fmt.Sprintf("/personal/%s/_layouts/15/download.aspx", user)
q.Set("share", share)
} else if strings.Contains(p.Host, ".sharepoint.com") {
// 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}
paths := strings.Split(p.Path, "/")
if len(paths) < 6 || paths[5] == "" {
return "", fmt.Errorf("invalid onedrive sharepoint link")
}
user := paths[4]
share := paths[5]
p.Path = fmt.Sprintf("/personal/%s/_layouts/15/download.aspx", user)
q.Set("share", share)
} else {
return "", fmt.Errorf("unsupported onedrive link host: %s", p.Host)
}
p.RawQuery = q.Encode()
return p.String(), nil
}

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