Commit d86a2320 authored by Louis Gesbert's avatar Louis Gesbert
Browse files

Enable downloading only the relevant artifacts (bc or js)

parent 11fa186b
......@@ -66,7 +66,7 @@ let () =
match get_encoded_token () with
| Some { arg_name = _; raw_arg = _; token } -> begin
let exercise_fetch =
retrieve (Learnocaml_api.Exercise (Some token, id))
retrieve (Learnocaml_api.Exercise (Some token, id, true))
in
init_tabs ();
exercise_fetch >>= fun (ex_meta, exo, _deadline) ->
......
......@@ -119,7 +119,7 @@ let () =
Js.string (id ^ " - " ^ "Learn OCaml" ^" v."^ Learnocaml_api.version);
let exercise_fetch =
token >>= fun token ->
retrieve (Learnocaml_api.Exercise (token, id))
retrieve (Learnocaml_api.Exercise (token, id, true))
in
let after_init top =
exercise_fetch >>= fun (_meta, exo, _deadline) ->
......
......@@ -484,7 +484,7 @@ let () =
| None -> ()
| Some ex_id ->
Lwt.async @@ fun () ->
retrieve (Learnocaml_api.Exercise (Some teacher_token, ex_id))
retrieve (Learnocaml_api.Exercise (Some teacher_token, ex_id, true))
>>= fun (meta, exo, _) ->
clear_tabs ();
let ans = SMap.find_opt ex_id save.Save.all_exercise_states in
......
......@@ -114,8 +114,8 @@ let fetch_lesson_index () =
let fetch_lesson id =
request_exn (Learnocaml_api.Lesson id)
let fetch_exercise token id =
request_exn (Learnocaml_api.Exercise (token,id))
let fetch_exercise token id js =
request_exn (Learnocaml_api.Exercise (token,id,js))
let fetch_tutorial_index () =
request_exn (Learnocaml_api.Tutorial_index ())
......
......@@ -24,7 +24,7 @@ exception Cannot_fetch of string
val request_exn: 'a Learnocaml_api.request -> 'a Lwt.t
val[@deprecated] fetch_exercise:
Token.t option -> Exercise.id -> (Exercise.Meta.t * Exercise.t * float option) Lwt.t
Token.t option -> Exercise.id -> bool -> (Exercise.Meta.t * Exercise.t * float option) Lwt.t
val[@deprecated] fetch_lesson_index: unit -> Lesson.Index.t Lwt.t
val[@deprecated] fetch_lesson : string -> Lesson.t Lwt.t
......
......@@ -484,7 +484,7 @@ let fetch server_url req =
| Error (`Failure s) -> Lwt.fail_with ("Server request failed: "^ s)
let fetch_exercise server_url token id =
Lwt.catch (fun () -> fetch server_url (Api.Exercise (token, id)))
Lwt.catch (fun () -> fetch server_url (Api.Exercise (token, id, false)))
@@ function
| Not_found ->
Printf.ksprintf Lwt.fail_with
......
......@@ -456,6 +456,17 @@ let field_from_file file files =
try File.(StringMap.find file.key files |> file.decode)
with Not_found -> raise File.(Missing_file file.key)
let strip need_js ex =
let f {cma; js} =
if need_js then {cma= ""; js} else {cma; js = ""}
in
{ ex with
compiled =
{ ex.compiled with
exercise_lib = f ex.compiled.exercise_lib;
test_lib = f ex.compiled.test_lib } }
module MakeReaderAnddWriter (Concur : Concur) = struct
module FileReader = File.MakeReader(Concur)
......
......@@ -110,6 +110,11 @@ val update: 'a File.file -> 'a -> t -> t
ciphers it. *)
val cipher: string File.file -> string -> t -> t
(** Selectively removes compiled data from an exercise.
If the first arg [js] is [true], keep only the javascript.
Otherwise, keep only the bytecode. *)
val strip: bool -> t -> t
(** Reader and decipherer *)
val read:
read_field:(string -> string option) ->
......
......@@ -449,17 +449,18 @@ module Request_handler = struct
| Api.Exercise_index None ->
lwt_fail (`Forbidden, "Forbidden")
| Api.Exercise (Some token, id) ->
| Api.Exercise (Some token, id, js) ->
(Exercise.Status.is_open id token >>= function
| `Open | `Deadline _ as o ->
Exercise.Meta.get id >>= fun meta ->
Exercise.get id >>= fun ex ->
let ex = Learnocaml_exercise.strip js ex in
respond_json cache
(meta, ex,
match o with `Deadline t -> Some (max t 0.) | `Open -> None)
| `Closed ->
lwt_fail (`Forbidden, "Exercise closed"))
| Api.Exercise (None, _) ->
| Api.Exercise (None, _, _) ->
lwt_fail (`Forbidden, "Forbidden")
| Api.Lesson_index () ->
......
......@@ -120,7 +120,8 @@ type _ request =
| Exercise_index:
'a token option -> (Exercise.Index.t * (Exercise.id * float) list) request
| Exercise:
'a token option * string -> (Exercise.Meta.t * Exercise.t * float option) request
'a token option * string * bool ->
(Exercise.Meta.t * Exercise.t * float option) request
| Lesson_index:
unit -> (string * string) list request
......@@ -166,7 +167,7 @@ let supported_versions
| Set_students_list (_, _)
| Students_csv (_, _, _)
| Exercise_index _
| Exercise (_, _)
| Exercise (_, _, _)
| Lesson_index _
| Lesson _
| Tutorial_index _
......@@ -334,10 +335,12 @@ module Conversions (Json: JSON_CODEC) = struct
| Exercise_index None ->
get ["exercise-index.json"]
| Exercise (Some token, id) ->
get ~token ("exercises" :: String.split_on_char '/' (id^".json"))
| Exercise (None, id) ->
get ("exercises" :: String.split_on_char '/' (id^".json"))
| Exercise (Some token, id, js) ->
let ext = if js then ".js.json" else ".bc.json" in
get ~token ("exercises" :: String.split_on_char '/' (id^ext))
| Exercise (None, id, js) ->
let ext = if js then ".js.json" else ".bc.json" in
get ("exercises" :: String.split_on_char '/' (id^ext))
| Lesson_index () ->
get ["lessons.json"]
......@@ -461,7 +464,15 @@ module Server (Json: JSON_CODEC) (Rh: REQUEST_HANDLER) = struct
(match token with
| Some token ->
let id = Filename.chop_suffix (String.concat "/" path) ".json" in
Exercise (Some token, id) |> k
let id_js = match Filename.chop_suffix_opt ~suffix:".bc" id with
| Some id -> Some (id, false)
| None -> match Filename.chop_suffix_opt ~suffix:".js" id with
| Some id -> Some (id, true)
| None -> None
in
(match id_js with
| Some (id, js) -> Exercise (Some token, id, js) |> k
| None -> Invalid_request "Missing bc/js extension" |> k)
| None -> Invalid_request "Missing token" |> k)
| Some "" ->
Static ["exercise.html"] |> k
......
......@@ -109,7 +109,8 @@ type _ request =
| Exercise_index:
'a token option -> (Exercise.Index.t * (Exercise.id * float) list) request
| Exercise:
'a token option * string -> (Exercise.Meta.t * Exercise.t * float option) request
'a token option * string * bool ->
(Exercise.Meta.t * Exercise.t * float option) request
| Lesson_index:
unit -> (string * string) list request
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment