Remove illegal characters from filepath when uploading to device.

This commit is contained in:
John Schember 2009-06-28 15:29:21 -04:00
parent c2f74ca22a
commit 1a10ff20ad
4 changed files with 25 additions and 6 deletions

View File

@ -68,10 +68,11 @@ class CYBOOKG3(USBMS):
newpath = os.path.join(newpath, mdata.get('authors', _('Unknown'))) newpath = os.path.join(newpath, mdata.get('authors', _('Unknown')))
newpath = os.path.join(newpath, mdata.get('title', _('Unknown'))) newpath = os.path.join(newpath, mdata.get('title', _('Unknown')))
newpath = self._sanitize_path(newpath)
if not os.path.exists(newpath): if not os.path.exists(newpath):
os.makedirs(newpath) os.makedirs(newpath)
filepath = os.path.join(newpath, names.next()) filepath = self._sanitize_path(os.path.join(newpath, names.next()))
paths.append(filepath) paths.append(filepath)
if hasattr(infile, 'read'): if hasattr(infile, 'read'):

View File

@ -78,10 +78,11 @@ class JETBOOK(USBMS):
if newpath == path: if newpath == path:
newpath = os.path.join(newpath, author, title) newpath = os.path.join(newpath, author, title)
newpath = self._sanitize_path(newpath)
if not os.path.exists(newpath): if not os.path.exists(newpath):
os.makedirs(newpath) os.makedirs(newpath)
filepath = os.path.join(newpath, fname) filepath = self._sanitize_path(os.path.join(newpath, fname))
paths.append(filepath) paths.append(filepath)
if hasattr(infile, 'read'): if hasattr(infile, 'read'):

View File

@ -4,7 +4,9 @@ __copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net> ' \
''' '''
Device driver for the SONY PRS-505 Device driver for the SONY PRS-505
''' '''
import os, time import os
import re
import time
from itertools import cycle from itertools import cycle
from calibre.devices.usbms.cli import CLI from calibre.devices.usbms.cli import CLI
@ -99,6 +101,11 @@ class PRS505(CLI, Device):
self.report_progress(1.0, _('Getting list of books on device...')) self.report_progress(1.0, _('Getting list of books on device...'))
return bl return bl
def _sanitize_path(self, path):
path = re.sub('[?<>:*|\^]+', '', path)
path = re.sub('[\.\s]+$', '', path)
return path
def upload_books(self, files, names, on_card=None, end_session=True, def upload_books(self, files, names, on_card=None, end_session=True,
metadata=None): metadata=None):
if on_card == 'carda' and not self._card_a_prefix: if on_card == 'carda' and not self._card_a_prefix:
@ -162,10 +169,11 @@ class PRS505(CLI, Device):
newpath = os.path.join(newpath, mdata.get('authors', _('Unknown'))) newpath = os.path.join(newpath, mdata.get('authors', _('Unknown')))
newpath = os.path.join(newpath, mdata.get('title', _('Unknown'))) newpath = os.path.join(newpath, mdata.get('title', _('Unknown')))
newpath = self._sanitize_path(newpath)
if not os.path.exists(newpath): if not os.path.exists(newpath):
os.makedirs(newpath) os.makedirs(newpath)
filepath = os.path.join(newpath, names.next()) filepath = self._sanitize_path(os.path.join(newpath, names.next()))
paths.append(filepath) paths.append(filepath)
self.put_file(infile, paths[-1], replace_file=True) self.put_file(infile, paths[-1], replace_file=True)

View File

@ -7,7 +7,10 @@ driver. It is intended to be subclassed with the relevant parts implemented
for a particular device. for a particular device.
''' '''
import os, fnmatch, shutil import os
import fnmatch
import shutil
import re
from itertools import cycle from itertools import cycle
from calibre.ebooks.metadata import authors_to_string from calibre.ebooks.metadata import authors_to_string
@ -113,6 +116,11 @@ class USBMS(CLI, Device):
raise FreeSpaceError(_("There is insufficient free space on the storage card")) raise FreeSpaceError(_("There is insufficient free space on the storage card"))
return path return path
def _sanitize_path(self, path):
path = re.sub('[?<>:*|\^]+', '', path)
path = re.sub('[\.\s]+$', '', path)
return path
def upload_books(self, files, names, on_card=None, end_session=True, def upload_books(self, files, names, on_card=None, end_session=True,
metadata=None): metadata=None):
@ -145,10 +153,11 @@ class USBMS(CLI, Device):
mdata.get('authors', _('Unknown')), mdata.get('authors', _('Unknown')),
mdata.get('title', _('Unknown'))) mdata.get('title', _('Unknown')))
newpath = self._sanitize_path(newpath)
if not os.path.exists(newpath): if not os.path.exists(newpath):
os.makedirs(newpath) os.makedirs(newpath)
filepath = os.path.join(newpath, names.next()) filepath = self._sanitize_path(os.path.join(newpath, names.next()))
paths.append(filepath) paths.append(filepath)
if hasattr(infile, 'read'): if hasattr(infile, 'read'):