Skip to content

Commit

Permalink
[cli] share read-access working
Browse files Browse the repository at this point in the history
  • Loading branch information
cboddy committed Aug 26, 2019
1 parent c2d9df3 commit 982fc5e
Showing 1 changed file with 39 additions and 8 deletions.
47 changes: 39 additions & 8 deletions src/peergos/server/cli/CLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public enum Command {
get_follow_requests("Show the users that have sent you a follow request"),
follow("Send a follow-request to another user.", "follow username-to-follow"),
passwd("Update your password"),
share("","");
share_read("share_read path <user>","Grant read access for a file to another user.");

public final String description, example;

Expand Down Expand Up @@ -197,6 +197,8 @@ private String handle(ParsedCommand parsedCommand, Terminal terminal, LineReader
case passwd:
return passwd(parsedCommand, terminal, reader);
// case share:
case share_read:
return shareReadAccess(parsedCommand);
default:
return "Unexpected cmd '" + parsedCommand.cmd + "'";
}
Expand All @@ -222,19 +224,22 @@ public String ls(ParsedCommand cmd) {
.collect(Collectors.joining("\n"));
}

public String get(ParsedCommand cmd) throws IOException {
if (!cmd.hasArguments())
throw new IllegalStateException();

Path remotePath = resolvedRemotePath(cmd.firstArgument());

private Stat checkPath(Path remotePath) {
Stat stat = null;
try {
stat = peergosFileSystem.stat(remotePath);
return peergosFileSystem.stat(remotePath);
} catch (Exception ex) {
throw new IllegalStateException("Could not find remote specified remote path '" + remotePath + "'", ex);
}

}
public String get(ParsedCommand cmd) throws IOException {
if (!cmd.hasArguments())
throw new IllegalStateException();

Path remotePath = resolvedRemotePath(cmd.firstArgument());

Stat stat = checkPath(remotePath);
// TODO
if (stat.fileProperties().isDirectory)
throw new IllegalStateException("Directory is not supported");
Expand Down Expand Up @@ -338,6 +343,32 @@ public String getFollowRequests(ParsedCommand cmd) {

}

public String shareReadAccess(ParsedCommand cmd) {

if (! cmd.hasSecondArgument())
throw new IllegalStateException();

String pathToShare = cmd.firstArgument();
Path remotePath = resolvedRemotePath(pathToShare);

Stat stat = checkPath(remotePath);
// TODO
if (stat.fileProperties().isDirectory)
throw new IllegalStateException("Directory is not supported");

String userToGrantReadAccess = cmd.secondArgument();
Set<String> followerUsernames = cliContext.userContext.getFollowerNames().join();
if (! followerUsernames.contains(userToGrantReadAccess))
return "File not shared: specified-user " + userToGrantReadAccess + " is not following you";
try {
cliContext.userContext.shareReadAccessWith(remotePath, new HashSet<>(Arrays.asList(userToGrantReadAccess))).join();
} catch (Exception ex) {
ex.printStackTrace();
return "Failed not share file";
}
return "Shared read-access to '"+ remotePath +"' with " + userToGrantReadAccess;
}

public String follow(ParsedCommand cmd) {
if (!cmd.hasArguments())
throw new IllegalStateException();
Expand Down

0 comments on commit 982fc5e

Please sign in to comment.