Fix various file access bugs.

This commit is contained in:
Kovid Goyal 2007-10-13 17:30:45 +00:00
parent ba7f030a88
commit 2e52d6dfe3

View File

@ -241,8 +241,6 @@ class HTMLConverter(object):
self.link_level += 1 self.link_level += 1
paths = [link['path'] for link in self.links] paths = [link['path'] for link in self.links]
def is_baen(self, soup): def is_baen(self, soup):
return bool(soup.find('meta', attrs={'name':'Publisher', return bool(soup.find('meta', attrs={'name':'Publisher',
'content':re.compile('Baen', re.IGNORECASE)})) 'content':re.compile('Baen', re.IGNORECASE)}))
@ -1248,7 +1246,7 @@ class HTMLConverter(object):
path = munge_paths(self.target_prefix, tag['href'])[0] path = munge_paths(self.target_prefix, tag['href'])[0]
ext = os.path.splitext(path)[1] ext = os.path.splitext(path)[1]
if ext: ext = ext[1:].lower() if ext: ext = ext[1:].lower()
if os.access(path, os.R_OK): if os.access(path, os.R_OK) and os.path.isfile(path):
if ext in ['png', 'jpg', 'bmp', 'jpeg']: if ext in ['png', 'jpg', 'bmp', 'jpeg']:
self.process_image(path, tag_css) self.process_image(path, tag_css)
else: else:
@ -1260,14 +1258,14 @@ class HTMLConverter(object):
if tag.has_key('id') or tag.has_key('name'): if tag.has_key('id') or tag.has_key('name'):
key = 'name' if tag.has_key('name') else 'id' key = 'name' if tag.has_key('name') else 'id'
self.targets[self.target_prefix+tag[key]] = self.current_block self.targets[self.target_prefix+tag[key]] = self.current_block
else: elif not urlparse(tag['href'])[0]:
self.logger.warn('Could not follow link to '+tag['href']) self.logger.warn('Could not follow link to '+tag['href'])
elif tag.has_key('name') or tag.has_key('id'): elif tag.has_key('name') or tag.has_key('id'):
self.process_anchor(tag, tag_css, tag_pseudo_css) self.process_anchor(tag, tag_css, tag_pseudo_css)
elif tagname == 'img': elif tagname == 'img':
if tag.has_key('src'): if tag.has_key('src'):
path = munge_paths(self.target_prefix, tag['src'])[0] path = munge_paths(self.target_prefix, tag['src'])[0]
if os.access(path, os.R_OK): if os.access(path, os.R_OK) and os.path.isfile(path):
width, height = None, None width, height = None, None
try: try:
width = int(tag['width']) width = int(tag['width'])
@ -1276,7 +1274,7 @@ class HTMLConverter(object):
pass pass
dropcaps = tag.has_key('class') and tag['class'] == 'libprs500_dropcaps' dropcaps = tag.has_key('class') and tag['class'] == 'libprs500_dropcaps'
self.process_image(path, tag_css, width, height, dropcaps=dropcaps) self.process_image(path, tag_css, width, height, dropcaps=dropcaps)
else: elif not urlparse(tag['src'])[0]:
self.logger.warn('Could not find image: '+tag['src']) self.logger.warn('Could not find image: '+tag['src'])
else: else:
self.logger.debug("Failed to process: %s", str(tag)) self.logger.debug("Failed to process: %s", str(tag))
@ -1532,27 +1530,25 @@ def process_file(path, options, logger=None):
level = logging.DEBUG if options.verbose else logging.INFO level = logging.DEBUG if options.verbose else logging.INFO
logger = logging.getLogger('html2lrf') logger = logging.getLogger('html2lrf')
setup_cli_handlers(logger, level) setup_cli_handlers(logger, level)
cwd = os.getcwd() path = os.path.normpath(os.path.abspath(path))
default_title = filename_to_utf8(os.path.splitext(os.path.basename(path))[0]) default_title = filename_to_utf8(os.path.splitext(os.path.basename(path))[0])
dirpath = os.path.dirname(path) dirpath = os.path.dirname(path)
try:
cpath, tpath = '', '' tpath = ''
try_opf(path, options, logger) try_opf(path, options, logger)
if options.cover: if options.cover:
cpath = os.path.join(dirpath, os.path.basename(options.cover)) options.cover = os.path.expanduser(options.cover)
if not os.path.exists(cpath): if not os.path.isabs(options.cover):
cpath = os.path.abspath(os.path.expanduser(options.cover)) options.cover = os.path.join(dirpath, options.cover)
options.cover = cpath
if os.access(options.cover, os.R_OK): if os.access(options.cover, os.R_OK):
th = Device.THUMBNAIL_HEIGHT th = Device.THUMBNAIL_HEIGHT
im = PILImage.open(os.path.join(cwd, cpath)) im = PILImage.open(options.cover)
cim = im.resize((options.profile.screen_width, cim = im.resize((options.profile.screen_width,
options.profile.screen_height - options.profile.fudge), options.profile.screen_height - options.profile.fudge),
PILImage.BICUBIC).convert('RGB') PILImage.BICUBIC).convert('RGB')
cf = PersistentTemporaryFile(prefix=__appname__+"_", suffix=".jpg") cf = PersistentTemporaryFile(prefix=__appname__+"_", suffix=".jpg")
cf.close() cf.close()
cim.save(cf.name) cim.save(cf.name)
cpath = cf.name
tim = im.resize((int(0.75*th), th), PILImage.ANTIALIAS).convert('RGB') tim = im.resize((int(0.75*th), th), PILImage.ANTIALIAS).convert('RGB')
tf = PersistentTemporaryFile(prefix="html2lrf_", suffix=".jpg") tf = PersistentTemporaryFile(prefix="html2lrf_", suffix=".jpg")
@ -1600,7 +1596,6 @@ def process_file(path, options, logger=None):
re.compile('$') re.compile('$')
fpb = re.compile(options.force_page_break, re.IGNORECASE) if options.force_page_break else \ fpb = re.compile(options.force_page_break, re.IGNORECASE) if options.force_page_break else \
re.compile('$') re.compile('$')
options.cover = cpath
options.force_page_break = fpb options.force_page_break = fpb
options.link_exclude = le options.link_exclude = le
options.page_break = pb options.page_break = pb
@ -1620,21 +1615,20 @@ def process_file(path, options, logger=None):
if not oname: if not oname:
suffix = '.lrs' if options.lrs else '.lrf' suffix = '.lrs' if options.lrs else '.lrf'
name = os.path.splitext(os.path.basename(path))[0] + suffix name = os.path.splitext(os.path.basename(path))[0] + suffix
oname = os.path.join(cwd,name) oname = os.path.join(os.getcwd(), name)
oname = os.path.abspath(os.path.expanduser(oname)) oname = os.path.abspath(os.path.expanduser(oname))
conv.writeto(oname, lrs=options.lrs) conv.writeto(oname, lrs=options.lrs)
logger.info('Output written to %s', oname) logger.info('Output written to %s', oname)
conv.cleanup() conv.cleanup()
return oname return oname
finally:
os.chdir(cwd)
def try_opf(path, options, logger): def try_opf(path, options, logger):
try: try:
opf = glob.glob(os.path.join(os.path.dirname(path),'*.opf'))[0] opf = glob.glob(os.path.join(os.path.dirname(path),'*.opf'))[0]
except IndexError: except IndexError:
return return
opf = OPFReader(open(opf, 'rb'), os.path.dirname(os.path.abspath(opf))) dirpath = os.path.dirname(os.path.abspath(opf))
opf = OPFReader(open(opf, 'rb'), dirpath)
try: try:
title = opf.title title = opf.title
if title and not options.title: if title and not options.title:
@ -1655,7 +1649,8 @@ def try_opf(path, options, logger):
if not options.cover: if not options.cover:
cover = opf.cover cover = opf.cover
if cover: if cover:
cover = os.path.join(os.path.dirname(path), cover) if not os.path.isabs(cover):
cover = os.path.join(dirpath, cover)
if os.access(cover, os.R_OK): if os.access(cover, os.R_OK):
try: try:
PILImage.open(cover) PILImage.open(cover)