1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
defmodule ShardWeb.DirectoryController do
use ShardWeb, :controller
def view_pub(conn, args) do
view(conn, true, args)
end
def view_priv(conn, args) do
view(conn, false, args)
end
def view(conn, public, %{"owner" => owner, "name" => name}=args) do
owner = Base.decode16! owner
manifest = %SApp.Directory.Manifest{public: public, owner: owner, name: name}
shard = SData.term_hash manifest
pid = Shard.Manager.find_or_start manifest
render conn, "view.html",
public: public,
shard: shard,
manifest: manifest,
pid: pid,
owner: owner,
name: name
end
def dir_add(conn, %{"dir_name" => dir_name, "dir_public" => dir_public, "add_name" => add_name, "add_uri" => add_uri, "add_stored" => add_stored}) do
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
item_manifest = ShardURI.to_manifest(add_uri)
SApp.Directory.add_item(pid, add_name, item_manifest, (add_stored == "yes"))
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_rm(conn, %{"dir_name" => dir_name, "dir_public" => dir_public, "item_name" => rm_name}) do
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
SApp.Directory.rm_item(pid, rm_name)
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_set_stored(conn, %{"dir_name" => dir_name, "dir_public" => dir_public, "item_name" => item_name, "item_stored" => item_stored}) do
dir_public = (dir_public == "true")
item_stored = (item_stored == "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
SApp.Directory.set_stored(pid, item_name, item_stored)
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(String.trim(info[:mime_type]))
|> send_file(200, info[:path])
else
conn
|> put_status(:not_found)
|> text("File not found or not yet completely downloaded. Come back later!\n#{inspect(info)}")
end
end
end
|