Don't copy input file for GS if not necessary, length variable name fix

This commit is contained in:
wiredfool 2014-09-02 23:09:04 -07:00
parent 6dc276599e
commit 81076d5f29

View File

@ -86,13 +86,19 @@ def Ghostscript(tile, size, fp, scale=1):
out_fd, outfile = tempfile.mkstemp() out_fd, outfile = tempfile.mkstemp()
os.close(out_fd) os.close(out_fd)
in_fd, infile = tempfile.mkstemp()
infile_temp = None
if hasattr(fp, 'name') and os.path.exists(fp.name):
infile = fp.name
else:
in_fd, infile_temp = tempfile.mkstemp()
os.close(in_fd) os.close(in_fd)
infile = infile_temp
# ignore length and offset! # ignore length and offset!
# ghostscript can read it # ghostscript can read it
# copy whole file to read in ghostscript # copy whole file to read in ghostscript
with open(infile, 'wb') as f: with open(infile_temp, 'wb') as f:
# fetch length of fp # fetch length of fp
fp.seek(0, 2) fp.seek(0, 2)
fsize = fp.tell() fsize = fp.tell()
@ -104,7 +110,7 @@ def Ghostscript(tile, size, fp, scale=1):
s = fp.read(min(lengthfile, 100*1024)) s = fp.read(min(lengthfile, 100*1024))
if not s: if not s:
break break
length -= len(s) lengthfile -= len(s)
f.write(s) f.write(s)
# Build ghostscript command # Build ghostscript command
@ -136,7 +142,8 @@ def Ghostscript(tile, size, fp, scale=1):
finally: finally:
try: try:
os.unlink(outfile) os.unlink(outfile)
os.unlink(infile) if infile_temo:
os.unlink(infile_temp)
except: pass except: pass
return im return im