Current section

44 Versions

Jump to

Compare versions

3 files changed
+24 additions
-15 deletions
  @@ -39,4 +39,4 @@
39 39 {<<"optional">>,false},
40 40 {<<"repository">>,<<"hexpm">>},
41 41 {<<"requirement">>,<<"~> 1.1">>}]]}.
42 - {<<"version">>,<<"0.1.2">>}.
42 + {<<"version">>,<<"0.1.3">>}.
  @@ -9,6 +9,7 @@ defmodule Fluminus.API.File do
9 9 * `:directory?` - whether this file is a directory
10 10 * `:children` - `nil` indicated the need to fetch, otherwise it contains a list of its children.
11 11 if `directory?` is `false`, then this field contains an empty list.
12 + * `:allow_upload?` - whether this is a student submission folder.
12 13 """
13 14
14 15 alias Fluminus.{API, Authorization}
  @@ -18,9 +19,10 @@ defmodule Fluminus.API.File do
18 19 id: String.t(),
19 20 name: String.t(),
20 21 directory?: bool(),
21 - children: [__MODULE__.t()] | nil
22 + children: [__MODULE__.t()] | nil,
23 + allow_upload?: bool()
22 24 }
23 - defstruct ~w(id name directory? children)a
25 + defstruct ~w(id name directory? children allow_upload?)a
24 26
25 27 @doc """
26 28 Creates `#{__MODULE__}` struct from a `Module`.
  @@ -31,7 +33,8 @@ defmodule Fluminus.API.File do
31 33 id: id,
32 34 name: sanitise_filename(code),
33 35 directory?: true,
34 - children: get_children(id, auth)
36 + children: get_children(id, auth, false),
37 + allow_upload?: false
35 38 }
36 39 end
37 40
  @@ -39,8 +42,11 @@ defmodule Fluminus.API.File do
39 42 Loads the children of a given `#{__MODULE__}` struct.
40 43 """
41 44 @spec load_children(__MODULE__.t(), Authorization.t()) :: __MODULE__.t()
42 - def load_children(file = %__MODULE__{id: id, directory?: true, children: nil}, auth = %Authorization{}) do
43 - %__MODULE__{file | children: get_children(id, auth)}
45 + def load_children(
46 + file = %__MODULE__{id: id, directory?: true, children: nil, allow_upload?: allow_upload?},
47 + auth = %Authorization{}
48 + ) do
49 + %__MODULE__{file | children: get_children(id, auth, allow_upload?)}
44 50 end
45 51
46 52 def load_children(file = %__MODULE__{directory?: false, children: nil}, _auth) do
  @@ -104,12 +110,14 @@ defmodule Fluminus.API.File do
104 110 end
105 111 end
106 112
107 - @spec get_children(String.t(), Authorization.t()) :: [__MODULE__.t()]
108 - defp get_children(id, auth = %Authorization{}) when is_binary(id) do
113 + @spec get_children(String.t(), Authorization.t(), bool()) :: [__MODULE__.t()]
114 + defp get_children(id, auth = %Authorization{}, allow_upload?) when is_binary(id) and is_boolean(allow_upload?) do
109 115 {:ok, %{"data" => directory_children_data}} = API.api(auth, "/files/?ParentID=#{id}")
110 - {:ok, %{"data" => files_children_data}} = API.api(auth, "/files/#{id}/file")
111 116
112 - Enum.map(directory_children_data ++ files_children_data, &parse_child/1)
117 + {:ok, %{"data" => files_children_data}} =
118 + API.api(auth, "/files/#{id}/file#{if allow_upload?, do: "?populate=Creator", else: ""}")
119 +
120 + Enum.map(directory_children_data ++ files_children_data, &parse_child(&1, allow_upload?))
113 121 end
114 122
115 123 @spec sanitise_filename(String.t()) :: String.t()
  @@ -119,15 +127,16 @@ defmodule Fluminus.API.File do
119 127 String.replace(name, ~r|[/\0]|, "-")
120 128 end
121 129
122 - @spec parse_child(map()) :: __MODULE__.t()
123 - defp parse_child(child = %{"id" => id, "name" => name}) do
130 + @spec parse_child(map(), bool()) :: __MODULE__.t()
131 + defp parse_child(child = %{"id" => id, "name" => name}, add_creator_name?) when is_boolean(add_creator_name?) do
124 132 directory? = is_map(child["access"])
125 133
126 134 %__MODULE__{
127 135 id: id,
128 - name: sanitise_filename(name),
136 + name: sanitise_filename("#{if add_creator_name?, do: "#{child["creatorName"]} - ", else: ""}#{name}"),
129 137 directory?: directory?,
130 - children: if(directory?, do: nil, else: [])
138 + children: if(directory?, do: nil, else: []),
139 + allow_upload?: (if child["allowUpload"], do: true, else: false)
131 140 }
132 141 end
133 142 end
  @@ -4,7 +4,7 @@ defmodule Fluminus.MixProject do
4 4 def project do
5 5 [
6 6 app: :fluminus,
7 - version: "0.1.2",
7 + version: "0.1.3",
8 8 elixir: "~> 1.6",
9 9 start_permanent: Mix.env() == :prod,
10 10 deps: deps(),