Apply same file checks files as for image files.

This commit is contained in:
Bastian Kleineidam 2013-12-05 18:29:15 +01:00
parent 599672acbf
commit 03fff069ee
2 changed files with 33 additions and 20 deletions

View file

@ -2,10 +2,9 @@
# Copyright (C) 2004-2005 Tristan Seligmann and Jonathan Jacobs # Copyright (C) 2004-2005 Tristan Seligmann and Jonathan Jacobs
# Copyright (C) 2012-2013 Bastian Kleineidam # Copyright (C) 2012-2013 Bastian Kleineidam
import os import os
import codecs
from .output import out from .output import out
from .util import getImageObject, normaliseURL, unquote, strsize, getDirname, getFilename from .util import getImageObject, normaliseURL, unquote, getDirname, getFilename, writeFile
from .events import getHandler from .events import getHandler
class ComicStrip(object): class ComicStrip(object):
@ -86,25 +85,11 @@ class ComicImage(object):
out.warn(u"Empty content from %s, try again..." % self.url) out.warn(u"Empty content from %s, try again..." % self.url)
self.connect() self.connect()
content = self.urlobj.content content = self.urlobj.content
try: out.debug(u'Writing comic to file %s...' % fn)
out.debug(u'Writing comic to file %s...' % fn) writeFile(fn, content)
with open(fn, 'wb') as comicOut:
comicOut.write(content)
comicOut.flush()
os.fsync(comicOut.fileno())
size = os.path.getsize(fn)
if size == 0:
raise OSError("empty file %s" % fn)
except Exception:
if os.path.isfile(fn):
os.remove(fn)
raise
else:
out.info(u"Saved %s (%s)." % (fn, strsize(size)))
getHandler().comicDownloaded(self, fn, text=self.text)
if self.text: if self.text:
fntext = os.path.join(comicDir, "%s.txt" % self.filename) fntext = os.path.join(comicDir, "%s.txt" % self.filename)
out.debug(u'Writing comic text to file %s...' % fntext) out.debug(u'Writing comic text to file %s...' % fntext)
with codecs.open(fntext, 'w', 'utf-8') as textOut: writeFile(fntext, self.text, encoding='utf-8')
textOut.write(self.text) getHandler().comicDownloaded(self, fn, text=self.text)
return fn, True return fn, True

View file

@ -19,6 +19,7 @@ import sys
import os import os
import cgi import cgi
import re import re
import codecs
import traceback import traceback
import time import time
import subprocess import subprocess
@ -565,3 +566,30 @@ def strlimit (s, length=72):
def getLangName(code): def getLangName(code):
"""Get name of language specified by ISO 693-1 code.""" """Get name of language specified by ISO 693-1 code."""
return Iso2Language[code] return Iso2Language[code]
def writeFile(filename, content, encoding=None):
"""Write content to given filename. Checks for zero-sized files.
If encoding is given writes to a codec.open() file."""
if not content:
raise OSError("empty content for file %s" % filename)
def getfp(filename, encoding):
if encoding:
return codecs.open(filename, 'w', encoding)
return open(filename, 'wb')
try:
with getfp(filename, encoding) as fp:
fp.write(content)
fp.flush()
os.fsync(fp.fileno())
size = os.path.getsize(filename)
if size == 0:
raise OSError("empty file %s" % filename)
except Exception:
if os.path.isfile(filename):
os.remove(filename)
raise
else:
out.info(u"Saved %s (%s)." % (filename, strsize(size)))