mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
sanitize file names with build in santitize function.
This commit is contained in:
parent
0a9ddcda0c
commit
b975eb135c
@ -4,9 +4,11 @@ __copyright__ = '2009, John Schember <john at nachtimwald.com>'
|
|||||||
Device driver for Bookeen's Cybook Gen 3
|
Device driver for Bookeen's Cybook Gen 3
|
||||||
'''
|
'''
|
||||||
|
|
||||||
import os, shutil
|
import os
|
||||||
|
import shutil
|
||||||
from itertools import cycle
|
from itertools import cycle
|
||||||
|
|
||||||
|
from calibre import sanitize_file_name as sanitize
|
||||||
from calibre.devices.errors import DeviceError, FreeSpaceError
|
from calibre.devices.errors import DeviceError, FreeSpaceError
|
||||||
from calibre.devices.usbms.driver import USBMS
|
from calibre.devices.usbms.driver import USBMS
|
||||||
import calibre.devices.cybookg3.t2b as t2b
|
import calibre.devices.cybookg3.t2b as t2b
|
||||||
@ -57,22 +59,21 @@ class CYBOOKG3(USBMS):
|
|||||||
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, mdata.get('title', ''))
|
newpath = os.path.join(newpath, sanitize(mdata.get('title', '')))
|
||||||
newpath = os.path.join(newpath, mdata.get('timestamp', ''))
|
newpath = os.path.join(newpath, sanitize(mdata.get('timestamp', '')))
|
||||||
elif tag.startswith('/'):
|
elif tag.startswith('/'):
|
||||||
newpath += tag
|
newpath += tag
|
||||||
newpath = os.path.normpath(newpath)
|
newpath = os.path.normpath(newpath)
|
||||||
break
|
break
|
||||||
|
|
||||||
if newpath == path:
|
if newpath == path:
|
||||||
newpath = os.path.join(newpath, mdata.get('authors', _('Unknown')))
|
newpath = os.path.join(newpath, sanitize(mdata.get('authors', _('Unknown'))))
|
||||||
newpath = os.path.join(newpath, mdata.get('title', _('Unknown')))
|
newpath = os.path.join(newpath, sanitize(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 = self._sanitize_path(os.path.join(newpath, names.next()))
|
filepath = os.path.join(newpath, sanitize(names.next()))
|
||||||
paths.append(filepath)
|
paths.append(filepath)
|
||||||
|
|
||||||
if hasattr(infile, 'read'):
|
if hasattr(infile, 'read'):
|
||||||
|
@ -78,11 +78,10 @@ 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 = self._sanitize_path(os.path.join(newpath, fname))
|
filepath = os.path.join(newpath, fname)
|
||||||
paths.append(filepath)
|
paths.append(filepath)
|
||||||
|
|
||||||
if hasattr(infile, 'read'):
|
if hasattr(infile, 'read'):
|
||||||
|
@ -5,10 +5,10 @@ __copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net> ' \
|
|||||||
Device driver for the SONY PRS-505
|
Device driver for the SONY PRS-505
|
||||||
'''
|
'''
|
||||||
import os
|
import os
|
||||||
import re
|
|
||||||
import time
|
import time
|
||||||
from itertools import cycle
|
from itertools import cycle
|
||||||
|
|
||||||
|
from calibre import sanitize_file_name as sanitize
|
||||||
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
|
||||||
from calibre.devices.errors import DeviceError, FreeSpaceError
|
from calibre.devices.errors import DeviceError, FreeSpaceError
|
||||||
@ -101,11 +101,6 @@ 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:
|
||||||
@ -157,8 +152,8 @@ class PRS505(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, mdata.get('title', ''))
|
newpath = os.path.join(newpath, sanitize(mdata.get('title', '')))
|
||||||
newpath = os.path.join(newpath, mdata.get('timestamp', ''))
|
newpath = os.path.join(newpath, sanitize(mdata.get('timestamp', '')))
|
||||||
elif tag.startswith('/'):
|
elif tag.startswith('/'):
|
||||||
newpath = path
|
newpath = path
|
||||||
newpath += tag
|
newpath += tag
|
||||||
@ -166,14 +161,13 @@ class PRS505(CLI, Device):
|
|||||||
break
|
break
|
||||||
|
|
||||||
if newpath == path:
|
if newpath == path:
|
||||||
newpath = os.path.join(newpath, mdata.get('authors', _('Unknown')))
|
newpath = os.path.join(newpath, sanitize(mdata.get('authors', _('Unknown'))))
|
||||||
newpath = os.path.join(newpath, mdata.get('title', _('Unknown')))
|
newpath = os.path.join(newpath, sanitize(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 = self._sanitize_path(os.path.join(newpath, names.next()))
|
filepath = os.path.join(newpath, sanitize(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)
|
||||||
|
@ -10,9 +10,9 @@ for a particular device.
|
|||||||
import os
|
import os
|
||||||
import fnmatch
|
import fnmatch
|
||||||
import shutil
|
import shutil
|
||||||
import re
|
|
||||||
from itertools import cycle
|
from itertools import cycle
|
||||||
|
|
||||||
|
from calibre import sanitize_file_name as sanitize
|
||||||
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
|
||||||
@ -116,11 +116,6 @@ 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):
|
||||||
|
|
||||||
@ -140,8 +135,8 @@ 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, mdata.get('title', ''))
|
newpath = os.path.join(newpath, sanitize(mdata.get('title', '')))
|
||||||
newpath = os.path.join(newpath, mdata.get('timestamp', ''))
|
newpath = os.path.join(newpath, sanitize(mdata.get('timestamp', '')))
|
||||||
break
|
break
|
||||||
elif tag.startswith('/'):
|
elif tag.startswith('/'):
|
||||||
newpath += tag
|
newpath += tag
|
||||||
@ -150,14 +145,13 @@ class USBMS(CLI, Device):
|
|||||||
|
|
||||||
if newpath == path:
|
if newpath == path:
|
||||||
newpath = os.path.join(newpath,
|
newpath = os.path.join(newpath,
|
||||||
mdata.get('authors', _('Unknown')),
|
sanitize(mdata.get('authors', _('Unknown'))),
|
||||||
mdata.get('title', _('Unknown')))
|
sanitize(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 = self._sanitize_path(os.path.join(newpath, names.next()))
|
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