Skip to content

Commit

Permalink
client: implements add file command/service
Browse files Browse the repository at this point in the history
part of #15
  • Loading branch information
nuno-silva committed Nov 30, 2017
1 parent df7b344 commit 702a162
Show file tree
Hide file tree
Showing 7 changed files with 230 additions and 26 deletions.
8 changes: 8 additions & 0 deletions client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@
<version>3.8.1</version>
<scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>

</dependencies>
<build>
<resources>
Expand Down
44 changes: 44 additions & 0 deletions client/src/main/java/a16/yarfs/client/LocalFileManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Created by nuno at 29/11/17
*/
package a16.yarfs.client;

import java.io.*;
import java.nio.file.*;

/**
* Class LocalFileManager
* retrives and writes files from/to the local machine
*/
public class LocalFileManager {

/**
* read a file from the local machine
* @param fileName the path of the file to read
* @return the WHOLE contents of the files
* @throws FileNotFoundException
* @throws IOException
*/
public static byte[] getFileContents(String fileName) throws FileNotFoundException, IOException {
Path path = Paths.get(fileName);
return Files.readAllBytes(path);
}

/**
* write a file to the local machine if it does not exist
* @param fileName the path of the file to write
* @param content the content to write
* @throws FileAlreadyExistsException
*/
public static void putFileContents(String fileName, byte[] content) throws FileAlreadyExistsException {
Path path = Paths.get(fileName);
try {
Files.write(path, content, StandardOpenOption.CREATE_NEW); // don't override existing files
} catch (FileAlreadyExistsException e) {
throw e;
} catch (IOException e) {
e.printStackTrace();
}
}

}
100 changes: 100 additions & 0 deletions client/src/main/java/a16/yarfs/client/presentation/AddFileCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**
* Created by nuno at 29/11/17
*/
package a16.yarfs.client.presentation;

import a16.yarfs.client.ClientConstants;
import a16.yarfs.client.LocalFileManager;
import a16.yarfs.client.service.exception.AlreadyExecutedException;
import a16.yarfs.client.service.exception.NotExecutedException;
import a16.yarfs.client.service.exception.ServiceResultException;
import a16.yarfs.client.service.file.AddFileService;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.FileUtils;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;

/**
* Class AddFileCommand
* uploads a (new) file to the yarfs server
*/
public class AddFileCommand extends Command {

public AddFileCommand(YarfsShell sh, String name) {
super(sh, name, "add a new file from your computer to " + sh.getName());
}

@Override
void execute(String[] args) {
// check prerequisites
YarfsShell shell = (YarfsShell) getShell();
if (!shell.isLoggedIn()) {
shell.println("please login first");
return;
}

// check (and parse) arguments

if (args.length < 1 || args.length > 2) {
shell.println(getUsage());
return;
}

String localFilename = args[0];
String remoteFilename = args.length == 1 ? localFilename : args[1];

if(localFilename.trim().isEmpty() || remoteFilename.trim().isEmpty()) { // can this happen? who knows...
shell.println("file names must not be empty");
return;
}

// read the file from local storage
byte[] content;
try {
content = LocalFileManager.getFileContents(localFilename);
shell.println("read '" + localFilename + "' ("+ FileUtils.byteCountToDisplaySize(content.length)+")");
} catch (FileNotFoundException e) {
shell.println(localFilename + ": No such file");
return;
} catch (IOException e) {
shell.println("error reading " + localFilename + ": " + e.getMessage());
return;
}

// FIXME deal with encryption, keys, signature, etc

byte[] signature = DigestUtils.sha256Hex(content).getBytes(); // FIXME change this to an HMAC
byte[] key = "TODO-key".getBytes();

// send the file to the server
try {
AddFileService service = new AddFileService(ClientConstants.baseUrl,
shell.getActiveSessionid(), remoteFilename, content, signature, key);

service.execute();

// check the result
long fileId = service.getFileId();
shell.println("added file '"+remoteFilename+"' with id " + fileId);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ServiceResultException e) {
shell.println("could not add file: " + e.getMessage());
} catch (AlreadyExecutedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (NotExecutedException e) {
e.printStackTrace();
}
}


@Override
public String getUsage() {
return "Usage: "+ getName() + " <local filename> [remote filename]" + System.lineSeparator() +
"<remote filename> defaults to <local filename>";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public YarfsShell(InputStream is, PrintStream w, boolean flush) {
new LoginCommand(this, "login");
new RegisterCommand(this, "register");
new LogoutCommand(this, "logout");
// TODO add commands
new AddFileCommand(this, "add");
// TODO add more commands
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* Created by jorge at 11/11/17
**/
package a16.yarfs.client.service.file;

import a16.yarfs.client.ClientConstants;
import a16.yarfs.client.service.exception.AlreadyExecutedException;
import a16.yarfs.client.service.exception.NotExecutedException;
import a16.yarfs.client.service.exception.ServiceResultException;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.net.MalformedURLException;

import org.apache.commons.codec.binary.Base64;
/**
Class AddFileService
**/
public class AddFileService extends FileService {

private final String sessId;
private final String fileName;
private final byte[] fileContents;
private final byte[] fileSignature;
private final byte[] fileKey;

public AddFileService(String baseUrl, String sessId, String filename, byte[] contents, byte[] signature, byte[] key) throws MalformedURLException {
super(baseUrl, ClientConstants.Endpoints.ADD_FILE);
this.sessId = sessId;
fileName = filename;
fileContents = contents;
fileSignature = signature;
fileKey = key;
}

@Override
public void execute() throws IOException, AlreadyExecutedException {
JSONObject req = new JSONObject();
try {
req.put("sessid", sessId);
req.put("filename", fileName);

req.put("signature", Base64.encodeBase64String(fileSignature));
req.put("key", Base64.encodeBase64String(fileKey));
req.put("file", Base64.encodeBase64String(fileContents));

setRequestParameters(req);

super.execute();
} catch (JSONException e) {
e.printStackTrace();
}
}


/**
* get the result of the service
* @return the file id of the newly added file
* @throws NotExecutedException
* @throws IOException
* @throws ServiceResultException if the file was not added
*/
public long getFileId() throws NotExecutedException, IOException, ServiceResultException {
assertExecuted();

try {
JSONObject res = getResponse();
return Long.parseLong(res.getString("id"));
} catch (JSONException e) {
e.printStackTrace();
throw new ServiceResultException("no file id included in the response");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,4 @@ protected FileService(String baseUrl, String endpoint) throws MalformedURLExcept
}


public abstract void execute();
}

This file was deleted.

0 comments on commit 702a162

Please sign in to comment.