Update cloudinary to 1.20.0

This commit is contained in:
JonnyWong16 2020-03-21 19:11:41 -07:00
parent 1c56d9c513
commit 2984629b39
27 changed files with 2865 additions and 923 deletions

View file

@ -6,9 +6,17 @@ as multipart/form-data suitable for a HTTP POST or PUT request.
multipart/form-data is the standard way to upload files over HTTP"""
__all__ = ['gen_boundary', 'encode_and_quote', 'MultipartParam',
'encode_string', 'encode_file_header', 'get_body_size', 'get_headers',
'multipart_encode']
import mimetypes
import os
import re
from email.header import Header
from cloudinary.compat import (PY3, advance_iterator, quote_plus, to_bytearray,
to_bytes, to_string)
__all__ = [
'gen_boundary', 'encode_and_quote', 'MultipartParam', 'encode_string',
'encode_file_header', 'get_body_size', 'get_headers', 'multipart_encode']
try:
from io import UnsupportedOperation
@ -17,25 +25,19 @@ except ImportError:
try:
import uuid
def gen_boundary():
"""Returns a random string to use as the boundary for a message"""
return uuid.uuid4().hex
except ImportError:
import random, sha
import random
import sha
def gen_boundary():
"""Returns a random string to use as the boundary for a message"""
bits = random.getrandbits(160)
return sha.new(str(bits)).hexdigest()
import re, os, mimetypes
from cloudinary.compat import (PY3, string_types, to_bytes, to_string,
to_bytearray, quote_plus, advance_iterator)
try:
from email.header import Header
except ImportError:
# Python 2.4
from email.Header import Header
if PY3:
def encode_and_quote(data):
if data is None:
@ -47,7 +49,7 @@ else:
"""If ``data`` is unicode, return quote_plus(data.encode("utf-8")) otherwise return quote_plus(data)"""
if data is None:
return None
if isinstance(data, unicode):
data = data.encode("utf-8")
return quote_plus(data)
@ -65,13 +67,15 @@ if PY3:
return to_bytes(str(s))
else:
def _strify(s):
"""If s is a unicode string, encode it to UTF-8 and return the results, otherwise return str(s), or None if s is None"""
"""If s is a unicode string, encode it to UTF-8 and return the results,
otherwise return str(s), or None if s is None"""
if s is None:
return None
if isinstance(s, unicode):
return s.encode("utf-8")
return str(s)
class MultipartParam(object):
"""Represents a single parameter in a multipart/form-data request
@ -105,7 +109,7 @@ class MultipartParam(object):
transferred, and the total size.
"""
def __init__(self, name, value=None, filename=None, filetype=None,
filesize=None, fileobj=None, cb=None):
filesize=None, fileobj=None, cb=None):
self.name = Header(name).encode()
self.value = _strify(value)
if filename is None:
@ -141,7 +145,7 @@ class MultipartParam(object):
fileobj.seek(0, 2)
self.filesize = fileobj.tell()
fileobj.seek(0)
except:
except Exception:
raise ValueError("Could not determine filesize")
def __cmp__(self, other):
@ -169,9 +173,9 @@ class MultipartParam(object):
"""
return cls(paramname, filename=os.path.basename(filename),
filetype=mimetypes.guess_type(filename)[0],
filesize=os.path.getsize(filename),
fileobj=open(filename, "rb"))
filetype=mimetypes.guess_type(filename)[0],
filesize=os.path.getsize(filename),
fileobj=open(filename, "rb"))
@classmethod
def from_params(cls, params):
@ -204,7 +208,7 @@ class MultipartParam(object):
filetype = None
retval.append(cls(name=name, filename=filename,
filetype=filetype, fileobj=value))
filetype=filetype, fileobj=value))
else:
retval.append(cls(name, value))
return retval
@ -216,8 +220,8 @@ class MultipartParam(object):
headers = ["--%s" % boundary]
if self.filename:
disposition = 'form-data; name="%s"; filename="%s"' % (self.name,
to_string(self.filename))
disposition = 'form-data; name="%s"; filename="%s"' % (
self.name, to_string(self.filename))
else:
disposition = 'form-data; name="%s"' % self.name
@ -267,8 +271,8 @@ class MultipartParam(object):
self.cb(self, current, total)
last_block = to_bytearray("")
encoded_boundary = "--%s" % encode_and_quote(boundary)
boundary_exp = re.compile(to_bytes("^%s$" % re.escape(encoded_boundary)),
re.M)
boundary_exp = re.compile(
to_bytes("^%s$" % re.escape(encoded_boundary)), re.M)
while True:
block = self.fileobj.read(blocksize)
if not block:
@ -296,6 +300,7 @@ class MultipartParam(object):
return len(self.encode_hdr(boundary)) + 2 + valuesize
def encode_string(boundary, name, value):
"""Returns ``name`` and ``value`` encoded as a multipart/form-data
variable. ``boundary`` is the boundary string used throughout
@ -303,8 +308,8 @@ def encode_string(boundary, name, value):
return MultipartParam(name, value).encode(boundary)
def encode_file_header(boundary, paramname, filesize, filename=None,
filetype=None):
def encode_file_header(boundary, paramname, filesize, filename=None, filetype=None):
"""Returns the leading data for a multipart/form-data field that contains
file data.
@ -324,7 +329,8 @@ def encode_file_header(boundary, paramname, filesize, filename=None,
"""
return MultipartParam(paramname, filesize=filesize, filename=filename,
filetype=filetype).encode_hdr(boundary)
filetype=filetype).encode_hdr(boundary)
def get_body_size(params, boundary):
"""Returns the number of bytes that the multipart/form-data encoding
@ -332,6 +338,7 @@ def get_body_size(params, boundary):
size = sum(p.get_size(boundary) for p in MultipartParam.from_params(params))
return size + len(boundary) + 6
def get_headers(params, boundary):
"""Returns a dictionary with Content-Type and Content-Length headers
for the multipart/form-data encoding of ``params``."""
@ -341,6 +348,7 @@ def get_headers(params, boundary):
headers['Content-Length'] = str(get_body_size(params, boundary))
return headers
class multipart_yielder:
def __init__(self, params, boundary, cb):
self.params = params
@ -396,6 +404,7 @@ class multipart_yielder:
for param in self.params:
param.reset()
def multipart_encode(params, boundary=None, cb=None):
"""Encode ``params`` as multipart/form-data.