feat(attach): absolute public URL from config

- attach.AbsolutePublicURL builds the absolute public URL of a blob key from
  storage.uploads.public_url, or from app.url and the public path prefix
- it reads the config only, not the values OpenBucket recorded, so a job
  worker that opened no bucket gets the same URL as serve
- attach.ErrNoAbsoluteURL when neither base is configured

Plan 35-01, task 5.
This commit is contained in:
Jakub Zych
2026-11-13 11:01:43 +01:00
parent f8fa0f1b4b
commit 935e4bf64f
3 changed files with 62 additions and 1 deletions

View File

@@ -95,6 +95,15 @@ The `/storage/uploads` route checks `is_public` before it serves a file; a publi
Leave `public_url` unset to keep the URLs under `public_path_prefix`. With `storage.uploads.serve` on, those URLs go through the [Serving files](#serving-files) route, which works for an S3 bucket too, and private files stay protected.
### Absolute URLs for mail
Without `public_url`, `attach.PublicURL` returns a path on the application's own host, which is right for a page and useless in a mail. `attach.AbsolutePublicURL` builds an absolute URL from the configuration instead:
1. `storage.uploads.public_url`, one slash and the blob key, when it is set;
2. otherwise `app.url`, `storage.uploads.public_path_prefix` and the blob key, for example `https://acme.example.com/storage/uploads/5f1/d0c/2e9/5f1d0c2e9a7b4c3d8e6f.jpg`.
When neither `public_url` nor an absolute `http` or `https` `app.url` is set, it returns `attach.ErrNoAbsoluteURL`. It takes the `*compass.Config` and reads nothing `attach.OpenBucket` recorded, so a process that never opened the bucket, such as a `queue:work` worker sending mail, gets the same URL as `serve`. With `app.url`, the files are reachable only when [Serving files](#serving-files) is on or a web server serves that path.
## Serving files
The framework mounts no file route unless you ask for one. Set `storage.uploads.serve` to `true` (environment `SUMMER_STORAGE__UPLOADS__SERVE=true`, default `false`) and the router registers `GET <public_path_prefix>/{path...}` on `attach.StaticHandlerPublic`: