Commit 4cf91cde authored by Hélène Ortiz's avatar Hélène Ortiz

add save and restore binary files (for attached files in chat)

parent fbd3ddd9
......@@ -61,7 +61,7 @@
<dependency>
<groupId>fr.ill.ics</groupId>
<artifactId>nomadcommandsystem-messages</artifactId>
<version>0.0.19</version>
<version>0.0.20</version>
</dependency>
<dependency>
<groupId>fr.ill.ics</groupId>
......
......@@ -21,6 +21,8 @@ package fr.ill.ics.bridge;
import java.util.HashMap;
import com.google.protobuf.ByteString;
import fr.ill.ics.nscclient.command.CommandZoneAccessor;
import fr.ill.ics.nscclient.dataprovider.ServantManagerAccessor;
import fr.ill.ics.nscclient.servant.ResourceNotFoundException;
......@@ -86,11 +88,18 @@ public class ResourceManager {
CommandZoneAccessor.getInstance(serverId).saveFile(fileName, content, fileType, addServerHome);
}
public void saveBinaryFile(String fileName, byte[] bytes, int fileType, boolean addServerHome) {
ByteString byteString = ByteString.copyFrom(bytes);
CommandZoneAccessor.getInstance(serverId).saveBinaryFile(fileName, byteString, fileType, addServerHome);
}
public String getFileContent(String fileName) throws ResourceNotFoundException {
return CommandZoneAccessor.getInstance(serverId).getFileContent(fileName);
}
public byte[] getBinaryFileContent(String fileName) throws ResourceNotFoundException {
return CommandZoneAccessor.getInstance(serverId).getBinaryFileContent(fileName);
}
public String getFileContentAbsolutePath(String fileName) throws ResourceNotFoundException {
return CommandZoneAccessor.getInstance(serverId).getFileContentAbsolutePath(fileName);
......
......@@ -51,8 +51,12 @@ public class AttachedFileChatEvent extends ChatEvent {
}
/**
* Returns the file extension (including dot separator), for example ".txt"
* @return
*/
public String getFileExtension() {
return fileName.substring(fileName.lastIndexOf(".")+1);
return fileName.substring(fileName.lastIndexOf("."));
}
}
......
......@@ -27,6 +27,7 @@ import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import fr.ill.ics.cameo.Application;
......@@ -3214,6 +3215,32 @@ public class CommandZoneAccessor {
}
public synchronized void saveBinaryFile(String fileName, ByteString content, int fileType, boolean addServerHome) {
// Create the message type.
CommandZoneRequests.Message type = CommandZoneRequests.Message.newBuilder()
.setType(CommandZoneRequests.Message.Type.SaveBinaryFile)
.build();
// Create the request.
CommandZoneRequests.SaveBinaryFileRequest request = CommandZoneRequests.SaveBinaryFileRequest.newBuilder()
.setClientID(getClientID())
.setFileName(fileName)
.setFileType(fileType)
.setContent(content)
.setAddServerHome(addServerHome)
.build();
commandBoxRequester.sendTwoParts(type.toByteArray(), request.toByteArray());
try {
Common.BooleanResponse.parseFrom(commandBoxRequester.receive());
}
catch (InvalidProtocolBufferException e) {
LOGGER.logp(Level.WARNING, this.getClass().getName(), "saveBinaryFile", "error in parsing response");
}
}
public synchronized String getFileContent(String fileName) throws ResourceNotFoundException {
// Create the message type.
......@@ -3244,6 +3271,36 @@ public class CommandZoneAccessor {
}
public synchronized byte[] getBinaryFileContent(String fileName) throws ResourceNotFoundException {
// Create the message type.
CommandZoneRequests.Message type = CommandZoneRequests.Message.newBuilder()
.setType(CommandZoneRequests.Message.Type.GetBinaryFileContent)
.build();
// Create the request.
CommandZoneRequests.GetFileContentRequest request = CommandZoneRequests.GetFileContentRequest.newBuilder()
.setFileName(fileName)
.build();
commandBoxRequester.sendTwoParts(type.toByteArray(), request.toByteArray());
try {
Common.BinaryResponse response = Common.BinaryResponse.parseFrom(commandBoxRequester.receive());
if (response.hasError()) {
throw new ResourceNotFoundException(fileName);
}
return response.getValue().toByteArray();
}
catch (InvalidProtocolBufferException e) {
LOGGER.logp(Level.WARNING, this.getClass().getName(), "getBinaryFileContent", "error in parsing response");
}
throw new ResourceNotFoundException(fileName);
}
public synchronized String getFileContentAbsolutePath(String fileName) throws ResourceNotFoundException {
// Create the message type.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment