Skip to content

Commit 08cce87

Browse files
Förbättringar i HelloModel m.m
1 parent 2607d6f commit 08cce87

3 files changed

Lines changed: 50 additions & 22 deletions

File tree

src/main/java/com/example/HelloModel.java

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -83,25 +83,41 @@ public boolean sendImage(File imageFile) {
8383

8484
private void receiveMessages() {
8585
connection.receive(dto -> {
86-
if (dto.message() == null || dto.message().isBlank()) return;
87-
if (dto.message().contains("\"clientId\":\"" + clientId + "\"")) {
86+
String msg = dto.message();
87+
if (msg == null || msg.isBlank()) return;
88+
89+
// Ignorera egna meddelanden
90+
if (msg.contains("\"clientId\":\"" + clientId + "\"")) {
8891
return;
8992
}
90-
if (dto.message().startsWith("![Bild](") || dto.message().contains("\n![Bild](")) {
91-
String url = dto.message().replaceAll(".*!\\[Bild\\]\\(([^)]+)\\).*", "$1");
92-
addMessageSafely(new NtfyMessageDto(dto.id(),
93-
dto.time(),
94-
dto.event(),
95-
dto.topic(),
96-
null,
97-
null,
98-
url));
99-
return;
93+
94+
// Identifiera Markdown-bild via enkel substring-sökning
95+
int marker = msg.indexOf("![Bild](");
96+
if (marker != -1) {
97+
int open = msg.indexOf('(', marker);
98+
int close = msg.indexOf(')', open + 1);
99+
100+
if (open != -1 && close != -1) {
101+
String url = msg.substring(open + 1, close);
102+
addMessageSafely(new NtfyMessageDto(
103+
dto.id(),
104+
dto.time(),
105+
dto.event(),
106+
dto.topic(),
107+
null,
108+
null,
109+
url
110+
));
111+
return;
112+
}
100113
}
114+
115+
// Ren text
101116
addMessageSafely(dto);
102117
});
103118
}
104119

120+
105121
private void addMessageSafely(NtfyMessageDto msg) {
106122
if (Platform.isFxApplicationThread()) {
107123
messages.add(msg);
@@ -122,17 +138,16 @@ protected String uploadToLocalServer(File imageFile) throws IOException {
122138

123139
int status = conn.getResponseCode();
124140
if (status < 200 || status >= 300) {
125-
conn.disconnect();
126-
throw new IOException("Upload failed with HTTP status: " + status);
141+
throw new IOException("Upload failed with status " + status);
127142
}
128143

129144
try (InputStream in = conn.getInputStream()) {
130-
String imageUrl = new String(in.readAllBytes());
131-
return imageUrl;
145+
return new String(in.readAllBytes());
132146
} finally {
133147
conn.disconnect();
134148
}
135149
}
136150

137151

152+
138153
}

src/main/java/com/example/NtfyConnectionImpl.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,31 @@ public boolean sendImage(File imageFile, String clientId) {
5858
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
5959
conn.setRequestProperty("X-Client-Id", clientId);
6060

61+
// JSON-del
62+
var json = mapper.writeValueAsString(
63+
new java.util.LinkedHashMap<String,Object>() {{
64+
put("clientId", clientId);
65+
put("type", "image");
66+
put("fileName", imageFile.getName());
67+
}}
68+
);
69+
6170
try (OutputStream output = conn.getOutputStream();
6271
PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, StandardCharsets.UTF_8), true)) {
6372

64-
// textdel med JSON
6573
writer.append("--").append(boundary).append("\r\n");
6674
writer.append("Content-Disposition: form-data; name=\"message\"\r\n\r\n");
67-
writer.append("{\"clientId\":\"").append(clientId).append("\",\"type\":\"image\",\"fileName\":\"")
68-
.append(imageFile.getName()).append("\"}\r\n");
75+
writer.append(json).append("\r\n");
6976
writer.flush();
7077

71-
// bildfil
78+
// Bildfil
79+
String contentType = Files.probeContentType(imageFile.toPath());
80+
if (contentType == null) contentType = "application/octet-stream";
81+
7282
writer.append("--").append(boundary).append("\r\n");
7383
writer.append("Content-Disposition: form-data; name=\"file\"; filename=\"")
7484
.append(imageFile.getName()).append("\"\r\n");
75-
writer.append("Content-Type: ").append(Files.probeContentType(imageFile.toPath())).append("\r\n\r\n");
85+
writer.append("Content-Type: ").append(contentType).append("\r\n\r\n");
7686
writer.flush();
7787

7888
Files.copy(imageFile.toPath(), output);
@@ -92,6 +102,7 @@ public boolean sendImage(File imageFile, String clientId) {
92102
}
93103

94104

105+
95106
@Override
96107
public void receive(Consumer<NtfyMessageDto> messageHandler) {
97108
Thread thread = new Thread(() -> {

src/test/java/com/example/NtfyConnectionSpy.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ public void stopReceiving() { }
4242
*/
4343
public void simulateIncomingMessage(String json) {
4444
if (messageHandler != null) {
45+
long now = System.currentTimeMillis() / 1000;
46+
4547
NtfyMessageDto dto = new NtfyMessageDto(
4648
"test-id",
47-
System.currentTimeMillis(),
49+
now,
4850
"message",
4951
HelloModel.DEFAULT_TOPIC,
5052
json.contains("\"message\"") ? json.replaceAll(".*\"message\":\"([^\"]+)\".*", "$1") : null,

0 commit comments

Comments
 (0)