Skip to content

Commit

Permalink
Remove usage of deprecated cgi.FieldStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
amol- committed Feb 19, 2024
1 parent 39e6442 commit 8e3113e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
7 changes: 4 additions & 3 deletions depot/io/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,10 @@ def get(self, file_or_id): # pragma: no cover
def create(self, content, filename=None, content_type=None): # pragma: no cover
"""Saves a new file and returns the ID of the newly created file.
``content`` parameter can either be ``bytes``, another ``file object``
or a :class:`cgi.FieldStorage`. When ``filename`` and ``content_type``
parameters are not provided they are deducted from the content itself.
``content`` parameter can either be ``bytes``, another ``file object``,
``multipart.MultipartPart`` or a :class:`cgi.FieldStorage`.
When ``filename`` and ``content_type`` parameters are not provided
they are deducted from the content itself.
"""
return

Expand Down
15 changes: 10 additions & 5 deletions depot/io/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import cgi
import mimetypes
import os
from datetime import datetime
Expand All @@ -20,13 +19,13 @@ def file_from_content(content):
``bytes`` to an actual file.
"""
f = content
if isinstance(content, cgi.FieldStorage):
f = content.file
elif isinstance(content, FileIntent):
if isinstance(content, FileIntent):
f = content._fileobj
elif isinstance(content, byte_string):
f = SpooledTemporaryFile(INMEMORY_FILESIZE)
f.write(content)
elif _is_fieldstorage_like(content):
f = content.file
f.seek(0)
return f

Expand Down Expand Up @@ -90,7 +89,7 @@ def _resolve(self, fileobj, filename, content_type):
return content, filename, content_type

def _get_content_from_file_obj(self, fileobj):
if isinstance(fileobj, cgi.FieldStorage):
if _is_fieldstorage_like(fileobj):
return fileobj.file
return fileobj

Expand All @@ -105,3 +104,9 @@ def _get_content_type_from_fileobj(self, fileobj):
return fileobj.content_type
elif getattr(fileobj, 'type', None) is not None:
return fileobj.type


def _is_fieldstorage_like(obj):
# Detect cgi.FieldStorage and multipart modules
return (getattr(obj, 'filename', None) is not None and \
getattr(obj, 'file', None) not in (None, False))
7 changes: 3 additions & 4 deletions depot/validators.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import cgi
from depot.io.utils import FileIntent
from depot.io.utils import FileIntent, _is_fieldstorage_like
from depot.io.interfaces import FileStorage

__all__ = ('TW2FileIntentValidator', )
Expand All @@ -16,7 +15,7 @@ class TW2FileIntentValidator(tw2.core.Validator):
so that it preserves file details like filename
and content type when uploaded. This is mostly
required when working with Sprox that converts
the cgi.FieldStorage to a standard BLOB when
the cgi.FieldStorage or multipart to a standard BLOB when
handling uploaded files, causing a loss of all
file metadata.
"""
Expand All @@ -27,7 +26,7 @@ class TW2FileIntentValidator(tw2.core.Validator):
}

def _convert_to_python(self, value, state=None):
if isinstance(value, cgi.FieldStorage):
if is_fieldstorage_like(value):
if self.required and not getattr(value, 'filename', None):
raise tw2.core.ValidationError('required', self)

Expand Down

0 comments on commit 8e3113e

Please sign in to comment.