Skip to content

Commit

Permalink
Add better blob support to HTTPRequest.ZopeFieldStorage.
Browse files Browse the repository at this point in the history
  • Loading branch information
hannosch committed Jul 23, 2016
1 parent e575c54 commit 4d5910f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Bugs Fixed
Features Added
++++++++++++++

- Add better blob support to HTTPRequest.ZopeFieldStorage.

Restructuring
+++++++++++++

Expand Down
34 changes: 31 additions & 3 deletions src/ZPublisher/HTTPRequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@
import codecs
from copy import deepcopy
import os
from os import unlink
from os.path import isfile
import random
import re
import sys
import tempfile
from tempfile import (
mkstemp,
_TemporaryFileWrapper,
)
import time
from urllib import unquote
from urllib import splittype
Expand Down Expand Up @@ -1601,10 +1606,33 @@ def sane_environment(env):
return dict


class TemporaryFileWrapper(_TemporaryFileWrapper):
"""
Variant of tempfile._TemporaryFileWrapper that doesn't rely on the
automatic Windows behavior of deleting closed files, which even
happens, when the file has been moved -- e.g. to the blob storage,
and doesn't complain about such a move either.
"""

unlink = staticmethod(unlink)
isfile = staticmethod(isfile)

def close(self):
if not self.close_called:
self.close_called = True
self.file.close()

def __del__(self):
self.close()
if self.isfile(self.name):
self.unlink(self.name)


class ZopeFieldStorage(FieldStorage):

def make_file(self, binary=None):
return tempfile.NamedTemporaryFile("w+b")
handle, name = mkstemp()
return TemporaryFileWrapper(os.fdopen(handle, 'w+b'), name)


# Original version: zope.publisher.browser.FileUpload
Expand Down Expand Up @@ -1648,7 +1676,7 @@ def __init__(self, aFieldStorage):
# self.headers so that managed code can access them.
try:
self.headers.__allow_access_to_unprotected_subobjects__ = 1
except:
except Exception:
pass

def __nonzero__(self):
Expand Down

0 comments on commit 4d5910f

Please sign in to comment.