Пример #1
0
def create_plain_file():
    """Create a local file using data from a ``Content-Type: multipart/form-data`` request.

    :param request.POST['filedata']: a ``cgi.FieldStorage`` object containing
        the file data.
    :param str request.POST['filename']: the name of the binary file.
    :returns: an SQLAlchemy model object representing the file.

    .. note::
    
        The validator expects ``request.POST`` to encode list input via the
        ``formencode.variabledecode.NestedVariables`` format.  E.g., a list of
        form ``id`` values would be provided as values to keys with names like
        ``'forms-0'``, ``'forms-1'``, ``'forms-2'``, etc.

    """
    values = dict(request.params)
    filedata = request.POST.get('filedata')
    if not hasattr(filedata, 'file'):
        raise InvalidFieldStorageObjectError
    if not values.get('filename'):
        values['filename'] = os.path.split(filedata.filename)[-1]
    values['filedata_first_KB'] = filedata.value[:1024]
    schema = FileCreateWithFiledataSchema()
    data = schema.to_python(values)

    file = File()
    file.filename = h.normalize(data['filename'])
    file.MIME_type = data['MIME_type']

    files_path = h.get_OLD_directory_path('files', config=config)
    file_path = os.path.join(files_path, file.filename)
    file_object, file_path = get_unique_file_path(file_path)
    file.filename = os.path.split(file_path)[-1]
    file.name = file.filename
    shutil.copyfileobj(filedata.file, file_object)
    filedata.file.close()
    file_object.close()
    file.size = os.path.getsize(file_path)

    file = add_standard_metadata(file, data)

    return file
Пример #2
0
def create_base64_file(data):
    """Create a local file using data from a ``Content-Type: application/json`` request.

    :param dict data: the data to create the file model.
    :param str data['base64_encoded_file']: Base64-encoded file data.
    :returns: an SQLAlchemy model object representing the file.

    """

    data['MIME_type'] = u''  # during validation, the schema will set a proper value based on the base64_encoded_file or filename attribute
    schema = FileCreateWithBase64EncodedFiledataSchema()
    state = h.State()
    state.full_dict = data
    state.user = session['user']
    data = schema.to_python(data, state)

    file = File()
    file.MIME_type = data['MIME_type']
    file.filename = h.normalize(data['filename'])

    file = add_standard_metadata(file, data)

    # Write the file to disk (making sure it's unique and thereby potentially)
    # modifying file.filename; and calculate file.size.
    file_data = data['base64_encoded_file']     # base64-decoded during validation
    files_path = h.get_OLD_directory_path('files', config=config)
    file_path = os.path.join(files_path, file.filename)
    file_object, file_path = get_unique_file_path(file_path)
    file.filename = os.path.split(file_path)[-1]
    file.name = file.filename
    file_object.write(file_data)
    file_object.close()
    file_data = None
    file.size = os.path.getsize(file_path)

    file = restrict_file_by_forms(file)
    return file