Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* show an alert dialog if the file is too big
* run the file selection dialog in non-modal mode
* do the file check as early as we can (don't bother loading gigabytes of data into memory)

git-svn-id: https://xpra.org/svn/Xpra/trunk@13903 3bb7dfac-3a0b-4e04-842a-767bc560f471
  • Loading branch information
totaam committed Sep 29, 2016
1 parent b35897d commit 5383506
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 12 deletions.
35 changes: 33 additions & 2 deletions src/xpra/client/gtk_base/gtk_client_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@
gtk_main_quit_on_fatal_exceptions_enable)
from xpra.util import updict, pver, iround, flatten_dict, envbool, DEFAULT_METADATA_SUPPORTED
from xpra.os_util import bytestostr
from xpra.simple_stats import std_unit
from xpra.gtk_common.cursor_names import cursor_types
from xpra.gtk_common.gtk_util import get_gtk_version_info, scaled_image, get_default_cursor, \
new_Cursor_for_display, new_Cursor_from_pixbuf, icon_theme_get_default, \
pixbuf_new_from_file, display_get_default, screen_get_default, get_pixbuf_from_data, \
get_default_root_window, get_root_size, get_xwindow, \
INTERP_BILINEAR, WINDOW_TOPLEVEL
INTERP_BILINEAR, WINDOW_TOPLEVEL, DIALOG_DESTROY_WITH_PARENT, MESSAGE_INFO, BUTTONS_CLOSE
from xpra.client.ui_client_base import UIXpraClient
from xpra.client.gobject_client_base import GObjectXpraClient
from xpra.client.gtk_base.gtk_keyboard_helper import GTKKeyboardHelper
Expand Down Expand Up @@ -151,6 +152,24 @@ def run_command_cb(command, sharing=True):
self.start_new_command.show()
return self.start_new_command


def file_size_warning(self, action, location, basefilename, filesize, limit):
parent = None
msgs = (
"Warning: cannot %s the file '%s'" % (action, basefilename),
"this file is too large: %sB" % std_unit(filesize, unit=1024),
"the %s file size limit is %iMB" % (location, limit),
)
md = gtk.MessageDialog(parent, DIALOG_DESTROY_WITH_PARENT, MESSAGE_INFO,
BUTTONS_CLOSE, "\n".join(msgs))
try:
image = gtk.image_new_from_stock(gtk.STOCK_DIALOG_WARNING, 64)
md.set_image(image)
except Exception as e:
log.warn("failed to set dialog image: %s", e)
md.connect("response", lambda w,resp: md.destroy())
md.show()

def show_file_upload(self, *args):
filelog("show_file_upload%s can open=%s", args, self.remote_open_files)
buttons = [gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL]
Expand All @@ -159,12 +178,24 @@ def show_file_upload(self, *args):
buttons += [gtk.STOCK_OK, gtk.RESPONSE_OK]
dialog = gtk.FileChooserDialog("File to upload", parent=None, action=gtk.FILE_CHOOSER_ACTION_OPEN, buttons=tuple(buttons))
dialog.set_default_response(gtk.RESPONSE_OK)
v = dialog.run()
dialog.connect("response", self.file_upload_dialog_response)
dialog.show()

def file_upload_dialog_response(self, dialog, v):
if v not in (gtk.RESPONSE_OK, gtk.RESPONSE_ACCEPT):
filelog("dialog response code %s", v)
dialog.destroy()
return
filename = dialog.get_filename()
filelog("file_upload_dialog_response: filename=%s", filename)
try:
filesize = os.stat(filename).st_size
except:
pass
else:
if not self.check_file_size("upload", filename, filesize):
dialog.destroy()
return
gfile = dialog.get_file()
dialog.destroy()
filelog("load_contents: filename=%s, response=%s", filename, v)
Expand Down
27 changes: 17 additions & 10 deletions src/xpra/net/file_transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,21 @@ def open_done(*args):
cr.add_process(proc, "Open File %s" % filename, command, True, True, open_done)


def file_size_warning(self, action, location, basefilename, filesize, limit):
filelog.warn("Warning: cannot %s the file '%s'", action, basefilename)
filelog.warn(" this file is too large: %sB", std_unit(filesize, unit=1024))
filelog.warn(" the %s file size limit is %iMB", location, limit)

def check_file_size(self, action, filename, filesize):
basefilename = os.path.basename(filename)
if filesize>self.file_size_limit*1024*1024:
self.file_size_warning(action, "local", basefilename, filesize, self.file_size_limit)
return False
if filesize>self.remote_file_size_limit*1024*1024:
self.file_size_warning(action, "remote", basefilename, filesize, self.remote_file_size_limit)
return False
return True

def send_file(self, filename, mimetype, data, filesize=0, printit=False, openit=False, options={}):
if printit:
if not self.printing:
Expand All @@ -384,16 +399,7 @@ def send_file(self, filename, mimetype, data, filesize=0, printit=False, openit=
data = data[:filesize] #gio may null terminate it
l("send_file%s", (filename, mimetype, type(data), "%i bytes" % filesize, printit, openit, options))
absfile = os.path.abspath(filename)
basefilename = os.path.basename(filename)
def sizewarn(location, limit):
filelog.warn("Warning: cannot %s the file '%s'", action, basefilename)
filelog.warn(" this file is too large: %sB", std_unit(filesize, unit=1024))
filelog.warn(" the %s file size limit is %iMB", location, limit)
if filesize>self.file_size_limit*1024*1024:
sizewarn("local", self.file_size_limit)
return False
if filesize>self.remote_file_size_limit*1024*1024:
sizewarn("remote", self.remote_file_size_limit)
if not self.check_file_size(action, filename, filesize):
return False
u = hashlib.sha1()
u.update(data)
Expand All @@ -415,6 +421,7 @@ def sizewarn(location, limit):
#send everything now:
cdata = self.compressed_wrapper("file-data", data)
assert len(cdata)<=filesize #compressed wrapper ensures this is true
basefilename = os.path.basename(filename)
self.send("send-file", basefilename, mimetype, printit, openit, filesize, cdata, options)
return True

Expand Down

0 comments on commit 5383506

Please sign in to comment.