From 956f082b93d4ed095844337cd2e6b2a2158e0338 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 18 Sep 2012 15:21:45 +0530 Subject: [PATCH] MTP driver: Handle long filenames when adding files from device to calibre on windows --- src/calibre/devices/mtp/driver.py | 7 ++++++- src/calibre/devices/mtp/unix/driver.py | 4 +++- src/calibre/devices/mtp/windows/driver.py | 4 +++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/calibre/devices/mtp/driver.py b/src/calibre/devices/mtp/driver.py index c5b874a0fa..55d5c8ccaa 100644 --- a/src/calibre/devices/mtp/driver.py +++ b/src/calibre/devices/mtp/driver.py @@ -17,6 +17,7 @@ from calibre.devices.mtp.base import debug from calibre.ptempfile import SpooledTemporaryFile, PersistentTemporaryDirectory from calibre.utils.config import from_json, to_json, JSONConfig from calibre.utils.date import now, isoformat, utcnow +from calibre.utils.filenames import shorten_components_to BASE = importlib.import_module('calibre.devices.mtp.%s.driver'%( 'windows' if iswindows else 'unix')).MTP_DEVICE @@ -264,7 +265,11 @@ class MTP_DEVICE(BASE): continue base = os.path.join(tdir, '%s'%f.object_id) os.mkdir(base) - with open(os.path.join(base, f.name), 'wb') as out: + name = f.name + if iswindows: + plen = len(base) + name = ''.join(shorten_components_to(245-plen, [name])) + with open(os.path.join(base, name), 'wb') as out: try: self.get_mtp_file(f, out) except Exception as e: diff --git a/src/calibre/devices/mtp/unix/driver.py b/src/calibre/devices/mtp/unix/driver.py index 4b9ed9e928..5472834453 100644 --- a/src/calibre/devices/mtp/unix/driver.py +++ b/src/calibre/devices/mtp/unix/driver.py @@ -297,14 +297,16 @@ class MTP_DEVICE(MTPDeviceBase): def get_mtp_file(self, f, stream=None, callback=None): if f.is_folder: raise ValueError('%s if a folder'%(f.full_path,)) + set_name = stream is None if stream is None: stream = SpooledTemporaryFile(5*1024*1024, '_wpd_receive_file.dat') - stream.name = f.name ok, errs = self.dev.get_file(f.object_id, stream, callback) if not ok: raise DeviceError('Failed to get file: %s with errors: %s'%( f.full_path, self.format_errorstack(errs))) stream.seek(0) + if set_name: + stream.name = f.name return stream @synchronous diff --git a/src/calibre/devices/mtp/windows/driver.py b/src/calibre/devices/mtp/windows/driver.py index 3da81d26e2..b6adb2a036 100644 --- a/src/calibre/devices/mtp/windows/driver.py +++ b/src/calibre/devices/mtp/windows/driver.py @@ -321,9 +321,9 @@ class MTP_DEVICE(MTPDeviceBase): def get_mtp_file(self, f, stream=None, callback=None): if f.is_folder: raise ValueError('%s if a folder'%(f.full_path,)) + set_name = stream is None if stream is None: stream = SpooledTemporaryFile(5*1024*1024, '_wpd_receive_file.dat') - stream.name = f.name try: try: self.dev.get_file(f.object_id, stream, callback) @@ -334,6 +334,8 @@ class MTP_DEVICE(MTPDeviceBase): raise DeviceError('Failed to fetch the file %s with error: %s'% f.full_path, as_unicode(e)) stream.seek(0) + if set_name: + stream.name = f.name return stream @same_thread