mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
MTP driver: Handle long filenames when adding files from device to calibre on windows
This commit is contained in:
parent
e3765c9a0a
commit
956f082b93
@ -17,6 +17,7 @@ from calibre.devices.mtp.base import debug
|
|||||||
from calibre.ptempfile import SpooledTemporaryFile, PersistentTemporaryDirectory
|
from calibre.ptempfile import SpooledTemporaryFile, PersistentTemporaryDirectory
|
||||||
from calibre.utils.config import from_json, to_json, JSONConfig
|
from calibre.utils.config import from_json, to_json, JSONConfig
|
||||||
from calibre.utils.date import now, isoformat, utcnow
|
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'%(
|
BASE = importlib.import_module('calibre.devices.mtp.%s.driver'%(
|
||||||
'windows' if iswindows else 'unix')).MTP_DEVICE
|
'windows' if iswindows else 'unix')).MTP_DEVICE
|
||||||
@ -264,7 +265,11 @@ class MTP_DEVICE(BASE):
|
|||||||
continue
|
continue
|
||||||
base = os.path.join(tdir, '%s'%f.object_id)
|
base = os.path.join(tdir, '%s'%f.object_id)
|
||||||
os.mkdir(base)
|
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:
|
try:
|
||||||
self.get_mtp_file(f, out)
|
self.get_mtp_file(f, out)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
@ -297,14 +297,16 @@ class MTP_DEVICE(MTPDeviceBase):
|
|||||||
def get_mtp_file(self, f, stream=None, callback=None):
|
def get_mtp_file(self, f, stream=None, callback=None):
|
||||||
if f.is_folder:
|
if f.is_folder:
|
||||||
raise ValueError('%s if a folder'%(f.full_path,))
|
raise ValueError('%s if a folder'%(f.full_path,))
|
||||||
|
set_name = stream is None
|
||||||
if stream is None:
|
if stream is None:
|
||||||
stream = SpooledTemporaryFile(5*1024*1024, '_wpd_receive_file.dat')
|
stream = SpooledTemporaryFile(5*1024*1024, '_wpd_receive_file.dat')
|
||||||
stream.name = f.name
|
|
||||||
ok, errs = self.dev.get_file(f.object_id, stream, callback)
|
ok, errs = self.dev.get_file(f.object_id, stream, callback)
|
||||||
if not ok:
|
if not ok:
|
||||||
raise DeviceError('Failed to get file: %s with errors: %s'%(
|
raise DeviceError('Failed to get file: %s with errors: %s'%(
|
||||||
f.full_path, self.format_errorstack(errs)))
|
f.full_path, self.format_errorstack(errs)))
|
||||||
stream.seek(0)
|
stream.seek(0)
|
||||||
|
if set_name:
|
||||||
|
stream.name = f.name
|
||||||
return stream
|
return stream
|
||||||
|
|
||||||
@synchronous
|
@synchronous
|
||||||
|
@ -321,9 +321,9 @@ class MTP_DEVICE(MTPDeviceBase):
|
|||||||
def get_mtp_file(self, f, stream=None, callback=None):
|
def get_mtp_file(self, f, stream=None, callback=None):
|
||||||
if f.is_folder:
|
if f.is_folder:
|
||||||
raise ValueError('%s if a folder'%(f.full_path,))
|
raise ValueError('%s if a folder'%(f.full_path,))
|
||||||
|
set_name = stream is None
|
||||||
if stream is None:
|
if stream is None:
|
||||||
stream = SpooledTemporaryFile(5*1024*1024, '_wpd_receive_file.dat')
|
stream = SpooledTemporaryFile(5*1024*1024, '_wpd_receive_file.dat')
|
||||||
stream.name = f.name
|
|
||||||
try:
|
try:
|
||||||
try:
|
try:
|
||||||
self.dev.get_file(f.object_id, stream, callback)
|
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'%
|
raise DeviceError('Failed to fetch the file %s with error: %s'%
|
||||||
f.full_path, as_unicode(e))
|
f.full_path, as_unicode(e))
|
||||||
stream.seek(0)
|
stream.seek(0)
|
||||||
|
if set_name:
|
||||||
|
stream.name = f.name
|
||||||
return stream
|
return stream
|
||||||
|
|
||||||
@same_thread
|
@same_thread
|
||||||
|
Loading…
x
Reference in New Issue
Block a user