Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.X] Replace future deprecated cgi.FieldStorage with multipart module #731

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"pytest>=7<8",
"werkzeug>=2<3",
"watchdog>=2<3",
"multipart>=0.2,<0.3",
],
# See https://pypi.python.org/pypi?%3Aaction=list_classifiers
classifiers=[
Expand Down
30 changes: 16 additions & 14 deletions src/masonite/input/InputBag.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
from urllib.parse import parse_qs
import re
import json
import cgi
import re
import multipart
from ..utils.structures import data_get
from ..filesystem import UploadedFile

Expand Down Expand Up @@ -56,26 +55,29 @@ def parse(self, environ):
self.post_data = self.parse_dict(parsed_request_body)

elif "multipart/form-data" in environ.get("CONTENT_TYPE", ""):
fields = cgi.FieldStorage(
fp=environ["wsgi.input"],
environ=environ,
keep_blank_values=1,
)

for name in fields:
value = fields.getvalue(name)
data, files = multipart.parse_form_data(environ)

for name, value in data.items():
self.post_data.update({name: value})

for name, value in files.items():
# self.assertEqual(files['file1'].file.read(), to_bytes('abc'))
# self.assertEqual(files['file1'].filename, 'random.png')
# self.assertEqual(files['file1'].name, 'file1')
# self.assertEqual(files['file1'].content_type, 'image/png')
if isinstance(value, list):
files = []
k = 0
for item in value:
# TODO: should we read it now ?
# later: process value.content_type
files.append(
UploadedFile(fields[name][k].filename, value[k])
UploadedFile(value.filename, value.file.read())
)
k += 1
self.post_data.update({name: files})
elif isinstance(value, bytes):
elif isinstance(value, multipart.MultipartPart):
self.post_data.update(
{name: [UploadedFile(fields[name].filename, value)]}
{name: [UploadedFile(value.filename, value.file.read())]}
)
else:
self.post_data.update({name: value})
Expand Down