Fix file renaming and moving with unicode characters on libtorrent 0.16

Fix torrent creation with unicode characters
This commit is contained in:
Chase Sterling 2012-11-26 17:18:41 -05:00
parent bd979da949
commit 4579886bb5
3 changed files with 37 additions and 20 deletions

View File

@ -885,31 +885,32 @@ class Torrent(object):
def move_storage(self, dest): def move_storage(self, dest):
"""Move a torrent's storage location""" """Move a torrent's storage location"""
try:
dest = unicode(dest, "utf-8")
except TypeError:
# String is already unicode
pass
if deluge.common.windows_check(): if not os.path.exists(dest):
# Attempt to convert utf8 path to unicode
# Note: Inconsistent encoding for 'dest', needs future investigation
try:
dest_u = unicode(dest, "utf-8")
except TypeError:
# String is already unicode
dest_u = dest
else:
dest_u = dest
if not os.path.exists(dest_u):
try: try:
# Try to make the destination path if it doesn't exist # Try to make the destination path if it doesn't exist
os.makedirs(dest_u) os.makedirs(dest)
except IOError, e: except IOError, e:
log.exception(e) log.exception(e)
log.error("Could not move storage for torrent %s since %s does " log.error("Could not move storage for torrent %s since %s does "
"not exist and could not create the directory.", "not exist and could not create the directory.",
self.torrent_id, dest_u) self.torrent_id, dest)
return False return False
dest_bytes = dest.encode('utf-8')
try: try:
self.handle.move_storage(dest_u) # libtorrent needs unicode object if wstrings are enabled, utf8 bytestring otherwise
except: try:
self.handle.move_storage(dest)
except TypeError:
self.handle.move_storage(dest_bytes)
except Exception, e:
log.error("Error calling libtorrent move_storage: %s" % e)
return False return False
return True return True
@ -985,8 +986,17 @@ class Torrent(object):
"""Renames files in the torrent. 'filenames' should be a list of """Renames files in the torrent. 'filenames' should be a list of
(index, filename) pairs.""" (index, filename) pairs."""
for index, filename in filenames: for index, filename in filenames:
# Make sure filename is a unicode object
try:
filename = unicode(filename, "utf-8")
except TypeError:
pass
filename = sanitize_filepath(filename) filename = sanitize_filepath(filename)
self.handle.rename_file(index, filename.encode("utf-8")) # libtorrent needs unicode object if wstrings are enabled, utf8 bytestring otherwise
try:
self.handle.rename_file(index, filename)
except TypeError:
self.handle.rename_file(index, filename.encode("utf-8"))
def rename_folder(self, folder, new_folder): def rename_folder(self, folder, new_folder):
"""Renames a folder within a torrent. This basically does a file rename """Renames a folder within a torrent. This basically does a file rename

View File

@ -452,9 +452,16 @@ class TorrentManager(component.Component):
# before adding to the session. # before adding to the session.
if options["mapped_files"]: if options["mapped_files"]:
for index, fname in options["mapped_files"].items(): for index, fname in options["mapped_files"].items():
try:
fname = unicode(fname, "utf-8")
except TypeError:
pass
fname = deluge.core.torrent.sanitize_filepath(fname) fname = deluge.core.torrent.sanitize_filepath(fname)
log.debug("renaming file index %s to %s", index, fname) log.debug("renaming file index %s to %s", index, fname)
torrent_info.rename_file(index, utf8_encoded(fname)) try:
torrent_info.rename_file(index, fname)
except TypeError:
torrent_info.rename_file(index, fname.encode("utf-8"))
add_torrent_params["ti"] = torrent_info add_torrent_params["ti"] = torrent_info

View File

@ -174,7 +174,7 @@ class CreateTorrentDialog:
chooser.destroy() chooser.destroy()
return return
path = result.decode('utf-8').encode(sys.getfilesystemencoding()) path = result.decode('utf-8')
self.files_treestore.clear() self.files_treestore.clear()
self.files_treestore.append(None, [result, gtk.STOCK_FILE, deluge.common.get_path_size(path)]) self.files_treestore.append(None, [result, gtk.STOCK_FILE, deluge.common.get_path_size(path)])
@ -202,7 +202,7 @@ class CreateTorrentDialog:
chooser.destroy() chooser.destroy()
return return
path = result.decode('utf-8').encode(sys.getfilesystemencoding()) path = result.decode('utf-8')
self.files_treestore.clear() self.files_treestore.clear()
self.files_treestore.append(None, [result, gtk.STOCK_OPEN, deluge.common.get_path_size(path)]) self.files_treestore.append(None, [result, gtk.STOCK_OPEN, deluge.common.get_path_size(path)])