Custom hash > file server

Not trying to solve a particular error here, just looking for more guidance on feasibility on Caddy.

What I’m trying to achieve is a hash-based URL system similar to Google Photos, where a randomized hash is determined for a file and those with that link would be able to access that file.

So my thinking the way this could work would be:

  1. Monitor a folder for changes
  2. Once a file is modified/added, generate the hash for that file.
  3. Update the running Caddy instance with that new endpoint and point that it should consider that endpoint related to that file .
  4. File should be served and all other config considered.

It doesn’t seem like file_server or the file request matcher would be able to do this as all seem to expect a whole directory of files, of which, the path would exactly match the file name.

Would writing my own plugin here make more sense and be the correct integration point to use Caddy?

I’m not sure I understand… what is a specific example of a request/response flow you want to achieve?

No worries @matt. Let me try again.

The most succinct tl;dr would be, I want to programmatically update Caddy with custom and arbitrary paths to point to files in a given directory but I don’t want their actual file name to serve those files.

An example, I have the following directory /data with one file in it:

/data

example.txt

I want to have Caddy host the /data directory, but not https://caddy.server/example.txt. I want to specify the path to serve example.txt to be something like https://caddy.server/abc_1234 to serve the file.

Does that make more sense?

I see, so user requests https://example.com/abc_1234 and Caddy actually serves up https://example.com/data/example.txt? That’s what rewrite is for:

rewrite /abc_1234 /data/example.txt

Ahh, I think this could work.

So in combination, I think I’d put all the files in /files and then use handle_path to 404 all requests to /files/<filename> but then apply rules for rewrite to map /links/abc_1234 to say /files/example.txt, would this work or would the handle_path then cause that rewritten request to 404?

I want to block direct access to the files and only allow the redirect path to succeed is there a doc that explains the precedence of the directives/rules?

This topic was automatically closed after 30 days. New replies are no longer allowed.