Skip to content

Commit 7a84ebb

Browse files
committed
fix backend when it comes to images
1 parent 4c0d489 commit 7a84ebb

File tree

4 files changed

+34
-25
lines changed

4 files changed

+34
-25
lines changed

nightwatch/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "0.10.6"
1+
__version__ = "0.10.7"
22

33
import re
44
HEX_COLOR_REGEX = re.compile(r"^[A-Fa-f0-9]{6}$")

nightwatch/rics/__init__.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ async def connect_endpoint(
162162
while websocket.application_state == WebSocketState.CONNECTED:
163163
match await client.receive():
164164
case {"type": "message", "data": {"message": message}}:
165+
if not message.strip():
166+
await client.send({"type": "problem", "data": {"message": "You cannot send a blank message."}})
167+
continue
168+
165169
await app.state.broadcast({"type": "message", "data": {"user": client.serialize(), "message": message}})
166170

167171
case {"type": "user-list", "data": _}:
@@ -182,7 +186,6 @@ async def connect_endpoint(
182186
# Handle image forwarding
183187
SESSION = Session()
184188
PROXY_SIZE_LIMIT = 10 * (1024 ** 2)
185-
PROXY_ALLOWED_SUFFIX = ["avif", "avifs", "apng", "png", "jpeg", "jpg", "jfif", "webp", "ico", "gif", "svg"]
186189

187190
@app.get("/api/fwd/{public_url:str}", response_model = None)
188191
async def forward_image(public_url: str) -> Response | JSONResponse:
@@ -192,13 +195,6 @@ async def forward_image(public_url: str) -> Response | JSONResponse:
192195
except (binascii.Error, UnicodeDecodeError):
193196
return JSONResponse({"code": 400, "message": "Failed to contact the specified URI."}, status_code = 400)
194197

195-
filename = new_url.split("?")[0].split("/")[-1]
196-
if "." not in filename:
197-
return JSONResponse({"code": 400, "message": "Specified URI does not have an extension."}, status_code = 400)
198-
199-
if filename.split(".")[-1] not in PROXY_ALLOWED_SUFFIX:
200-
return JSONResponse({"code": 400, "message": "Specified URI has an unsupported extension."}, status_code = 400)
201-
202198
try:
203199
data = b""
204200
with SESSION.get(new_url, stream = True) as response:
@@ -213,7 +209,7 @@ async def forward_image(public_url: str) -> Response | JSONResponse:
213209
response.status_code,
214210
{
215211
k: v
216-
for k, v in response.headers.items() if k in ["Content-Type", "Content-Length", "Cache-Control"]
212+
for k, v in response.headers.items() if k in ["Content-Type", "Cache-Control"]
217213
}
218214
)
219215

nightwatch/web/css/main.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ input:hover {
103103
padding-top: 5px;
104104
padding-bottom: 5px;
105105
}
106+
.message-content.has-image > span > a {
107+
display: flex;
108+
align-items: center;
109+
}
106110
.message-content > a > img {
107111
max-width: 500px;
108112
}

nightwatch/web/js/nightwatch.js

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const leftmark_rules = [
99
{ regex: /__(.*?)__/g, replace: "<u>$1</u>" },
1010
{ regex: /~~(.*?)~~/g, replace: "<s>$1</s>" },
1111
{ regex: /\*(.*?)\*/g, replace: "<em>$1</em>" },
12+
{ regex: /\!\[(.*?)\]\((.*?)\)/g, replace: `<a href = "$2" target = "_blank"><img alt = "$1" src = "$2"></a>` },
1213
{ regex: /\[(.*?)\]\((.*?)\)/g, replace: `<a href = "$2" target = "_blank" rel = "noreferrer">$1</a>` }
1314
];
1415

@@ -86,21 +87,29 @@ const NOTIFICATION_SFX = new Audio("/audio/notification.mp3");
8687
// Construct text/attachment
8788
let attachment = message.message, classlist = "message-content";
8889
if (attachment.toLowerCase().match(/^https:\/\/[\w\d./-]+.(?:avifs?|a?png|jpe?g|jfif|webp|ico|gif|svg)(?:\?.+)?$/)) {
89-
const url = `http${connection.protocol}://${address}/api/fwd/${btoa(attachment.slice(8))}`;
90-
attachment = `<a href = "${url}" target = "_blank"><img src = "${url}"></a>`;
91-
classlist += " has-image";
92-
} else {
93-
94-
// Clean attachment for the love of god
95-
const cleaned = attachment.replace(/&/g, "&amp;")
96-
.replace(/</g, "&lt;")
97-
.replace(/>/g, "&gt;")
98-
.replace(/"/g, "&quot;")
99-
.replace(/"/g, "&#039;");
100-
101-
// Apply leftmark
102-
attachment = leftmark(cleaned);
103-
if (cleaned !== attachment) attachment = `<span>${attachment}</span>`;
90+
attachment = `![untitled](${attachment})`;
91+
}
92+
93+
// Clean attachment for the love of god
94+
const cleaned = attachment.replace(/&/g, "&amp;")
95+
.replace(/</g, "&lt;")
96+
.replace(/>/g, "&gt;")
97+
.replace(/"/g, "&quot;")
98+
.replace(/"/g, "&#039;");
99+
100+
// Apply leftmark
101+
attachment = leftmark(cleaned);
102+
if (cleaned !== attachment) {
103+
attachment = `<span>${attachment}</span>`;
104+
const dom = new DOMParser().parseFromString(attachment, "text/html");
105+
106+
// Handle image adjusting
107+
const image = dom.querySelector("img");
108+
if (image) {
109+
classlist += " has-image";
110+
image.src = `http${connection.protocol}://${address}/api/fwd/${btoa(image.src.slice(8))}`;
111+
attachment = dom.body.innerHTML;
112+
};
104113
};
105114

106115
// Construct message

0 commit comments

Comments
 (0)