From 4e97f8810d8a1f5406a5a615bda7f431de7b5779 Mon Sep 17 00:00:00 2001 From: abraunegg Date: Fri, 2 Aug 2019 18:43:31 +1000 Subject: [PATCH] Implement --get-file-link (#612) * Implement --get-file-link which will return the weburl of a file which has been synced to OneDrive --- docs/USAGE.md | 2 ++ onedrive.1.in | 3 +++ src/config.d | 6 +++++- src/main.d | 14 +++++++++++--- src/onedrive.d | 2 +- src/sync.d | 32 ++++++++++++++++++++++++++++++++ 6 files changed, 54 insertions(+), 5 deletions(-) diff --git a/docs/USAGE.md b/docs/USAGE.md index 3a09038d..04e1bbbd 100644 --- a/docs/USAGE.md +++ b/docs/USAGE.md @@ -510,6 +510,8 @@ Options: Force the use of HTTP/2 for all operations where applicable --get-O365-drive-id ARG Query and return the Office 365 Drive ID for a given Office 365 SharePoint Shared Library + --get-file-link ARG + Display the file link of a synced file --help -h This help information. --local-first diff --git a/onedrive.1.in b/onedrive.1.in index 919d1e17..9a246698 100644 --- a/onedrive.1.in +++ b/onedrive.1.in @@ -91,6 +91,9 @@ Configuration file key: \fBforce_http_2\fP (default: \fBfalse\fP) \fB\-\-get\-O365\-drive\-id\fP ARG Query and return the Office 365 Drive ID for a given Office 365 SharePoint Shared Library .TP +\fB\-\-get\-file\-link\fP ARG +Display the file link of a synced file +.TP \fB\-\-local\-first\fP Synchronize from the local directory source first, before downloading changes from OneDrive. .br diff --git a/src/config.d b/src/config.d index 1b0ea254..2ae7ac4d 100644 --- a/src/config.d +++ b/src/config.d @@ -40,7 +40,7 @@ final class Config boolValues["disable_upload_validation"] = false; boolValues["enable_logging"] = false; boolValues["force_http_11"] = false; - boolValues["force_http_2"] = false; + boolValues["force_http_2"] = false; boolValues["local_first"] = false; boolValues["no_remote_delete"] = false; boolValues["skip_symlinks"] = false; @@ -148,6 +148,7 @@ final class Config // Add additional options that are NOT configurable via config file stringValues["create_directory"] = ""; stringValues["destination_directory"] = ""; + stringValues["get_file_link"] = ""; stringValues["get_o365_drive_id"] = ""; stringValues["remove_directory"] = ""; stringValues["single_directory"] = ""; @@ -216,6 +217,9 @@ final class Config "force-http-2", "Force the use of HTTP/2 for all operations where applicable", &boolValues["force_http_2"], + "get-file-link", + "Display the file link of a synced file", + &stringValues["get_file_link"], "get-O365-drive-id", "Query and return the Office 365 Drive ID for a given Office 365 SharePoint Shared Library", &stringValues["get_o365_drive_id"], diff --git a/src/main.d b/src/main.d index 606d3a13..5d613cf2 100644 --- a/src/main.d +++ b/src/main.d @@ -234,7 +234,7 @@ int main(string[] args) // create-directory, remove-directory, source-directory, destination-directory // are activities that dont perform a sync no error message for these items either - if (((cfg.getValueString("create_directory") != "") || (cfg.getValueString("remove_directory") != "")) || ((cfg.getValueString("source_directory") != "") && (cfg.getValueString("destination_directory") != "")) || (cfg.getValueString("get_o365_drive_id") != "") || cfg.getValueBool("display_sync_status")) { + if (((cfg.getValueString("create_directory") != "") || (cfg.getValueString("remove_directory") != "")) || ((cfg.getValueString("source_directory") != "") && (cfg.getValueString("destination_directory") != "")) || (cfg.getValueString("get_file_link") != "") || (cfg.getValueString("get_o365_drive_id") != "") || cfg.getValueBool("display_sync_status")) { performSyncOK = true; } @@ -314,7 +314,10 @@ int main(string[] args) selectiveSync.setFileMask(cfg.getValueString("skip_file")); // Initialize the sync engine - log.logAndNotify("Initializing the Synchronization Engine ..."); + if (cfg.getValueString("get_file_link") == "") { + // Print out that we are initializing the engine only if we are not grabbing the file link + log.logAndNotify("Initializing the Synchronization Engine ..."); + } auto sync = new SyncEngine(cfg, oneDrive, itemDb, selectiveSync); try { @@ -367,10 +370,15 @@ int main(string[] args) } // Are we obtaining the Office 365 Drive ID for a given Office 365 SharePoint Shared Library? - if (cfg.getValueString("get_o365_drive_id") != ""){ + if (cfg.getValueString("get_o365_drive_id") != "") { sync.querySiteCollectionForDriveID(cfg.getValueString("get_o365_drive_id")); } + // Are we obtaining the URL path for a synced file? + if (cfg.getValueString("get_file_link") != "") { + sync.queryOneDriveForFileURL(cfg.getValueString("get_file_link"), syncDir); + } + // Are we displaying the sync status of the client? if (cfg.getValueBool("display_sync_status")) { string remotePath = "/"; diff --git a/src/onedrive.d b/src/onedrive.d index b3705309..0620bdb6 100644 --- a/src/onedrive.d +++ b/src/onedrive.d @@ -320,7 +320,7 @@ final class OneDriveApi const(char)[] url; // string driveByIdUrl = "https://graph.microsoft.com/v1.0/drives/"; url = driveByIdUrl ~ driveId ~ "/items/" ~ id; - url ~= "?select=size,malware,file"; + url ~= "?select=size,malware,file,webUrl"; return get(url); } diff --git a/src/sync.d b/src/sync.d index de2678a7..70647ccc 100644 --- a/src/sync.d +++ b/src/sync.d @@ -2635,6 +2635,38 @@ final class SyncEngine } } + // Query OneDrive for a URL path of a file + void queryOneDriveForFileURL(string localFilePath, string syncDir) { + // Query if file is valid locally + if (exists(localFilePath)) { + // File exists locally, does it exist in the database + // Path needs to be relative to sync_dir path + string relativePath = relativePath(localFilePath, syncDir); + Item item; + if (itemdb.selectByPath(relativePath, defaultDriveId, item)) { + // File is in the local database cache + JSONValue fileDetails; + + try { + fileDetails = onedrive.getFileDetails(item.driveId, item.id); + } catch (OneDriveException e) { + log.error("ERROR: Query of OneDrive for file details failed"); + } + + if ((fileDetails.type() == JSONType.object) && ("webUrl" in fileDetails)) { + // Valid JSON object + writeln(fileDetails["webUrl"].str); + } + } else { + // File has not been synced with OneDrive + log.error("File has not been synced with OneDrive: ", localFilePath); + } + } else { + // File does not exist locally + log.error("File not found on local system: ", localFilePath); + } + } + // Query the OneDrive 'drive' to determine if we are 'in sync' or if there are pending changes void queryDriveForChanges(string path) {