-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
client: implements add file command/service
part of #15
- Loading branch information
1 parent
df7b344
commit 702a162
Showing
7 changed files
with
230 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
client/src/main/java/a16/yarfs/client/LocalFileManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
100
client/src/main/java/a16/yarfs/client/presentation/AddFileCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
client/src/main/java/a16/yarfs/client/service/file/AddFileService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 0 additions & 24 deletions
24
client/src/main/java/a16/yarfs/client/service/file/PutFileService.java
This file was deleted.
Oops, something went wrong.