From d58fdb10db0e9b989e93d263d8f39784becd3c51 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Thu, 7 Jan 2021 11:02:54 +0000 Subject: [PATCH] onedrive: enhance link creation with expiry, scope, type and password This change makes the --expire flag in `rclone link` work. It also adds the new flags --onedrive-link-type --onedrive-link-scope --onedrive-link-password See: https://forum.rclone.org/t/create-share-link-within-the-organization-only/21498 --- backend/onedrive/api/types.go | 6 +++-- backend/onedrive/onedrive.go | 48 +++++++++++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/backend/onedrive/api/types.go b/backend/onedrive/api/types.go index e144e5868..394403b0e 100644 --- a/backend/onedrive/api/types.go +++ b/backend/onedrive/api/types.go @@ -253,8 +253,10 @@ type MoveItemRequest struct { //CreateShareLinkRequest is the request to create a sharing link //Always Type:view and Scope:anonymous for public sharing type CreateShareLinkRequest struct { - Type string `json:"type"` //Link type in View, Edit or Embed - Scope string `json:"scope,omitempty"` //Optional. Scope in anonymous, organization + Type string `json:"type"` // Link type in View, Edit or Embed + Scope string `json:"scope,omitempty"` // Scope in anonymous, organization + Password string `json:"password,omitempty"` // The password of the sharing link that is set by the creator. Optional and OneDrive Personal only. + Expiry *time.Time `json:"expirationDateTime,omitempty"` // A String with format of yyyy-MM-ddTHH:mm:ssZ of DateTime indicates the expiration time of the permission. } //CreateShareLinkResponse is the response from CreateShareLinkRequest diff --git a/backend/onedrive/onedrive.go b/backend/onedrive/onedrive.go index cade6eef1..a98bdb3ce 100755 --- a/backend/onedrive/onedrive.go +++ b/backend/onedrive/onedrive.go @@ -295,6 +295,41 @@ modification time and removes all but the last version. **NB** Onedrive personal can't currently delete versions so don't use this flag there. +`, + Advanced: true, + }, { + Name: "link_scope", + Default: "anonymous", + Help: `Set the scope of the links created by the link command.`, + Advanced: true, + Examples: []fs.OptionExample{{ + Value: "anonymous", + Help: "Anyone with the link has access, without needing to sign in. This may include people outside of your organization. Anonymous link support may be disabled by an administrator.", + }, { + Value: "organization", + Help: "Anyone signed into your organization (tenant) can use the link to get access. Only available in OneDrive for Business and SharePoint.", + }}, + }, { + Name: "link_type", + Default: "view", + Help: `Set the type of the links created by the link command.`, + Advanced: true, + Examples: []fs.OptionExample{{ + Value: "view", + Help: "Creates a read-only link to the item.", + }, { + Value: "edit", + Help: "Creates a read-write link to the item.", + }, { + Value: "embed", + Help: "Creates an embeddable link to the item.", + }}, + }, { + Name: "link_password", + Default: "", + Help: `Set the password for links created by the link command. + +At the time of writing this only works with OneDrive personal paid accounts. `, Advanced: true, }, { @@ -355,6 +390,9 @@ type Options struct { ExposeOneNoteFiles bool `config:"expose_onenote_files"` ServerSideAcrossConfigs bool `config:"server_side_across_configs"` NoVersions bool `config:"no_versions"` + LinkScope string `config:"link_scope"` + LinkType string `config:"link_type"` + LinkPassword string `config:"link_password"` Enc encoder.MultiEncoder `config:"encoding"` } @@ -1303,8 +1341,14 @@ func (f *Fs) PublicLink(ctx context.Context, remote string, expire fs.Duration, opts := newOptsCall(info.GetID(), "POST", "/createLink") share := api.CreateShareLinkRequest{ - Type: "view", - Scope: "anonymous", + Type: f.opt.LinkType, + Scope: f.opt.LinkScope, + Password: f.opt.LinkPassword, + } + + if expire < fs.Duration(time.Hour*24*365*100) { + expiry := time.Now().Add(time.Duration(expire)) + share.Expiry = &expiry } var resp *http.Response