Commit d881c7e8 authored by Arnaud Fontaine's avatar Arnaud Fontaine

Zope 4.x: Fix OFS.Image PUT() for Python 2 (upstream patch for #1015, #1016).

parent 8cd7a917
From 9a603533bacd9311ee634d8509c090037112b637 Mon Sep 17 00:00:00 2001
From: Arnaud Fontaine <arnaud.fontaine@nexedi.com>
Date: Fri, 25 Feb 2022 14:46:34 +0100
Subject: [PATCH] Fix PUT() for Python 2 (#1015).
---
src/OFS/Image.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/OFS/Image.py b/src/OFS/Image.py
index d81d2a0fa..c33ef68a5 100644
--- a/src/OFS/Image.py
+++ b/src/OFS/Image.py
@@ -661,7 +661,7 @@ class File(
file = REQUEST['BODYFILE']
data, size = self._read_data(file)
- if isinstance(data, str):
+ if isinstance(data, text_type):
data = data.encode('UTF-8')
content_type = self._get_content_type(file, data, self.__name__,
type or self.content_type)
--
2.35.1
From d3885eadb919abfcc86a82431deb9b9916127602 Mon Sep 17 00:00:00 2001
From: dieter <dieter@handshake.de>
Date: Wed, 2 Mar 2022 08:31:58 +0100
Subject: [PATCH] fix `OFS.Image.File.PUT` -- Issue 1015
work around `cgi` bug
make `flake8` happy
---
src/OFS/Image.py | 47 ++++++++++++++++++++++++++++++++++++++++++++---
1 file changed, 44 insertions(+), 3 deletions(-)
diff --git a/src/OFS/Image.py b/src/OFS/Image.py
index c7b012a5e..fde006deb 100644
--- a/src/OFS/Image.py
+++ b/src/OFS/Image.py
@@ -16,6 +16,8 @@
import struct
from email.generator import _make_boundary
from io import BytesIO
+from io import TextIOBase
+from tempfile import TemporaryFile
from warnings import warn
from six import PY2
@@ -568,6 +570,16 @@ class File(
self, REQUEST, manage_tabs_message=msg)
def _get_content_type(self, file, body, id, content_type=None):
+ """return content type or ``None``.
+
+ *file* usually is a ``FileUpload`` (like) instance; if this
+ specifies a content type, it is used. If *file*
+ is not ``FileUpload`` like, it is ignored and the
+ content type is guessed from the other parameters.
+
+ *body* is either a ``bytes`` or a ``Pdata`` instance
+ and assumed to be the *file* data.
+ """
headers = getattr(file, 'headers', None)
if headers and 'content-type' in headers:
content_type = headers['content-type']
@@ -579,6 +591,13 @@ class File(
return content_type
def _read_data(self, file):
+ """return the data and size of *file* as tuple *data*, *size*.
+
+ *file* can be a ``bytes``, ``Pdata``, ``FileUpload`` or
+ (binary) file like instance.
+
+ For large files, *data* is a ``Pdata``, otherwise a ``bytes`` instance.
+ """
import transaction
n = 1 << 16
@@ -656,13 +675,35 @@ class File(
"""Handle HTTP PUT requests"""
self.dav__init(REQUEST, RESPONSE)
self.dav__simpleifhandler(REQUEST, RESPONSE, refresh=1)
- type = REQUEST.get_header('content-type', None)
+ type = REQUEST.get_header('content-type', None)
file = REQUEST['BODYFILE']
+ # Work around ``cgi`` bug
+ # ``cgi`` can turn the request body into a text file using
+ # the default encoding. ``File``, however, insists to work
+ # with bytes and binary files and forbids text.
+ # Convert back.
+ tfile = None
+ if isinstance(file, TextIOBase): # ``cgi`` bug
+ if hasattr(file, "buffer"):
+ file = file.buffer # underlying binary buffer
+ else:
+ from ZPublisher.HTTPRequest import default_encoding
+ tfile = TemporaryFile("wb+")
+ bufsize = 1 << 16
+ while True:
+ data = file.read(bufsize)
+ if not data:
+ break
+ tfile.write(data.encode(default_encoding))
+ file.seek(0, 0)
+ tfile.seek(0, 0)
+ file = tfile
+
data, size = self._read_data(file)
- if isinstance(data, str):
- data = data.encode('UTF-8')
+ if tfile is not None:
+ tfile.close()
content_type = self._get_content_type(file, data, self.__name__,
type or self.content_type)
self.update_data(data, content_type, size)
--
2.35.1
......@@ -617,7 +617,7 @@ python-magic-patch-options = -p1
# Zope 4 patches
Zope-patches =
${:_profile_base_location_}/../../component/egg-patch/Zope/0001-OFS-XMLExportImport.patch#12a9b9db76b3cd9035b6032d516143e0
${:_profile_base_location_}/../../component/egg-patch/Zope/0001-Fix-PUT-for-Python-2-1015.patch#e81c46a1de8916b05abc653209722c0b
${:_profile_base_location_}/../../component/egg-patch/Zope/0001-fix-OFS.Image.File.PUT-Issue-1015.patch#c706e2f572ad8cd37ed033fb5f873cbe
Zope-patch-options = -p1
[eggs-all-scripts]
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment