mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Fix #2808 (PRS-505 upload does not handle long titles well)
This commit is contained in:
parent
cf818b1c62
commit
11c0953f2d
@ -10,9 +10,11 @@ for a particular device.
|
|||||||
import os
|
import os
|
||||||
import fnmatch
|
import fnmatch
|
||||||
import shutil
|
import shutil
|
||||||
|
from math import ceil
|
||||||
from itertools import cycle
|
from itertools import cycle
|
||||||
|
|
||||||
from calibre import sanitize_file_name as sanitize
|
from calibre import sanitize_file_name as sanitize
|
||||||
|
from calibre.constants import iswindows
|
||||||
from calibre.ebooks.metadata import authors_to_string
|
from calibre.ebooks.metadata import authors_to_string
|
||||||
from calibre.devices.usbms.cli import CLI
|
from calibre.devices.usbms.cli import CLI
|
||||||
from calibre.devices.usbms.device import Device
|
from calibre.devices.usbms.device import Device
|
||||||
@ -127,6 +129,7 @@ class USBMS(CLI, Device):
|
|||||||
|
|
||||||
for i, infile in enumerate(files):
|
for i, infile in enumerate(files):
|
||||||
newpath = path
|
newpath = path
|
||||||
|
resizable = []
|
||||||
|
|
||||||
if self.SUPPORTS_SUB_DIRS:
|
if self.SUPPORTS_SUB_DIRS:
|
||||||
mdata = metadata.next()
|
mdata = metadata.next()
|
||||||
@ -135,23 +138,54 @@ class USBMS(CLI, Device):
|
|||||||
for tag in mdata['tags']:
|
for tag in mdata['tags']:
|
||||||
if tag.startswith(_('News')):
|
if tag.startswith(_('News')):
|
||||||
newpath = os.path.join(newpath, 'news')
|
newpath = os.path.join(newpath, 'news')
|
||||||
newpath = os.path.join(newpath, sanitize(mdata.get('title', '')))
|
c = sanitize(mdata.get('title', ''))
|
||||||
newpath = os.path.join(newpath, sanitize(mdata.get('timestamp', '')))
|
if c:
|
||||||
|
newpath = os.path.join(newpath, c)
|
||||||
|
resizable.append(c)
|
||||||
|
c = sanitize(mdata.get('timestamp', ''))
|
||||||
|
if c:
|
||||||
|
newpath = os.path.join(newpath, c)
|
||||||
|
resizable.append(c)
|
||||||
break
|
break
|
||||||
elif tag.startswith('/'):
|
elif tag.startswith('/'):
|
||||||
newpath += tag
|
for c in tag.split('/'):
|
||||||
newpath = os.path.normpath(newpath)
|
c = sanitize(c)
|
||||||
|
if not c: continue
|
||||||
|
newpath = os.path.join(newpath, c)
|
||||||
|
resizable.append(c)
|
||||||
break
|
break
|
||||||
|
|
||||||
if newpath == path:
|
if newpath == path:
|
||||||
newpath = os.path.join(newpath,
|
c = sanitize(mdata.get('authors', _('Unknown')))
|
||||||
sanitize(mdata.get('authors', _('Unknown'))),
|
if c:
|
||||||
sanitize(mdata.get('title', _('Unknown'))))
|
newpath = os.path.join(newpath, c)
|
||||||
|
resizable.append(c)
|
||||||
|
c = sanitize(mdata.get('title', _('Unknown')))
|
||||||
|
if c:
|
||||||
|
newpath = os.path.join(newpath, c)
|
||||||
|
resizable.append(c)
|
||||||
|
|
||||||
|
newpath = os.path.abspath(newpath)
|
||||||
|
fname = sanitize(names.next())
|
||||||
|
resizable.append(fname)
|
||||||
|
filepath = os.path.join(newpath, fname)
|
||||||
|
|
||||||
|
if iswindows and len(filepath) > 250:
|
||||||
|
extra = len(filepath) - 250
|
||||||
|
delta = int(ceil(extra/float(len(resizable))))
|
||||||
|
for x in resizable:
|
||||||
|
if delta > len(x):
|
||||||
|
r = ''
|
||||||
|
else:
|
||||||
|
r = x[:-delta]
|
||||||
|
filepath = filepath.replace(os.sep+x+os.sep, os.sep+r+os.sep)
|
||||||
|
filepath = filepath.replace(os.sep+os.sep, os.sep)
|
||||||
|
newpath = os.path.dirname(filepath)
|
||||||
|
|
||||||
|
|
||||||
if not os.path.exists(newpath):
|
if not os.path.exists(newpath):
|
||||||
os.makedirs(newpath)
|
os.makedirs(newpath)
|
||||||
|
|
||||||
filepath = os.path.join(newpath, sanitize(names.next()))
|
|
||||||
paths.append(filepath)
|
paths.append(filepath)
|
||||||
|
|
||||||
if hasattr(infile, 'read'):
|
if hasattr(infile, 'read'):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user