from mimetypes import guess_extension
from uuid import uuid4

from magic import from_buffer


class Files(object):

    def __init__(self, client):
        self.client = client

    def create(self, data, **kwargs):
        """Create a file object bound to the client."""
        return File(self.client, data, **kwargs)


class File(object):
    """Normalise urls, files, and binary data into one object.

    if mime_type is provided the data is assumed to be binary data, otherwise
    it is assumed to be a path or URL.

    if the binary data or file size exceeds the size of chunk_size it will be
    pre uploaded using the documents api.

    """

    def __init__(self, client, data, mime_type=None, chunk_size=None):
        self.client = client
        self.chunk_size = chunk_size or (1024 * 1024)
        self.headers = {}
        self.body = None
        self.mime_type = None

        if mime_type:
            self._init_binary(data, mime_type)
        elif data.startswith('http://') or data.startswith('https://'):
            self._init_url(data)
        else:
            self._init_path(data)

    def _init_url(self, data):
        """Initialise with a URL."""
        self.headers['Content-Location'] = data

    def _init_path(self, data):
        """Initialise with a file path."""
        with open(data, 'rb') as f:
            data = f.read()

        mime_type = from_buffer(data[0:1024], True)

        self._init_binary(data, mime_type)

    def _init_binary(self, data, mime_type):
        """Initialise with binary data."""
        if len(data) > self.chunk_size:
            return self._init_document(data, mime_type)

        self.mime_type = mime_type
        self.body = data

    def _init_document(self, data, mime_type):
        """Upload the data using the documents API."""
        filename = 'upload-{0}{1}'.format(uuid4(), guess_extension(mime_type))
        document = self.client.documents.create(filename, len(data))

        cursor = 0

        while cursor < len(data):
            chunk = data[cursor:cursor + self.chunk_size]

            document.upload(cursor, cursor + len(chunk) - 1, chunk)
            cursor += len(chunk)

        self._init_url(document.uri)

    def file_tuple(self):
        """Generate a file tuple for requests to upload."""
        return (None, self.body or '', self.mime_type, self.headers or None)

@@ -1545,24 +1545,85 @@
 ry data.%22%22%22%0A
+        self.mime_type = mime_type%0A        self.body = data%0A%0A
         if l
@@ -1711,67 +1711,8 @@
 e)%0A%0A
-        self.mime_type = mime_type%0A        self.body = data
 %0A%0A  
@@ -1978,16 +1978,36 @@
 rsor = 0
+%0A        counter = 1
 %0A%0A      
@@ -2092,16 +2092,71 @@
 k_size%5D%0A
+            print('SENDING CHUNK %7B%7D..'.format(counter))
 %0A       
@@ -2248,16 +2248,41 @@
 n(chunk)
+%0A            counter += 1
 %0A%0A      

Update files.py