From 95146f88cba6aa57379073a1507eddf2941a6e82 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Thu, 7 Sep 2023 00:10:58 +0200 Subject: [PATCH] Add `format` override Signed-off-by: Knut Ahlers --- README.md | 7 ++++--- main.go | 28 ++++++++++++++++++++++++---- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 153c405..ef6be4b 100644 --- a/README.md +++ b/README.md @@ -70,10 +70,11 @@ GET /job/{uuid}/download Download the resulting archive (You may specify an 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. - -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: +Supported query parameters: +- `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 { "download": "/job/fb5ee7b8-679a-4bf2-951b-acabaf43b43e/download", diff --git a/main.go b/main.go index 97e7052..30fd3e7 100644 --- a/main.go +++ b/main.go @@ -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) { vars := mux.Vars(r) uid, err := uuid.FromString(vars["uid"]) @@ -102,13 +119,13 @@ func downloadAssets(res http.ResponseWriter, r *http.Request) { filename string ) - switch r.Header.Get("Accept") { - case "application/tar", "application/x-tar", "application/x-gtar", "multipart/x-tar", "application/x-compress", "application/x-compressed": + switch dist := chooseDistribution(r); dist { + case "tar": contentType = "application/tar" content, err = buildAssetsTAR(uid) filename = uid.String() + ".tar" - case "application/pdf": + case "pdf": contentType = "application/pdf" filename = uid.String() + ".pdf" content, err = getAssetsFile(uid, ".pdf") @@ -119,9 +136,12 @@ func downloadAssets(res http.ResponseWriter, r *http.Request) { content, err = getAssetsFile(uid, ".log") } - default: + case "zip": content, err = buildAssetsZIP(uid) filename = uid.String() + ".zip" + + default: + err = errors.Errorf("unknown distribution %q", dist) } if err != nil {