Skip to content

Commit

Permalink
download-directory use path
Browse files Browse the repository at this point in the history
Path replace driver name and directory name, #3.
  • Loading branch information
skodapetr committed Feb 13, 2024
1 parent 9c45634 commit 1f5f93c
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 31 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ Argumenty:
- `tenant` - Nahrazuje hodnotu z `MS_TENANT`. Volitelný argument.
- `secret` - Nahrazuje hodnotu z `MS_SECRET`. Volitelný argument.
- `site` - Identifikátor stránky, získání je popsáno v samosatné sekci.
- `drive` - Jméno knihovny.
Název "Knihovny dokumentů" v Obsahu webu. Doporučená hodnota "Dokumenty".
- `directory` - Jméno adresáře v knihovně.
Název adresáře ve složce "Dokumenty".
- `Path` - Název "Knihovny dokumentů" v "Obsahu webu" následovaný jmény adresářů. Znak `/` slouží jako oddělovač.
- `output` - Cesta k adresáři kam uložit soubory.

Příklad spuštění:
Expand All @@ -50,6 +47,7 @@ K získání identifikátoru stránky je možné použít náseldující dotaz:
```
https://graph.microsoft.com/v1.0/sites?$select=id&search={název stránky}
```
Poduk je odpovědí `403` je třeba udělit oprávnění `Sites.Read.All` v záložce `Modify permissions`.

K ziskání identifikátoru seznamu je možné otevřít nejprve seznam v prohlížeči.
Dále pak v sekci nastavení, tlačítko kolečka vpravo nahoře, je možné se navigovat na "Nastavení seznamu".
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/cz/gov/data/ms/EntryPoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,20 +110,20 @@ protected String getOption(
public void runDownloadDirectory(String[] args) {
Options options = new Options();
addCommonOptions(options);
options.addRequiredOption(null, "drive", true, "Drive name.");
options.addRequiredOption(null, "directory", true, "Directory name.");
options.addRequiredOption(null, "path", true,
"Path starting with drive name, followed by directory names. " +
"Use '/' as a separator.");
options.addRequiredOption(null, "output", true, "Output directory.");
//
CommandLine commandLine = parseCommandLine(options, args);
loadCommonOptions(commandLine);
//
String drive = commandLine.getOptionValue("drive");
String directory = commandLine.getOptionValue("directory");
String path = commandLine.getOptionValue("path");
Path output = Path.of(commandLine.getOptionValue("output"));
//
try {
DownloadSharepointDirectory.downloadContent(
authentication, siteIdentifier, drive, directory, output);
authentication, siteIdentifier, path, output);
} catch (Throwable t) {
LOG.error("Failed to download SharePoint directory.", t);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;

public class DownloadSharepointDirectory {
Expand All @@ -24,8 +25,7 @@ public class DownloadSharepointDirectory {
public static void downloadContent(
AzureAuthentication authentication,
String siteIdentifier,
String driveName,
String directoryName,
String path,
Path outputPath) {
var instance = new DownloadSharepointDirectory(
authentication, siteIdentifier);
Expand All @@ -34,7 +34,7 @@ public static void downloadContent(
} catch (IOException ex) {
LOG.error("Can not create target directory '{}'.", outputPath, ex);
}
instance.downloadList(driveName, directoryName, outputPath);
instance.downloadList(path, outputPath);
}

public DownloadSharepointDirectory(
Expand All @@ -44,7 +44,7 @@ public DownloadSharepointDirectory(
}

protected void downloadList(
String driveName, String directoryName, Path outputPath) {
String drivePath, Path outputPath) {
var graphClient = GraphServiceClient
.builder()
.authenticationProvider(authentication.provider())
Expand All @@ -53,11 +53,9 @@ protected void downloadList(
List<SharepointFile> fileList;

try {
fileList = sharepoint.listDriveDirectory(
siteIdentifier, driveName, directoryName);
fileList = sharepoint.listDriveDirectory(siteIdentifier, drivePath);
} catch (SharepointException ex) {
LOG.error("Can not list content of a directory '{}' drive '{}'",
directoryName, driveName, ex);
LOG.error("Can not list content of '{}'.", drivePath, ex);
return;
}

Expand Down
56 changes: 42 additions & 14 deletions src/main/java/cz/gov/data/ms/sharepoint/Sharepoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class Sharepoint<R> {
Expand Down Expand Up @@ -144,17 +146,24 @@ protected Row createRow(List<Column> columns, ListItem item) {
}

public List<SharepointFile> listDriveDirectory(
String siteIdentifier, String driveName, String directoryName)
String siteIdentifier, String drivePath)
throws SharepointException {
List<String> path = Arrays.asList(drivePath.split("/"));
if (path.isEmpty()) {
LOG.warn("Path is empty");
return Collections.emptyList();
}
var pathIterator = path.iterator();
String driveName = pathIterator.next();
String driveIdentifier = findDriveByName(siteIdentifier, driveName);
if (driveIdentifier == null) {
LOG.warn("Can't find drive with required name '{}'.", driveName);
return Collections.emptyList();
}
String directoryIdentifier = findDirectoryOnDrive(
siteIdentifier, driveIdentifier, directoryName);
siteIdentifier, driveIdentifier, pathIterator);
if (directoryIdentifier == null) {
LOG.warn("Can't find directory with required name '{}'.", directoryName);
LOG.warn("Directory not found.");
return Collections.emptyList();
}
return listDriveDirectoryByIdentifier(
Expand Down Expand Up @@ -182,24 +191,43 @@ protected String findDriveByName(

protected String findDirectoryOnDrive(
String siteIdentifier, String driveIdentifier,
String directoryName) throws SharepointException {
var rootItems = graphServiceClient
Iterator<String> pathIterator) throws SharepointException {
var driveBuilder = graphServiceClient
.sites(siteIdentifier)
.drives(driveIdentifier)
.drives(driveIdentifier);
var directoryItems = driveBuilder
.root()
.children()
.buildRequest()
.get();
if (rootItems == null) {
throw new SharepointException("Can't list drive content!");
}
var rootItemsPage = rootItems.getCurrentPage();
for (DriveItem driveItem : rootItemsPage) {
if (directoryName.equals(driveItem.name)) {
return driveItem.id;
StringBuilder visitedPath = new StringBuilder();
String resultIdentifier = null;
while (pathIterator.hasNext()) {
String nextName = pathIterator.next();
if (directoryItems == null) {
throw new SharepointException("Can't list drive content!");
}
String nextIdentifier = null;
for (DriveItem driveItem : directoryItems.getCurrentPage()) {
if (nextName.equals(driveItem.name)) {
nextIdentifier = driveItem.id;
break;
}
}
if (nextIdentifier == null) {
LOG.info("Can't find '{}' in '{}'.", nextName, visitedPath);
return null;
}
// Move to next iteration.
visitedPath.append('/').append(nextName);
resultIdentifier = nextIdentifier;
directoryItems = driveBuilder
.items(nextIdentifier)
.children()
.buildRequest()
.get();
}
return null;
return resultIdentifier;
}

protected List<SharepointFile> listDriveDirectoryByIdentifier(
Expand Down

0 comments on commit 1f5f93c

Please sign in to comment.