1
0
mirror of https://github.com/Luzifer/tex-api.git synced 2024-09-19 01:22:57 +00:00

Add format override

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2023-09-07 00:10:58 +02:00
parent ab63204ee8
commit 95146f88cb
Signed by: luzifer
GPG Key ID: D91C3E91E4CAD6F5
2 changed files with 28 additions and 7 deletions

View File

@ -70,10 +70,11 @@ GET /job/{uuid}/download Download the resulting archive (You may specify an
TAR archive or just the raw PDF.) TAR archive or just the raw PDF.)
``` ```
All routes accept the `log-on-error` parameter: If set a PDF download (`Accept` header set to `application/pdf`) will return the log instead of the PDF if no PDF is found. Supported query parameters:
Providing `report-urls` parameter to the `POST /jobs` endpoint will not redirect to the wait endpoint but report a JSON object containing the URL paths:
- `format` overrides the `Accept` header detection and choses the output format (`pdf`, `tar`, `zip`) and is accepted on all endpoints
- `log-on-error` - If set a PDF download (`Accept` header set to `application/pdf` or `format` is chosen) will return the log instead of the PDF if no PDF is found.
- `report-urls` on the `POST /jobs` endpoint will not redirect to the wait endpoint but report a JSON object containing the URL paths:
```json ```json
{ {
"download": "/job/fb5ee7b8-679a-4bf2-951b-acabaf43b43e/download", "download": "/job/fb5ee7b8-679a-4bf2-951b-acabaf43b43e/download",

28
main.go
View File

@ -88,6 +88,23 @@ func main() {
} }
} }
func chooseDistribution(r *http.Request) string {
if dist := r.URL.Query().Get("format"); dist != "" {
return dist
}
switch r.Header.Get("Accept") {
case "application/tar", "application/x-tar", "application/x-gtar", "multipart/x-tar", "application/x-compress", "application/x-compressed":
return "tar"
case "application/pdf":
return "pdf"
default:
return "zip"
}
}
func downloadAssets(res http.ResponseWriter, r *http.Request) { func downloadAssets(res http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
uid, err := uuid.FromString(vars["uid"]) uid, err := uuid.FromString(vars["uid"])
@ -102,13 +119,13 @@ func downloadAssets(res http.ResponseWriter, r *http.Request) {
filename string filename string
) )
switch r.Header.Get("Accept") { switch dist := chooseDistribution(r); dist {
case "application/tar", "application/x-tar", "application/x-gtar", "multipart/x-tar", "application/x-compress", "application/x-compressed": case "tar":
contentType = "application/tar" contentType = "application/tar"
content, err = buildAssetsTAR(uid) content, err = buildAssetsTAR(uid)
filename = uid.String() + ".tar" filename = uid.String() + ".tar"
case "application/pdf": case "pdf":
contentType = "application/pdf" contentType = "application/pdf"
filename = uid.String() + ".pdf" filename = uid.String() + ".pdf"
content, err = getAssetsFile(uid, ".pdf") content, err = getAssetsFile(uid, ".pdf")
@ -119,9 +136,12 @@ func downloadAssets(res http.ResponseWriter, r *http.Request) {
content, err = getAssetsFile(uid, ".log") content, err = getAssetsFile(uid, ".log")
} }
default: case "zip":
content, err = buildAssetsZIP(uid) content, err = buildAssetsZIP(uid)
filename = uid.String() + ".zip" filename = uid.String() + ".zip"
default:
err = errors.Errorf("unknown distribution %q", dist)
} }
if err != nil { if err != nil {