diff options
author | Alex Auvolat <alex@adnab.me> | 2018-11-05 15:40:18 +0100 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2018-11-05 15:40:18 +0100 |
commit | 0b27af3dab56b667b50fd80590cdfd9df0feffbd (patch) | |
tree | 7c1185496166b04aa21ea66f285650d62e954f20 /shardweb/lib | |
parent | 72906c6bb473ea605235c84b6d01c318f7b6cef8 (diff) | |
download | shard-0b27af3dab56b667b50fd80590cdfd9df0feffbd.tar.gz shard-0b27af3dab56b667b50fd80590cdfd9df0feffbd.zip |
File progress
Diffstat (limited to 'shardweb/lib')
-rw-r--r-- | shardweb/lib/controllers/directory_controller.ex | 31 | ||||
-rw-r--r-- | shardweb/lib/router.ex | 2 | ||||
-rw-r--r-- | shardweb/lib/templates/directory/view.html.eex | 30 | ||||
-rw-r--r-- | shardweb/lib/templates/identity/view.html.eex | 2 | ||||
-rw-r--r-- | shardweb/lib/templates/page/shard_entry.html.eex | 11 |
5 files changed, 75 insertions, 1 deletions
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 @@ </table> <%= if @owner == @pk do %> + <%= if @public do %> + <fieldset><legend>Upload file</legend> + <%= 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 %> + <div class="form-group"> + <%= label :file, "File:", class: ["col-sm-2 control-label"] %> + <div class="col-sm-10"> + <%= file_input f, :file, [class: "form-control"] %> + </div> + </div> + <div class="form-group"> + <%= label :upload_name, "Save as (optionnal):", class: ["col-sm-2 control-label"] %> + <div class="col-sm-10"> + <%= text_input f, :upload_name, [class: "form-control", value: ""] %> + </div> + </div> + <div class="form-group"> + <div class="col-sm-offset-2 col-sm-10"> + <%= submit "Upload file", [class: "btn btn-primary"] %> + </div> + </div> + <% end %> + </fieldset> + <% else %> + <p>Warning: file upload in private directories does not keeps file private (yeah WTF right?), so we don't let you do that.</p> + <% end %> + <fieldset><legend>Add existing item</legend> <%= form_for @conn, directory_path(@conn, :dir_add), [class: "form-horizontal"], fn f -> %> <%= hidden_input f, :dir_name, value: @name %> @@ -106,7 +134,7 @@ <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> - <%= submit "Add", [class: "btn btn-default"] %> + <%= submit "Add", [class: "btn btn-primary"] %> </div> </div> <% 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 @@ <a class="btn btn-s btn-primary" href="<%= chat_path(@conn, :privchat, @pk |> Base.encode16) %>"><i class="fa fa-comments"></i> PM</a> <% end %> + <a class="btn btn-s btn-success" href="<%= directory_path(@conn, :view_pub, @pk |> Base.encode16, "collection") %>"><i class="fa fa-folder"></i> Public stuff</a> + <pre> <%= inspect(SApp.Identity.get_info(@pid), pretty: true, width: 40) %> </pre> 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 @@ <a href="<%= directory_path(@conn, (if public do :view_pub else :view_priv end), owner|>Base.encode16(), name) %>"><%= name %></a> <%= if public do %><i class="fa fa-globe"></i><% else %><i class="fa fa-lock"></i><% end %> + <% %SApp.File.Manifest{infohash: infohash} -> %> + <% file_info = SApp.File.get_info(Shard.Manager.find_or_start(@manifest)) %> + <i class="fa fa-file"></i> + <%= if file_info[:size] != nil do Size.humanize!(file_info[:size]) end %> + <%= if file_info[:mime_type] != nil do %><code><%= file_info[:mime_type] %></code><% 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 %> + <a href="<%= directory_path(@conn, :raw_file, infohash|>Base.encode16) %>">[raw]</a> + <% end %> <% x -> %> <%= inspect x %> <% end %> |