Fix #2808 (PRS-505 upload does not handle long titles well)

This commit is contained in:
Kovid Goyal 2009-07-13 20:57:38 -06:00
parent cf818b1c62
commit 11c0953f2d

View File

@ -10,9 +10,11 @@ for a particular device.
import os
import fnmatch
import shutil
from math import ceil
from itertools import cycle
from calibre import sanitize_file_name as sanitize
from calibre.constants import iswindows
from calibre.ebooks.metadata import authors_to_string
from calibre.devices.usbms.cli import CLI
from calibre.devices.usbms.device import Device
@ -127,6 +129,7 @@ class USBMS(CLI, Device):
for i, infile in enumerate(files):
newpath = path
resizable = []
if self.SUPPORTS_SUB_DIRS:
mdata = metadata.next()
@ -135,23 +138,54 @@ class USBMS(CLI, Device):
for tag in mdata['tags']:
if tag.startswith(_('News')):
newpath = os.path.join(newpath, 'news')
newpath = os.path.join(newpath, sanitize(mdata.get('title', '')))
newpath = os.path.join(newpath, sanitize(mdata.get('timestamp', '')))
c = sanitize(mdata.get('title', ''))
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
elif tag.startswith('/'):
newpath += tag
newpath = os.path.normpath(newpath)
for c in tag.split('/'):
c = sanitize(c)
if not c: continue
newpath = os.path.join(newpath, c)
resizable.append(c)
break
if newpath == path:
newpath = os.path.join(newpath,
sanitize(mdata.get('authors', _('Unknown'))),
sanitize(mdata.get('title', _('Unknown'))))
c = sanitize(mdata.get('authors', _('Unknown')))
if c:
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):
os.makedirs(newpath)
filepath = os.path.join(newpath, sanitize(names.next()))
paths.append(filepath)
if hasattr(infile, 'read'):