Add option to create a read-write shareable link (#2154)

* Implement Feature Request 2110 to add option to create a read-write shareable link
This commit is contained in:
abraunegg 2022-09-26 17:56:42 +10:00 committed by GitHub
parent 993770f49a
commit 514df625b9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 60 additions and 13 deletions

View file

@ -44,6 +44,7 @@
* [How to sync shared folders (OneDrive Business or Office 365)?](#how-to-sync-shared-folders-onedrive-business-or-office-365)
* [How to sync sharePoint / Office 365 Shared Libraries?](#how-to-sync-sharepoint--office-365-shared-libraries)
* [How to run a user systemd service at boot without user login?](#how-to-run-a-user-systemd-service-at-boot-without-user-login)
* [How to create a shareable link?](#how-to-create-a-shareable-link)
- [Running 'onedrive' in 'monitor' mode](#running-onedrive-in-monitor-mode)
* [Use webhook to subscribe to remote updates in 'monitor' mode](#use-webhook-to-subscribe-to-remote-updates-in-monitor-mode)
* [More webhook configuration options](#more-webhook-configuration-options)
@ -840,6 +841,21 @@ To avoid this issue, you need to reconfigure your 'user' account so that the sys
loginctl enable-linger <your_user_name>
```
### How to create a shareable link?
In some cases it may be desirable to create a shareable file link and give this link to other users to access a specific file.
To do this, use the following command:
```text
onedrive --create-share-link <path/to/file>
```
**Note:** By default this will be a read-only link.
To make this a read-write link, use the following command:
```text
onedrive --create-share-link <path/to/file> --with-editing-perms
```
**Note:** The ordering of the option file path and option flag is important.
## Running 'onedrive' in 'monitor' mode
Monitor mode (`--monitor`) allows the onedrive process to continually monitor your local file system for changes to files.
@ -1300,4 +1316,6 @@ Options:
Print more details, useful for debugging (repeat for extra debugging)
--version
Print the version and exit
--with-editing-perms
Create a read-write shareable link for an existing file on OneDrive when used with --create-share-link <file>
```

View file

@ -263,6 +263,9 @@ enables even more verbose debug statements.
\fB\-\-version\fP
Print the version and exit
.TP
\fB\-\-with\-editing\-perms\fP
Create a read-write shareable link for an existing file on OneDrive when used with --create-share-link <file>
.TP
\fB\-h \-\-help\fP
This help information.
.PP

View file

@ -332,6 +332,7 @@ final class Config
boolValues["force"] = false;
boolValues["list_business_shared_folders"] = false;
boolValues["force_sync"] = false;
boolValues["with_editing_perms"] = false;
// Application Startup option validation
try {
@ -523,7 +524,10 @@ final class Config
&boolValues["list_business_shared_folders"],
"sync-shared-folders",
"Sync OneDrive Business Shared Folders",
&boolValues["sync_business_shared_folders"]
&boolValues["sync_business_shared_folders"],
"with-editing-perms",
"Create a read-write shareable link for an existing file on OneDrive when used with --create-share-link <file>",
&boolValues["with_editing_perms"]
);
if (opt.helpWanted) {
outputLongHelp(opt.options);

View file

@ -1208,12 +1208,12 @@ int main(string[] args)
// Do we need to create or remove a directory?
if ((cfg.getValueString("create_directory") != "") || (cfg.getValueString("remove_directory") != "")) {
// create directory
if (cfg.getValueString("create_directory") != "") {
// create a directory on OneDrive
sync.createDirectoryNoSync(cfg.getValueString("create_directory"));
}
//remove directory
if (cfg.getValueString("remove_directory") != "") {
// remove a directory on OneDrive
sync.deleteDirectoryNoSync(cfg.getValueString("remove_directory"));
@ -1234,10 +1234,17 @@ int main(string[] args)
return EXIT_SUCCESS;
}
// Are we createing an anonymous read-only shareable link for an existing file on OneDrive?
// --create-share-link - Are we createing a shareable link for an existing file on OneDrive?
if (cfg.getValueString("create_share_link") != "") {
// Query OneDrive for the file, and if valid, create a shareable link for the file
sync.createShareableLinkForFile(cfg.getValueString("create_share_link"));
// By default, the shareable link will be read-only.
// If the user adds:
// --with-editing-perms
// this will create a writeable link
bool writeablePermissions = cfg.getValueBool("with_editing_perms");
sync.createShareableLinkForFile(cfg.getValueString("create_share_link"), writeablePermissions);
// Exit application
// Use exit scopes to shutdown API
return EXIT_SUCCESS;

View file

@ -6216,7 +6216,7 @@ final class SyncEngine
}
// Create an anonymous read-only shareable link for an existing file on OneDrive
void createShareableLinkForFile(string filePath)
void createShareableLinkForFile(string filePath, bool writeablePermissions)
{
JSONValue onedrivePathDetails;
JSONValue createShareableLinkResponse;
@ -6232,6 +6232,7 @@ final class SyncEngine
if (e.httpStatusCode == 404) {
// Requested path could not be found
log.error("ERROR: The requested path to query was not found on OneDrive");
log.error("ERROR: Cannot create a shareable link for a file that does not exist on OneDrive");
return;
}
@ -6240,7 +6241,7 @@ final class SyncEngine
handleOneDriveThrottleRequest();
// Retry original request by calling function again to avoid replicating any further error handling
log.vdebug("Retrying original request that generated the OneDrive HTTP 429 Response Code (Too Many Requests) - calling queryDriveForChanges(path);");
createShareableLinkForFile(filePath);
createShareableLinkForFile(filePath, writeablePermissions);
// return back to original call
return;
}
@ -6249,7 +6250,7 @@ final class SyncEngine
// HTTP request returned status code 504 (Gateway Timeout)
log.log("OneDrive returned a 'HTTP 504 - Gateway Timeout' - retrying request");
// Retry original request by calling function again to avoid replicating any further error handling
createShareableLinkForFile(filePath);
createShareableLinkForFile(filePath, writeablePermissions);
// return back to original call
return;
} else {
@ -6266,11 +6267,21 @@ final class SyncEngine
driveId = onedrivePathDetails["parentReference"]["driveId"].str;
itemId = onedrivePathDetails["id"].str;
// configure the access scope
JSONValue accessScope = [
"type": "view",
"scope": "anonymous"
];
// What sort of shareable link is required?
JSONValue accessScope;
if (writeablePermissions) {
// configure the read-write access scope
accessScope = [
"type": "edit",
"scope": "anonymous"
];
} else {
// configure the read-only access scope (default)
accessScope = [
"type": "view",
"scope": "anonymous"
];
}
// Create the shareable file link
createShareableLinkResponse = onedrive.createShareableLink(driveId, itemId, accessScope);
@ -6278,6 +6289,10 @@ final class SyncEngine
// Extract the file share link from the JSON response
fileShareLink = createShareableLinkResponse["link"]["webUrl"].str;
writeln("File Shareable Link: ", fileShareLink);
if (writeablePermissions) {
writeln("Shareable Link has read-write permissions - use and provide with caution");
}
} else {
// not a valid JSON object
log.error("ERROR: There was an error performing this operation on OneDrive");