MTP driver: Handle long filenames when adding files from device to calibre on windows

This commit is contained in:
Kovid Goyal 2012-09-18 15:21:45 +05:30
parent e3765c9a0a
commit 956f082b93
3 changed files with 12 additions and 3 deletions

View File

@ -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:

View File

@ -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

View File

@ -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