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(
"/page/index.mu",
response_generator = self.serve_default_index,
allow = RNS.Destination.ALLOW_ALL
)
allow = RNS.Destination.ALLOW_ALL)
for page in self.servedpages:
request_path = "/page"+page.replace(self.app.pagespath, "")
self.destination.register_request_handler(
request_path,
response_generator = self.serve_page,
allow = RNS.Destination.ALLOW_ALL
)
allow = RNS.Destination.ALLOW_ALL)
def register_files(self):
# TODO: Deregister previously registered files
@ -83,8 +81,8 @@ class Node:
self.destination.register_request_handler(
request_path,
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):
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)
try:
RNS.log("Serving file: "+file_path, RNS.LOG_VERBOSE)
fh = open(file_path, "rb")
file_data = fh.read()
fh.close()
return [file_name, file_data]
return [open(file_path, "rb"), {"name": file_name.encode("utf-8")}]
except Exception as e:
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 LXMF
import io
import os
import time
import urwid
import shutil
import nomadnet
import subprocess
import threading
@ -1036,13 +1038,26 @@ class Browser:
def file_received(self, request_receipt):
try:
if type(request_receipt.response) == io.BufferedReader:
if request_receipt.metadata != None:
file_name = os.path.basename(request_receipt.metadata["name"].decode("utf-8"))
file_handle = request_receipt.response
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)
shutil.move(file_handle.name, file_destination)
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