def validate_file(data, max_size=None, msg=_('Size must be less than %d KB')):
    # check against the first roundtrip with the client
    if data is None or isinstance(data, basestring):
        return None

    if data.done == -1:
        raise ValueError(_('Transfer was interrupted'))

    # gets the file size (it's a StringIO)
    data.file.seek(0, 2)  # 0 from EOF
    filesize = data.file.tell()
    data.file.seek(0)

    if max_size is not None and filesize > max_size * 1024:
        raise ValueError(msg % max_size)

    filedata = data.file.read()
    data.file.seek(0)

    # some browsers (i.e. Internet Explorer) send the full path of the file
    # instead of the filename
    # so, we remove the path from the filename
    filename = ntpath.basename(unicode(data.filename))

    return {'filename': filename,
            'data': filedata,
            'content_type': unicode(data.type)}
Validate a 'file' input data against the max size in KB
dataset/ETHPy150Open Net-ng/kansha/kansha/validator.py/validate_file