mirror of
git://github.com/kovidgoyal/calibre.git
synced 2026-05-09 07:43:08 +02:00
MTP driver: Handle long filenames when adding files from device to calibre on windows
This commit is contained in:
parent
e3765c9a0a
commit
956f082b93
3 changed files with 12 additions and 3 deletions
|
|
@ -17,6 +17,7 @@
|
|||
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 @@ def prepare_addable_books(self, paths):
|
|||
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:
|
||||
|
|
|
|||
|
|
@ -297,14 +297,16 @@ def put_file(self, parent, name, stream, size, callback=None, replace=True):
|
|||
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
|
||||
|
|
|
|||
|
|
@ -321,9 +321,9 @@ def free_space(self, end_session=True):
|
|||
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 @@ def get_mtp_file(self, f, stream=None, callback=None):
|
|||
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
|
||||
|
|
|
|||
Loading…
Reference in a new issue