ntfy icon indicating copy to clipboard operation
ntfy copied to clipboard

Support relative paths for attachments

Open Curid opened this issue 2 years ago • 4 comments

"base-url" is the root URL for the ntfy server; this is needed for the generated attachment URLs

If the client knows the URL of the server, why does the baseURL need to be configured manually?

Curid avatar May 11 '22 18:05 Curid

There are many reasons, not all of them are rock solid. Here's the gist of it:

  • When you publish a message via PUT/POST, the client doesn't pass the base URL in the HTTP request. Especially if passing through a proxy, the server would have to re-assemble the URL based on the HTTP Host header and the protocol. The re-assembled URL doesn't always match the publicly available URL. For instance, the ntfy.sh server runs on an HTTP port, but is proxied through nginx on HTTPS, so for all that the ntfy server is concerned, it's on HTTP, though that is not true.
  • Other entry points (such as SMTP) don't even have the Host header, so they absolutely need a base URL.
  • We could solve this problem by returning the relative URL path (e.g /file/abcdefg.jpg instead of https://...), but then the clients would have to differentiate between relative and absolute URLs. Setting the base URL in the server config seems much easier than that.

Makes sense? Why are you asking about this?

binwiederhier avatar May 11 '22 19:05 binwiederhier

When you publish a message via PUT/POST, the client doesn't pass the base URL in the HTTP request.

Does the server need to know the base URL for messages to be published?

Makes sense? Why are you asking about this?

I want to embed ntfy in a web app and I've been careful to make all paths in the app relative so users wouldn't need to specify a baseURL.

How about putting @ in front of relative paths and have the clients replace it?

if path[0] == "@" {
  path = calculatedPath + "/" + path[1:]
}

Curid avatar May 11 '22 19:05 Curid

Does the server need to know the base URL for messages to be published?

If you check for where config.BaseURL is used, you can see that it's in two places:

  • To calculate the attachment URL
  • To calculate the topic URL (in the email footer)

The attachment URL is stored in the DB and returned to the client. So the base URL has to be known to the server at time of publishing. If you don't use either of these features, you don't need to specify it.

I want to embed ntfy in a web app and I've been careful to make all paths in the app relative

The web app will not work unless it's at the doc root. I've tried very hard to make it relative in React, but I couldn't make it work and eventually gave up.

binwiederhier avatar May 11 '22 19:05 binwiederhier

@binwiederhier I need some help to implement this.

In the WebUI I'm thinking of replacing the @ before they are saved in the database here Does there need to be 2 methods for adding notifications or can I combine them into one?

For the Android side I have no idea.

Curid avatar Jun 06 '22 17:06 Curid