aboutsummaryrefslogtreecommitdiff
path: root/shardweb
diff options
context:
space:
mode:
Diffstat (limited to 'shardweb')
-rw-r--r--shardweb/lib/controllers/directory_controller.ex31
-rw-r--r--shardweb/lib/router.ex2
-rw-r--r--shardweb/lib/templates/directory/view.html.eex30
-rw-r--r--shardweb/lib/templates/identity/view.html.eex2
-rw-r--r--shardweb/lib/templates/page/shard_entry.html.eex11
-rw-r--r--shardweb/mix.exs2
-rw-r--r--shardweb/mix.lock1
7 files changed, 78 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 %>
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"},
}