Use RNS file responses for file downloads instead of reading entire file into memory

This commit is contained in:
Mark Qvist 2025-05-11 16:42:50 +02:00
parent 4eed5bab48
commit f107a9ea30
2 changed files with 34 additions and 24 deletions

View file

@ -61,16 +61,14 @@ class Node:
self.destination.register_request_handler( self.destination.register_request_handler(
"/page/index.mu", "/page/index.mu",
response_generator = self.serve_default_index, response_generator = self.serve_default_index,
allow = RNS.Destination.ALLOW_ALL allow = RNS.Destination.ALLOW_ALL)
)
for page in self.servedpages: for page in self.servedpages:
request_path = "/page"+page.replace(self.app.pagespath, "") request_path = "/page"+page.replace(self.app.pagespath, "")
self.destination.register_request_handler( self.destination.register_request_handler(
request_path, request_path,
response_generator = self.serve_page, response_generator = self.serve_page,
allow = RNS.Destination.ALLOW_ALL allow = RNS.Destination.ALLOW_ALL)
)
def register_files(self): def register_files(self):
# TODO: Deregister previously registered files # TODO: Deregister previously registered files
@ -83,8 +81,8 @@ class Node:
self.destination.register_request_handler( self.destination.register_request_handler(
request_path, request_path,
response_generator = self.serve_file, response_generator = self.serve_file,
allow = RNS.Destination.ALLOW_ALL allow = RNS.Destination.ALLOW_ALL,
) auto_compress = 32_000_000)
def scan_pages(self, base_path): def scan_pages(self, base_path):
files = [file for file in os.listdir(base_path) if os.path.isfile(os.path.join(base_path, file)) and file[:1] != "."] files = [file for file in os.listdir(base_path) if os.path.isfile(os.path.join(base_path, file)) and file[:1] != "."]
@ -205,10 +203,7 @@ class Node:
file_name = path.replace("/file/", "", 1) file_name = path.replace("/file/", "", 1)
try: try:
RNS.log("Serving file: "+file_path, RNS.LOG_VERBOSE) RNS.log("Serving file: "+file_path, RNS.LOG_VERBOSE)
fh = open(file_path, "rb") return [open(file_path, "rb"), {"name": file_name.encode("utf-8")}]
file_data = fh.read()
fh.close()
return [file_name, file_data]
except Exception as e: except Exception as e:
RNS.log("Error occurred while handling request "+RNS.prettyhexrep(request_id)+" for: "+str(path), RNS.LOG_ERROR) RNS.log("Error occurred while handling request "+RNS.prettyhexrep(request_id)+" for: "+str(path), RNS.LOG_ERROR)

View file

@ -1,8 +1,10 @@
import RNS import RNS
import LXMF import LXMF
import io
import os import os
import time import time
import urwid import urwid
import shutil
import nomadnet import nomadnet
import subprocess import subprocess
import threading import threading
@ -1036,23 +1038,36 @@ class Browser:
def file_received(self, request_receipt): def file_received(self, request_receipt):
try: try:
file_name = request_receipt.response[0] if type(request_receipt.response) == io.BufferedReader:
file_data = request_receipt.response[1] if request_receipt.metadata != None:
file_destination_name = file_name.split("/") file_name = os.path.basename(request_receipt.metadata["name"].decode("utf-8"))
file_destination_name = file_destination_name[len(file_destination_name)-1] file_handle = request_receipt.response
file_destination = self.app.downloads_path+"/"+file_destination_name file_destination = self.app.downloads_path+"/"+file_name
counter = 0
counter = 0 while os.path.isfile(file_destination):
while os.path.isfile(file_destination): counter += 1
counter += 1 file_destination = self.app.downloads_path+"/"+file_name+"."+str(counter)
file_destination = self.app.downloads_path+"/"+file_name+"."+str(counter)
fh = open(file_destination, "wb") shutil.move(file_handle.name, file_destination)
fh.write(file_data)
fh.close()
self.saved_file_name = file_destination.replace(self.app.downloads_path+"/", "", 1) else:
file_name = request_receipt.response[0]
file_data = request_receipt.response[1]
file_destination_name = file_name.split("/")
file_destination_name = file_destination_name[len(file_destination_name)-1]
file_destination = self.app.downloads_path+"/"+file_destination_name
counter = 0
while os.path.isfile(file_destination):
counter += 1
file_destination = self.app.downloads_path+"/"+file_name+"."+str(counter)
fh = open(file_destination, "wb")
fh.write(file_data)
fh.close()
self.saved_file_name = file_destination.replace(self.app.downloads_path+"/", "", 1)
self.status = Browser.DONE self.status = Browser.DONE
self.response_progress = 0 self.response_progress = 0