aboutsummaryrefslogtreecommitdiff
path: root/shardweb/lib/controllers/directory_controller.ex
diff options
context:
space:
mode:
Diffstat (limited to 'shardweb/lib/controllers/directory_controller.ex')
-rw-r--r--shardweb/lib/controllers/directory_controller.ex31
1 files changed, 31 insertions, 0 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