Simplify job_run_file and return 404

This commit is contained in:
Reynir Björnsson 2020-12-22 13:36:14 +01:00
parent bf1777b7cb
commit f2ab0d797f

View file

@ -64,29 +64,32 @@ let routes (t : Model.t) =
let job = Router.param req "job" let job = Router.param req "job"
and run = Router.param req "run" and run = Router.param req "run"
and file = Router.splat req |> String.concat "/" in and file = Router.splat req |> String.concat "/" in
let+ job_run = match safe_seg job, safe_seg run, Fpath.of_string file with
Lwt_result.lift (safe_seg job) >>= fun job -> | Error (`Msg e), _, _ | _, Error (`Msg e), _ | _, _, Error (`Msg e) ->
Lwt_result.lift (safe_seg run) >>= fun run -> Log.debug (fun m -> m "bad path: %s" e);
Model.read_full_with_digests t job run in Response.of_plain_text ~status:`Not_found "File not found"
match job_run with |> Lwt.return
| Error (`Msg e) -> | Ok job, Ok run, Ok filep ->
Log.warn (fun m -> m "Error getting job run: %s" e); let+ job_run = Model.read_full_with_digests t job run in
Response.of_plain_text ~status:`Internal_server_error match job_run with
"Error getting job run" | Error (`Msg e) ->
| Ok (job_run, digests) -> Log.warn (fun m -> m "Error getting job run: %s" e);
match List.find_opt (fun (p, _) -> Fpath.(equal (v file) p)) job_run.data with Response.of_plain_text ~status:`Internal_server_error
| None -> "Error getting job run"
Log.debug (fun m -> m "Trying to get non-existent build artifact %s" | Ok (job_run, digests) ->
file); match List.find_opt (fun (p, _) -> Fpath.(equal filep p)) job_run.data with
Response.of_plain_text ~status:`Not_found | None ->
("build artifact not found: " ^ file) Log.debug (fun m -> m "Trying to get non-existent build artifact %s"
| Some (path, data) -> file);
(* Should never fail if caching is not broken, or 'full' file untampered *) Response.of_plain_text ~status:`Not_found
let digest = snd (List.find (fun (p, _) -> Fpath.equal path p) digests) in ("build artifact not found: " ^ file)
let body = Body.of_string data in | Some (path, data) ->
Response.make ~body () (* Should never fail if caching is not broken, or 'full' file untampered *)
|> Response.add_header ("Content-type", Magic_mime.lookup file) let digest = snd (List.find (fun (p, _) -> Fpath.equal path p) digests) in
|> Response.set_etag (Base64.encode_string digest.sha256) let body = Body.of_string data in
Response.make ~body ()
|> Response.add_header ("Content-type", Magic_mime.lookup file)
|> Response.set_etag (Base64.encode_string digest.sha256)
in in
[ [