If a text message is received, save it in the ReceiveModeRequest object, and add includes_text=True to REQUEST_STARTED

This commit is contained in:
Micah Lee 2021-04-30 14:12:41 -07:00
parent b51c0ee046
commit 47e02d781a
No known key found for this signature in database
GPG Key ID: 403C2657CD994F73
2 changed files with 86 additions and 56 deletions

View File

@ -26,14 +26,25 @@ from datetime import datetime
from flask import Request, request, render_template, make_response, flash, redirect from flask import Request, request, render_template, make_response, flash, redirect
from werkzeug.utils import secure_filename from werkzeug.utils import secure_filename
""" # Receive mode uses a special flask requests object, ReceiveModeRequest, in
Receive mode uses a special flask requests object, ReceiveModeRequest, in # order to keep track of upload progress. Here's what happens when someone
order to keep track of upload progress. Here's what happens when someone # uploads files:
uploads files: # - new ReceiveModeRequest object is created
# - ReceiveModeRequest.__init__
- new ReceiveModeRequest object is created # - creates a directory based on the timestamp
- creates a directory based on the timestamp # - creates empty self.progress = dict, which will map uploaded files to their upload progress
""" # - ReceiveModeRequest._get_file_stream
# - called for each file that gets upload
# - the first time, send REQUEST_STARTED to GUI, and append to self.web.receive_mode.uploads_in_progress
# - updates self.progress[self.filename] for the current file
# - uses custom ReceiveModeFile to save file to disk
# - ReceiveModeRequest.file_write_func called on each write
# - Display progress in CLI, and send REQUEST_PROGRESS to GUI
# - ReceiveModeRequest.file_close_func called when each file closes
# - self.progress[filename]["complete"] = True
# - ReceiveModeRequest.close
# - send either REQUEST_UPLOAD_CANCELED or REQUEST_UPLOAD_FINISHED to GUI
# - remove from self.web.receive_mode.uploads_in_progress
class ReceiveModeWeb: class ReceiveModeWeb:
@ -88,39 +99,7 @@ class ReceiveModeWeb:
Handle the upload files POST request, though at this point, the files have Handle the upload files POST request, though at this point, the files have
already been uploaded and saved to their correct locations. already been uploaded and saved to their correct locations.
""" """
text_received = False text_received = request.includes_text
if not self.web.settings.get("receive", "disable_text"):
text_message = request.form.get("text")
if text_message:
if text_message.strip() != "":
text_received = True
filename = "message.txt"
local_path = os.path.join(request.receive_mode_dir, filename)
with open(local_path, "w") as f:
f.write(text_message)
# Tell the GUI a message has been uploaded
self.web.add_request(
self.web.REQUEST_STARTED,
local_path,
{
"id": request.history_id,
"content_length": len(text_message),
},
)
self.web.add_request(
self.web.REQUEST_UPLOAD_FINISHED,
local_path,
{"id": request.history_id},
)
self.common.log(
"ReceiveModeWeb",
"define_routes",
f"/upload, sent text message, saving to {local_path}",
)
print(f"\nReceived: {local_path}")
files_received = 0 files_received = 0
if not self.web.settings.get("receive", "disable_files"): if not self.web.settings.get("receive", "disable_files"):
@ -150,7 +129,7 @@ class ReceiveModeWeb:
"define_routes", "define_routes",
f"/upload, uploaded {f.filename}, saving to {local_path}", f"/upload, uploaded {f.filename}, saving to {local_path}",
) )
print(f"\nReceived: {local_path}") print(f"Received: {local_path}")
files_received = len(filenames) files_received = len(filenames)
@ -380,8 +359,6 @@ class ReceiveModeRequest(Request):
self.web = environ["web"] self.web = environ["web"]
self.stop_q = environ["stop_q"] self.stop_q = environ["stop_q"]
self.web.common.log("ReceiveModeRequest", "__init__")
# Prevent running the close() method more than once # Prevent running the close() method more than once
self.closed = False self.closed = False
@ -392,6 +369,8 @@ class ReceiveModeRequest(Request):
self.upload_request = True self.upload_request = True
if self.upload_request: if self.upload_request:
self.web.common.log("ReceiveModeRequest", "__init__")
# No errors yet # No errors yet
self.upload_error = False self.upload_error = False
@ -475,6 +454,49 @@ class ReceiveModeRequest(Request):
self.previous_file = None self.previous_file = None
# Is there a text message?
self.includes_text = False
if not self.web.settings.get("receive", "disable_text"):
text_message = self.form.get("text")
if text_message:
if text_message.strip() != "":
self.includes_text = True
filename = "message.txt"
local_path = os.path.join(self.receive_mode_dir, filename)
with open(local_path, "w") as f:
f.write(text_message)
self.web.common.log(
"ReceiveModeRequest",
"__init__",
f"saved message to {local_path}",
)
print(f"Received: {local_path}")
self.tell_gui_request_started()
def tell_gui_request_started(self):
# Tell the GUI about the request
if not self.told_gui_about_request:
self.web.common.log(
"ReceiveModeRequest",
"_get_file_stream",
"sending REQUEST_STARTED to GUI",
)
self.web.add_request(
self.web.REQUEST_STARTED,
self.path,
{
"id": self.history_id,
"content_length": self.content_length,
"includes_text": self.includes_text,
},
)
self.web.receive_mode.uploads_in_progress.append(self.history_id)
self.told_gui_about_request = True
def _get_file_stream( def _get_file_stream(
self, total_content_length, content_type, filename=None, content_length=None self, total_content_length, content_type, filename=None, content_length=None
): ):
@ -483,16 +505,7 @@ class ReceiveModeRequest(Request):
writable stream. writable stream.
""" """
if self.upload_request: if self.upload_request:
if not self.told_gui_about_request: self.tell_gui_request_started()
# Tell the GUI about the request
self.web.add_request(
self.web.REQUEST_STARTED,
self.path,
{"id": self.history_id, "content_length": self.content_length},
)
self.web.receive_mode.uploads_in_progress.append(self.history_id)
self.told_gui_about_request = True
self.filename = secure_filename(filename) self.filename = secure_filename(filename)
@ -519,7 +532,8 @@ class ReceiveModeRequest(Request):
return return
self.closed = True self.closed = True
self.web.common.log("ReceiveModeRequest", "close") if self.upload_request:
self.web.common.log("ReceiveModeRequest", "close")
try: try:
if self.told_gui_about_request: if self.told_gui_about_request:
@ -559,7 +573,11 @@ class ReceiveModeRequest(Request):
size_str = self.web.common.human_readable_filesize( size_str = self.web.common.human_readable_filesize(
self.progress[filename]["uploaded_bytes"] self.progress[filename]["uploaded_bytes"]
) )
print(f"\r=> {size_str} {filename} ", end="")
if self.web.common.verbose:
print(f"=> {size_str} {filename}")
else:
print(f"\r=> {size_str} {filename} ", end="")
# Update the GUI on the upload progress # Update the GUI on the upload progress
if self.told_gui_about_request: if self.told_gui_about_request:

View File

@ -277,6 +277,12 @@ class ReceiveHistoryItem(HistoryItem):
self.started = datetime.now() self.started = datetime.now()
self.status = HistoryItem.STATUS_STARTED self.status = HistoryItem.STATUS_STARTED
self.common.log(
"ReceiveHistoryItem",
"__init__",
f"id={self.id} content_length={self.content_length}",
)
# Label # Label
self.label = QtWidgets.QLabel( self.label = QtWidgets.QLabel(
strings._("gui_all_modes_transfer_started").format( strings._("gui_all_modes_transfer_started").format(
@ -318,6 +324,12 @@ class ReceiveHistoryItem(HistoryItem):
Using the progress from Web, update the progress bar and file size labels Using the progress from Web, update the progress bar and file size labels
for each file for each file
""" """
# self.common.log(
# "ReceiveHistoryItem",
# "update",
# f"id={self.id} data[action]={data['action']} files={list(self.files)}",
# )
if data["action"] == "progress": if data["action"] == "progress":
total_uploaded_bytes = 0 total_uploaded_bytes = 0
for filename in data["progress"]: for filename in data["progress"]: