From 0b27af3dab56b667b50fd80590cdfd9df0feffbd Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Mon, 5 Nov 2018 15:40:18 +0100 Subject: File progress --- shardweb/lib/controllers/directory_controller.ex | 31 ++++++++++++++++++++++++ shardweb/lib/router.ex | 2 ++ shardweb/lib/templates/directory/view.html.eex | 30 ++++++++++++++++++++++- shardweb/lib/templates/identity/view.html.eex | 2 ++ shardweb/lib/templates/page/shard_entry.html.eex | 11 +++++++++ shardweb/mix.exs | 2 ++ shardweb/mix.lock | 1 + 7 files changed, 78 insertions(+), 1 deletion(-) (limited to 'shardweb') diff --git a/shardweb/lib/controllers/directory_controller.ex b/shardweb/lib/controllers/directory_controller.ex index 10fdec2..ad5c426 100644 --- a/shardweb/lib/controllers/directory_controller.ex +++ b/shardweb/lib/controllers/directory_controller.ex @@ -61,4 +61,35 @@ defmodule ShardWeb.DirectoryController do redirect conn, to: directory_path(conn, (if dir_public do :view_pub else :view_priv end), conn.assigns.pk|>Base.encode16, dir_name) end + + def dir_upload(conn, %{"dir_name" => dir_name, "dir_public" => dir_public, "upload_name" => upload_name, "file" => file}) do + %Plug.Upload{content_type: mime_type, filename: filename, path: path} = file + + dir_public = (dir_public == "true") + + manifest = %SApp.Directory.Manifest{public: dir_public, owner: conn.assigns.pk, name: dir_name} + shard = SData.term_hash manifest + pid = Shard.Manager.find_or_start manifest + + {:ok, file_manifest, _file_pid} = SApp.File.create(path, mime_type) + name_as = if upload_name == "" do filename else upload_name end + SApp.Directory.add_item(pid, name_as, file_manifest, true) + + redirect conn, to: directory_path(conn, (if dir_public do :view_pub else :view_priv end), conn.assigns.pk|>Base.encode16, dir_name) + end + + def raw_file(conn, %{"infohash" => infohash}) do + infohash = Base.decode16!(infohash) + pid = Shard.Manager.find_or_start %SApp.File.Manifest{infohash: infohash} + info = SApp.File.get_info(pid) + if info[:missing_blocks] == 0 do + conn + |> put_resp_content_type(info[:mime_type]) + |> send_file(200, info[:path]) + else + conn + |> put_status(:not_found) + |> text("File not found or not yet completely downloaded.\n#{inspect(info)}") + end + end end diff --git a/shardweb/lib/router.ex b/shardweb/lib/router.ex index 3f78afd..a6a61f9 100644 --- a/shardweb/lib/router.ex +++ b/shardweb/lib/router.ex @@ -34,6 +34,8 @@ defmodule ShardWeb.Router do post "/dir/add", DirectoryController, :dir_add post "/dir/rm", DirectoryController, :dir_rm post "/dir/set_stored", DirectoryController, :dir_set_stored + post "/dir/upload", DirectoryController, :dir_upload + get "/raw/:infohash", DirectoryController, :raw_file get "/chat/:chan", ChatController, :chat get "/pm/:people_list", ChatController, :privchat diff --git a/shardweb/lib/templates/directory/view.html.eex b/shardweb/lib/templates/directory/view.html.eex index ef8200b..7b9be38 100644 --- a/shardweb/lib/templates/directory/view.html.eex +++ b/shardweb/lib/templates/directory/view.html.eex @@ -76,6 +76,34 @@ <%= if @owner == @pk do %> + <%= if @public do %> +
Upload file + <%= form_for @conn, directory_path(@conn, :dir_upload), [multipart: true, class: "form-horizontal"], fn f -> %> + <%= hidden_input f, :dir_name, value: @name %> + <%= hidden_input f, :dir_public, value: @public %> +
+ <%= label :file, "File:", class: ["col-sm-2 control-label"] %> +
+ <%= file_input f, :file, [class: "form-control"] %> +
+
+
+ <%= label :upload_name, "Save as (optionnal):", class: ["col-sm-2 control-label"] %> +
+ <%= text_input f, :upload_name, [class: "form-control", value: ""] %> +
+
+
+
+ <%= submit "Upload file", [class: "btn btn-primary"] %> +
+
+ <% end %> +
+ <% else %> +

Warning: file upload in private directories does not keeps file private (yeah WTF right?), so we don't let you do that.

+ <% end %> +
Add existing item <%= form_for @conn, directory_path(@conn, :dir_add), [class: "form-horizontal"], fn f -> %> <%= hidden_input f, :dir_name, value: @name %> @@ -106,7 +134,7 @@
- <%= submit "Add", [class: "btn btn-default"] %> + <%= submit "Add", [class: "btn btn-primary"] %>
<% end %> diff --git a/shardweb/lib/templates/identity/view.html.eex b/shardweb/lib/templates/identity/view.html.eex index c5dfaf2..c81caf6 100644 --- a/shardweb/lib/templates/identity/view.html.eex +++ b/shardweb/lib/templates/identity/view.html.eex @@ -23,6 +23,8 @@ PM <% end %> + "> Public stuff +
   <%= inspect(SApp.Identity.get_info(@pid), pretty: true, width: 40) %>
 
diff --git a/shardweb/lib/templates/page/shard_entry.html.eex b/shardweb/lib/templates/page/shard_entry.html.eex index d99fc34..bc9edd4 100644 --- a/shardweb/lib/templates/page/shard_entry.html.eex +++ b/shardweb/lib/templates/page/shard_entry.html.eex @@ -23,6 +23,17 @@ <%= name %> <%= if public do %><% else %><% end %> + <% %SApp.File.Manifest{infohash: infohash} -> %> + <% file_info = SApp.File.get_info(Shard.Manager.find_or_start(@manifest)) %> + + <%= if file_info[:size] != nil do Size.humanize!(file_info[:size]) end %> + <%= if file_info[:mime_type] != nil do %><%= file_info[:mime_type] %><% end %> + <%= if file_info[:missing_blocks] != nil and file_info[:missing_blocks] > 0 do %> + <%= file_info[:num_blocks] - file_info[:missing_blocks] %> / <%= file_info[:num_blocks] %> + <% end %> + <%= if file_info[:missing_blocks] == 0 do %> + [raw] + <% end %> <% x -> %> <%= inspect x %> <% end %> diff --git a/shardweb/mix.exs b/shardweb/mix.exs index 7e4b0b2..90fc530 100644 --- a/shardweb/mix.exs +++ b/shardweb/mix.exs @@ -41,6 +41,8 @@ defmodule ShardWeb.Mixfile do {:cowboy, "~> 1.0"}, {:phoenix_gon, "~> 0.4.0"}, + {:size, "~> 0.1.0"}, + {:shard, path: Path.expand (Path.join [System.cwd!, "..", "shard"])}, ] end diff --git a/shardweb/mix.lock b/shardweb/mix.lock index 71267a0..fe97f67 100644 --- a/shardweb/mix.lock +++ b/shardweb/mix.lock @@ -16,4 +16,5 @@ "poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], [], "hexpm"}, "ranch": {:hex, :ranch, "1.3.2", "e4965a144dc9fbe70e5c077c65e73c57165416a901bd02ea899cfd95aa890986", [:rebar3], [], "hexpm"}, "salty": {:hex, :libsalty, "0.1.3", "13332eb13ac995f5deb76903b44f96f740e1e3a6e511222bffdd8b42cd079ffb", [:make, :mix], [{:elixir_make, "~> 0.4", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm"}, + "size": {:hex, :size, "0.1.0", "49c4bc026286b1f66f9d6b78756e833150b0b86b640d8e3fd5ad597e705dc846", [:mix], [], "hexpm"}, } -- cgit v1.2.3