Skip to content

Commit dfae21e

Browse files
authored
Add files via upload
1 parent 0a0702a commit dfae21e

3 files changed

Lines changed: 56 additions & 8 deletions

File tree

src/main/java/com/example/HelloController.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
import javafx.fxml.FXML;
66
import javafx.scene.control.*;
77
import javafx.scene.layout.AnchorPane;
8+
import javafx.stage.FileChooser;
89
import javafx.stage.Stage;
910

11+
import java.io.File;
1012
import java.time.LocalTime;
1113
import java.time.format.DateTimeFormatter;
1214

@@ -24,10 +26,13 @@ public class HelloController {
2426
@FXML
2527
private TextField usernameField;
2628

29+
30+
2731
private NtfyClient ntfy;
2832
private final DateTimeFormatter timeFormat = DateTimeFormatter.ofPattern("HH:mm");
2933

3034

35+
3136
@FXML
3237
public void initialize() {
3338
// Set your topic name (you can make this configurable)
@@ -51,6 +56,21 @@ private void handleSend() {
5156
ntfy.sendMessage(username, message);
5257
messageField.clear();
5358
}
59+
@FXML
60+
private void handleAttachFile(ActionEvent event) {
61+
FileChooser fileChooser = new FileChooser();
62+
fileChooser.setTitle("Select a file to send");
63+
64+
File selectedFile = fileChooser.showOpenDialog(null);
65+
66+
if (selectedFile != null) {
67+
System.out.println("Selected file: " + selectedFile.getAbsolutePath());
68+
sendFileToBackend(selectedFile);
69+
}
70+
}
71+
72+
private void sendFileToBackend(File selectedFile) {
73+
}
5474

5575
Stage stage;
5676
public void logout(ActionEvent event) {
@@ -64,6 +84,7 @@ public void logout(ActionEvent event) {
6484
System.out.println("You have been logged out");
6585
stage.close();
6686
}
87+
6788
}
6889
}
6990

src/main/java/com/example/HelloFX.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,14 @@
99
import javafx.scene.control.Alert;
1010
import javafx.scene.control.Button;
1111
import javafx.scene.control.ButtonType;
12+
import javafx.stage.FileChooser;
1213
import javafx.stage.Stage;
1314

15+
import java.io.File;
16+
import java.io.FileInputStream;
17+
import java.io.OutputStream;
18+
import java.net.HttpURLConnection;
19+
import java.net.URL;
1420
import java.util.Objects;
1521

1622
public class HelloFX extends Application {
@@ -46,6 +52,33 @@ public void start(Stage stage) throws Exception {
4652

4753
});
4854
}
55+
56+
private void sendFileToBackend(File file) {
57+
try {
58+
String backendUrl = System.getenv("BACKEND_URL");
59+
if (backendUrl == null) {
60+
System.err.println("BACKEND_URL not set");
61+
return;
62+
}
63+
64+
HttpURLConnection conn = (HttpURLConnection) new URL(backendUrl).openConnection();
65+
conn.setDoOutput(true);
66+
conn.setRequestMethod("POST");
67+
conn.setRequestProperty("Content-Type", "application/octet-stream");
68+
conn.setRequestProperty("Title", file.getName());
69+
70+
try (OutputStream os = conn.getOutputStream();
71+
FileInputStream fis = new FileInputStream(file)) {
72+
fis.transferTo(os);
73+
}
74+
75+
int responseCode = conn.getResponseCode();
76+
System.out.println("Upload response: " + responseCode);
77+
conn.disconnect();
78+
} catch (Exception e) {
79+
e.printStackTrace();
80+
}
81+
}
4982
public void logout(Stage stage) {
5083

5184
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);

src/main/resources/com/example/hello-view.fxml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,10 @@
1717
<TextField fx:id="usernameField" prefWidth="120" promptText="Username" />
1818
<TextField fx:id="messageField" promptText="Type a message..." HBox.hgrow="ALWAYS" />
1919
<Button onAction="#handleSend" text="Send" />
20+
<Button layoutX="420" layoutY="520" onAction="#handleAttachFile" prefWidth="80" text="📎 Attach" />
2021
<ListView fx:id="chatList" />
2122
</HBox>
22-
<Button fx:id="logOut"
23-
layoutX="528.0"
24-
mnemonicParsing="false"
25-
prefHeight="71.0"
26-
prefWidth="72.0"
27-
text="Logout"
28-
textFill="#5b3333"
29-
onAction="#logout">
23+
<Button fx:id="logOut" layoutX="528.0" layoutY="335.0" mnemonicParsing="false" onAction="#logout" prefHeight="71.0" prefWidth="72.0" text="Logout" textFill="#5b3333">
3024
<font>
3125
<Font size="13.0" />
3226
</font>

0 commit comments

Comments
 (0)