mirror of
https://github.com/markqvist/NomadNet.git
synced 2025-05-17 13:20:25 -04:00
Use RNS file responses for file downloads instead of reading entire file into memory
This commit is contained in:
parent
4eed5bab48
commit
f107a9ea30
2 changed files with 34 additions and 24 deletions
|
@ -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)
|
||||
|
|
|
@ -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,23 +1038,36 @@ class Browser:
|
|||
|
||||
def file_received(self, request_receipt):
|
||||
try:
|
||||
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
|
||||
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)
|
||||
|
||||
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)
|
||||
|
||||
fh = open(file_destination, "wb")
|
||||
fh.write(file_data)
|
||||
fh.close()
|
||||
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
|
||||
|
||||
self.saved_file_name = file_destination.replace(self.app.downloads_path+"/", "", 1)
|
||||
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.response_progress = 0
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue