Skip to content

Commit

Permalink
Merge pull request #731 from girardinsamuel/feat/remove-cgi-usage
Browse files Browse the repository at this point in the history
[5.X] Replace future deprecated `cgi.FieldStorage` with `multipart` module
  • Loading branch information
josephmancuso authored Dec 6, 2023
2 parents 7b4df44 + ba6dc46 commit 338ebb7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
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

0 comments on commit 338ebb7

Please sign in to comment.