Implemented local node browsing.

This commit is contained in:
Mark Qvist 2021-09-16 21:56:59 +02:00
parent a6376c716d
commit 150076be6b
2 changed files with 78 additions and 9 deletions

View File

@ -51,6 +51,7 @@ class Browser:
self.last_keypress = None
self.link = None
self.loopback = None
self.status = Browser.DISCONECTED
self.response_progress = 0
self.response_size = None
@ -176,7 +177,11 @@ class Browser:
else:
self.display_widget.set_attr_map({None: "body_text"})
self.browser_header = self.make_control_widget()
self.linebox.set_title(self.app.directory.simplest_display_str(self.destination_hash))
remote_display_string = self.app.directory.simplest_display_str(self.destination_hash)
if remote_display_string == RNS.prettyhexrep(self.loopback):
remote_display_string = self.app.node.name
self.linebox.set_title(remote_display_string)
if self.status == Browser.DONE:
self.browser_footer = self.make_status_widget()
@ -272,11 +277,14 @@ class Browser:
if destination_hash != None and path != None:
if path.startswith("/file/"):
if destination_hash == self.destination_hash:
self.download_file(destination_hash, path)
if destination_hash != self.loopback:
if destination_hash == self.destination_hash:
self.download_file(destination_hash, path)
else:
RNS.log("Cannot request file download from a node that is not currently connected.", RNS.LOG_ERROR)
RNS.log("The requested URL was: "+str(url), RNS.LOG_ERROR)
else:
RNS.log("Cannot request file download from a node that is not currently connected.", RNS.LOG_ERROR)
RNS.log("The requested URL was: "+str(url), RNS.LOG_ERROR)
self.download_local_file(path)
else:
self.set_destination_hash(destination_hash)
self.set_path(path)
@ -297,6 +305,35 @@ class Browser:
def set_timeout(self, timeout):
self.timeout = timeout
def download_local_file(self, path):
try:
file_path = self.app.filespath+path.replace("/file", "", 1)
if os.path.isfile(file_path):
file_name = os.path.basename(file_path)
file_destination = self.app.downloads_path+"/"+file_name
counter = 0
while os.path.isfile(file_destination):
counter += 1
file_destination = self.app.downloads_path+"/"+file_name+"."+str(counter)
fs = open(file_path, "rb")
fd = open(file_destination, "wb")
fd.write(fs.read())
fd.close()
fs.close()
self.saved_file_name = file_destination.replace(self.app.downloads_path+"/", "", 1)
self.update_display()
else:
RNS.log("The requested local download file does not exist: "+str(file_path), RNS.LOG_ERROR)
self.status = Browser.DONE
self.response_progress = 0
except Exception as e:
RNS.log("An error occurred while handling file response. The contained exception was: "+str(e), RNS.LOG_ERROR)
def download_file(self, destination_hash, path):
if self.link != None and self.link.destination.hash == self.destination_hash:
@ -326,9 +363,28 @@ class Browser:
def load_page(self):
load_thread = threading.Thread(target=self.__load)
load_thread.setDaemon(True)
load_thread.start()
if self.destination_hash != self.loopback:
load_thread = threading.Thread(target=self.__load)
load_thread.setDaemon(True)
load_thread.start()
else:
RNS.log("Browser handling local page: "+str(self.path), RNS.LOG_VERBOSE)
page_path = self.app.pagespath+self.path.replace("/page", "", 1)
RNS.log(page_path)
page_data = b"The requested local page did not exist in the file system"
if os.path.isfile(page_path):
file = open(page_path, "rb")
page_data = file.read()
file.close()
self.status = Browser.DONE
self.page_data = page_data
self.markup = self.page_data.decode("utf-8")
self.attr_maps = markup_to_attrmaps(self.markup, url_delegate=self)
self.response_progress = 0
self.update_display()
def __load(self):

View File

@ -735,6 +735,9 @@ class NodeInfo(urwid.WidgetWrap):
options = self.parent.left_pile.options(height_type="pack", height_amount=None)
self.parent.left_pile.contents[2] = (LocalPeer(self.app, self.parent), options)
def connect_query(sender):
self.parent.browser.retrieve_url(RNS.hexrep(self.app.node.destination.hash, delimit=False))
if NodeInfo.announce_timer == None:
self.t_last_announce = NodeAnnounceTime(self.app)
NodeInfo.announce_timer = self.t_last_announce
@ -750,6 +753,7 @@ class NodeInfo(urwid.WidgetWrap):
self.t_active_links.update_stat()
announce_button = urwid.Button("Announce Now", on_press=announce_query)
connect_button = urwid.Button("Browse", on_press=connect_query)
widget_style = ""
if self.app.enable_node:
@ -760,7 +764,13 @@ class NodeInfo(urwid.WidgetWrap):
self.t_last_announce,
self.t_active_links,
urwid.Divider(g["divider1"]),
urwid.Columns([("weight", 0.45, urwid.Button("Back", on_press=show_peer_info)), ("weight", 0.1, urwid.Text("")), ("weight", 0.45, announce_button)])
urwid.Columns([
("weight", 0.3, urwid.Button("Back", on_press=show_peer_info)),
("weight", 0.1, urwid.Text("")),
("weight", 0.3, connect_button),
("weight", 0.1, urwid.Text("")),
("weight", 0.3, announce_button)
])
])
else:
pile = urwid.Pile([
@ -858,6 +868,9 @@ class NetworkDisplay():
self.browser = Browser(self.app, "nomadnetwork", "node", auth_identity = self.app.identity, delegate = self)
if self.app.node != None:
self.browser.loopback = self.app.node.destination.hash
self.known_nodes_display = KnownNodes(self.app)
self.network_stats_display = NetworkStats(self.app, self)
self.announce_stream_display = AnnounceStream(self.app, self)