4 from twisted.internet import address
5 from twisted.web import http
6 from nevow import rend, url, tags as T
7 from nevow.inevow import IRequest
8 from nevow.static import File as nevow_File # TODO: merge with static.File?
9 from nevow.util import resource_filename
10 from formless import webform
12 import allmydata # to display import path
13 from allmydata import get_package_versions_string
14 from allmydata import provisioning
15 from allmydata.util import idlib
16 from allmydata.interfaces import IFileNode
17 from allmydata.web import filenode, directory, unlinked, status
18 from allmydata.web.common import abbreviate_size, IClient, getxmlfile, \
19 WebError, get_arg, RenderMixin
23 class URIHandler(RenderMixin, rend.Page):
24 # I live at /uri . There are several operations defined on /uri itself,
25 # mostly involed with creation of unlinked files and directories.
27 def render_GET(self, ctx):
29 uri = get_arg(req, "uri", None)
31 raise WebError("GET /uri requires uri=")
32 there = url.URL.fromContext(ctx)
33 there = there.clear("uri")
34 # I thought about escaping the childcap that we attach to the URL
35 # here, but it seems that nevow does that for us.
36 there = there.child(uri)
39 def render_PUT(self, ctx):
41 # either "PUT /uri" to create an unlinked file, or
42 # "PUT /uri?t=mkdir" to create an unlinked directory
43 t = get_arg(req, "t", "").strip()
45 mutable = bool(get_arg(req, "mutable", "").strip())
47 return unlinked.PUTUnlinkedSSK(ctx)
49 return unlinked.PUTUnlinkedCHK(ctx)
51 return unlinked.PUTUnlinkedCreateDirectory(ctx)
52 errmsg = ("/uri accepts only PUT, PUT?t=mkdir, POST?t=upload, "
54 raise WebError(errmsg, http.BAD_REQUEST)
56 def render_POST(self, ctx):
57 # "POST /uri?t=upload&file=newfile" to upload an
58 # unlinked file or "POST /uri?t=mkdir" to create a
61 t = get_arg(req, "t", "").strip()
62 if t in ("", "upload"):
63 mutable = bool(get_arg(req, "mutable", "").strip())
65 return unlinked.POSTUnlinkedSSK(ctx)
67 return unlinked.POSTUnlinkedCHK(ctx)
69 return unlinked.POSTUnlinkedCreateDirectory(ctx)
70 errmsg = ("/uri accepts only PUT, PUT?t=mkdir, POST?t=upload, "
72 raise WebError(errmsg, http.BAD_REQUEST)
74 def childFactory(self, ctx, name):
75 # 'name' is expected to be a URI
78 node = client.create_node_from_uri(name)
79 return directory.make_handler_for(node)
80 except (TypeError, AssertionError):
81 raise WebError("'%s' is not a valid file- or directory- cap"
84 class FileHandler(rend.Page):
85 # I handle /file/$FILECAP[/IGNORED] , which provides a URL from which a
86 # file can be downloaded correctly by tools like "wget".
88 def childFactory(self, ctx, name):
90 if req.method not in ("GET", "HEAD"):
91 raise WebError("/file can only be used with GET or HEAD")
92 # 'name' must be a file URI
95 node = client.create_node_from_uri(name)
96 except (TypeError, AssertionError):
97 raise WebError("'%s' is not a valid file- or directory- cap"
99 if not IFileNode.providedBy(node):
100 raise WebError("'%s' is not a file-cap" % name)
101 return filenode.FileNodeDownloadHandler(node)
103 def renderHTTP(self, ctx):
104 raise WebError("/file must be followed by a file-cap and a name",
107 class Root(rend.Page):
110 docFactory = getxmlfile("welcome.xhtml")
112 child_uri = URIHandler()
113 child_file = FileHandler()
114 child_named = FileHandler()
116 child_webform_css = webform.defaultCSS
117 child_tahoe_css = nevow_File(resource_filename('allmydata.web', 'tahoe.css'))
119 child_provisioning = provisioning.ProvisioningTool()
120 child_status = status.Status()
121 child_helper_status = status.HelperStatus()
122 child_statistics = status.Statistics()
124 def data_version(self, ctx, data):
125 return get_package_versions_string()
126 def data_import_path(self, ctx, data):
127 return str(allmydata)
128 def data_my_nodeid(self, ctx, data):
129 return idlib.nodeid_b2a(IClient(ctx).nodeid)
131 def render_services(self, ctx, data):
133 client = IClient(ctx)
135 ss = client.getServiceNamed("storage")
136 allocated_s = abbreviate_size(ss.allocated_size())
137 allocated = "about %s allocated" % allocated_s
138 sizelimit = "no size limit"
139 if ss.sizelimit is not None:
140 sizelimit = "size limit is %s" % abbreviate_size(ss.sizelimit)
141 ul[T.li["Storage Server: %s, %s" % (allocated, sizelimit)]]
143 ul[T.li["Not running storage server"]]
146 h = client.getServiceNamed("helper")
147 stats = h.get_stats()
148 active_uploads = stats["chk_upload_helper.active_uploads"]
149 ul[T.li["Helper: %d active uploads" % (active_uploads,)]]
151 ul[T.li["Not running helper"]]
155 def data_introducer_furl(self, ctx, data):
156 return IClient(ctx).introducer_furl
157 def data_connected_to_introducer(self, ctx, data):
158 if IClient(ctx).connected_to_introducer():
162 def data_helper_furl(self, ctx, data):
164 uploader = IClient(ctx).getServiceNamed("uploader")
167 furl, connected = uploader.get_helper_info()
169 def data_connected_to_helper(self, ctx, data):
171 uploader = IClient(ctx).getServiceNamed("uploader")
173 return "no" # we don't even have an Uploader
174 furl, connected = uploader.get_helper_info()
179 def data_known_storage_servers(self, ctx, data):
180 ic = IClient(ctx).introducer_client
182 for c in ic.get_all_connectors().values()
183 if c.service_name == "storage"]
186 def data_connected_storage_servers(self, ctx, data):
187 ic = IClient(ctx).introducer_client
188 return len(ic.get_all_connections_for("storage"))
190 def data_services(self, ctx, data):
191 ic = IClient(ctx).introducer_client
192 c = [ (service_name, nodeid, rsc)
193 for (nodeid, service_name), rsc
194 in ic.get_all_connectors().items() ]
198 def render_service_row(self, ctx, data):
199 (service_name, nodeid, rsc) = data
200 ctx.fillSlots("peerid", "%s %s" % (idlib.nodeid_b2a(nodeid),
203 rhost = rsc.remote_host
204 if nodeid == IClient(ctx).nodeid:
205 rhost_s = "(loopback)"
206 elif isinstance(rhost, address.IPv4Address):
207 rhost_s = "%s:%d" % (rhost.host, rhost.port)
210 connected = "Yes: to " + rhost_s
211 since = rsc.last_connect_time
214 since = rsc.last_loss_time
216 TIME_FORMAT = "%H:%M:%S %d-%b-%Y"
217 ctx.fillSlots("connected", connected)
218 ctx.fillSlots("since", time.strftime(TIME_FORMAT, time.localtime(since)))
219 ctx.fillSlots("announced", time.strftime(TIME_FORMAT,
220 time.localtime(rsc.announcement_time)))
221 ctx.fillSlots("version", rsc.version)
222 ctx.fillSlots("service_name", rsc.service_name)
226 def render_download_form(self, ctx, data):
227 # this is a form where users can download files by URI
228 form = T.form(action="uri", method="get",
229 enctype="multipart/form-data")[
231 T.legend(class_="freeform-form-label")["Download a file"],
233 T.input(type="text", name="uri"), " ",
234 "Filename to download as: ",
235 T.input(type="text", name="filename"), " ",
236 T.input(type="submit", value="Download!"),
240 def render_view_form(self, ctx, data):
241 # this is a form where users can download files by URI, or jump to a
243 form = T.form(action="uri", method="get",
244 enctype="multipart/form-data")[
246 T.legend(class_="freeform-form-label")["View a file or directory"],
248 T.input(type="text", name="uri"), " ",
249 T.input(type="submit", value="View!"),
253 def render_upload_form(self, ctx, data):
254 # this is a form where users can upload unlinked files
255 form = T.form(action="uri", method="post",
256 enctype="multipart/form-data")[
258 T.legend(class_="freeform-form-label")["Upload a file"],
260 T.input(type="file", name="file", class_="freeform-input-file"),
261 T.input(type="hidden", name="t", value="upload"),
262 " Mutable?:", T.input(type="checkbox", name="mutable"),
263 T.input(type="submit", value="Upload!"),
267 def render_mkdir_form(self, ctx, data):
268 # this is a form where users can create new directories
269 form = T.form(action="uri", method="post",
270 enctype="multipart/form-data")[
272 T.legend(class_="freeform-form-label")["Create a directory"],
273 T.input(type="hidden", name="t", value="mkdir"),
274 T.input(type="hidden", name="redirect_to_result", value="true"),
275 T.input(type="submit", value="Create Directory!"),