Skip to content

Commit

Permalink
Update PR
Browse files Browse the repository at this point in the history
* Add --get-file-link
* Add --modified-by
  • Loading branch information
abraunegg committed Oct 10, 2023
1 parent 0d3fc3e commit 72a4680
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 4 deletions.
12 changes: 8 additions & 4 deletions docs/application-config-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -883,9 +883,11 @@ Are you sure you wish to proceed with --force-sync [Y/N]
To procceed with this sync task, you must risk accept the actions you are taking. If you have any concerns, first use `--dry-run` and evaluate the outcome before proceeding with the actual action.

### CLI Option: --get-file-link
_**Description:**_
_**Description:**_ This CLI option queries the OneDrive API and return's the WebURL for the given local file.

_**Usage Example:**_
_**Usage Example:**_ `onedrive --get-file-link 'relative/path/to/your/file.txt'`

_**Additional Usage Notes:**_ The path that you should use must be relative to your 'sync_dir'

### CLI Option: --get-sharepoint-drive-id
_**Description:**_ This CLI option queries the OneDrive API and return's the Office 365 Drive ID for a given Office 365 SharePoint Shared Library that can then be used with 'drive_id' to sync a specific SharePoint Library.
Expand All @@ -898,9 +900,11 @@ _**Description:**_ This CLI option removes this clients authentictaion status wi
_**Usage Example:**_ `onedrive --logout`

### CLI Option: --modified-by
_**Description:**_
_**Description:**_ This CLI option queries the OneDrive API and return's the last modified details for the given local file.

_**Usage Example:**_
_**Usage Example:**_ `onedrive --modified-by 'relative/path/to/your/file.txt'`

_**Additional Usage Notes:**_ The path that you should use must be relative to your 'sync_dir'

### CLI Option: --monitor | -m
_**Description:**_ This CLI option controls the 'Monitor Mode' operational aspect of the client. When this option is used, the client will perform on-going syncs of data between Microsoft OneDrive and your local system. Local changes will be uploaded in near-realtime, whilst online changes will be downloaded on the next sync process. The frequency of these checks is governed by the 'monitor_interval' value.
Expand Down
18 changes: 18 additions & 0 deletions src/main.d
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,24 @@ int main(string[] cliArgs) {
return EXIT_SUCCESS;
}

// --get-file-link - Get the URL path for a synced file?
if (appConfig.getValueString("get_file_link") != "") {
// Query the OneDrive API for the file link
syncEngineInstance.queryOneDriveForFileDetails(appConfig.getValueString("get_file_link"), runtimeSyncDirectory, "URL");
// Exit application
// Use exit scopes to shutdown API and cleanup data
return EXIT_SUCCESS;
}

// --modified-by - Are we listing the modified-by details of a provided path?
if (appConfig.getValueString("modified_by") != "") {
// Query the OneDrive API for the last modified by details
syncEngineInstance.queryOneDriveForFileDetails(appConfig.getValueString("modified_by"), runtimeSyncDirectory, "ModifiedBy");
// Exit application
// Use exit scopes to shutdown API and cleanup data
return EXIT_SUCCESS;
}

// If we get to this point, we have not performed a 'no-sync' task ..
log.error("\n --sync or --monitor switches missing from your command line input. Please add one (not both) of these switches to your command line or use 'onedrive --help' for further assistance.\n");
log.error("No OneDrive sync will be performed without one of these two arguments being present.\n");
Expand Down
73 changes: 73 additions & 0 deletions src/sync.d
Original file line number Diff line number Diff line change
Expand Up @@ -6660,4 +6660,77 @@ class SyncEngine {
writeln("There are no pending changes from Microsoft OneDrive; your local directory matches the data online.");
}
}

// Query OneDrive for file details of a given path, returning either the 'webURL' or 'lastModifiedBy' JSON facet
void queryOneDriveForFileDetails(string inputFilePath, string runtimePath, string outputType) {

// Calculate the full local file path
string fullLocalFilePath = buildNormalizedPath(buildPath(runtimePath, inputFilePath));

// Query if file is valid locally
if (exists(fullLocalFilePath)) {
// search drive_id list
string[] distinctDriveIds = itemDB.selectDistinctDriveIds();
bool fileInDB = false;
Item dbItem;

foreach (searchDriveId; distinctDriveIds) {
// Does this path exist in the database, use the 'inputFilePath'
if (itemDB.selectByPath(inputFilePath, searchDriveId, dbItem)) {
// item is in the database
fileInDB = true;
JSONValue fileDetailsFromOneDrive;

// Create a new API Instance for this thread and initialise it
OneDriveApi queryOneDriveForFileDetailsApiInstance;
queryOneDriveForFileDetailsApiInstance = new OneDriveApi(appConfig);
queryOneDriveForFileDetailsApiInstance.initialise();

try {
fileDetailsFromOneDrive = queryOneDriveForFileDetailsApiInstance.getPathDetailsById(dbItem.driveId, dbItem.id);
} catch (OneDriveException exception) {
// display what the error is
displayOneDriveErrorMessage(exception.msg, getFunctionName!({}));
return;
}

// debug output of response
log.vdebug("API Response: ", fileDetailsFromOneDrive);

// What sort of response to we generate
// --get-file-link response
if (outputType == "URL") {
if ((fileDetailsFromOneDrive.type() == JSONType.object) && ("webUrl" in fileDetailsFromOneDrive)) {
// Valid JSON object
writeln();
writeln("WebURL: ", fileDetailsFromOneDrive["webUrl"].str);
}
}

// --modified-by response
if (outputType == "ModifiedBy") {
if ((fileDetailsFromOneDrive.type() == JSONType.object) && ("lastModifiedBy" in fileDetailsFromOneDrive)) {
// Valid JSON object
writeln();
writeln("Last modified: ", fileDetailsFromOneDrive["lastModifiedDateTime"].str);
writeln("Last modified by: ", fileDetailsFromOneDrive["lastModifiedBy"]["user"]["displayName"].str);
// if 'email' provided, add this to the output
if ("email" in fileDetailsFromOneDrive["lastModifiedBy"]["user"]) {
writeln("Email Address: ", fileDetailsFromOneDrive["lastModifiedBy"]["user"]["email"].str);
}
}
}
}
}

// was path found?
if (!fileInDB) {
// File has not been synced with OneDrive
log.error("Path has not been synced with OneDrive: ", inputFilePath);
}
} else {
// File does not exist locally
log.error("Path not found on local system: ", inputFilePath);
}
}
}

0 comments on commit 72a4680

Please sign in to comment.