コード例 #1
0
ファイル: files.py プロジェクト: dativebase/old
def create_externally_hosted_file(data):
    """Create an externally hosted file.

    :param dict data: the data to create the file model.
    :param str data['url']: a valid URL where the file data are served.
    :returns: an SQLAlchemy model object representing the file.

    Optional keys of the data dictionary, not including the standard metadata
    ones, are ``name``, ``password`` and ``MIME_type``.
    
    """
    data['password'] = data.get('password') or u''
    schema = FileExternallyHostedSchema()
    data = schema.to_python(data)
    file = File()

    # User-inputted string data
    file.name = h.normalize(data['name'])
    file.password = data['password']
    file.MIME_type = data['MIME_type']
    file.url = data['url']

    file = add_standard_metadata(file, data)
    file = restrict_file_by_forms(file)
    return file
コード例 #2
0
ファイル: files.py プロジェクト: dativebase/old
def create_subinterval_referencing_file(data):
    """Create a subinterval-referencing file.

    :param dict data: the data to create the file model.
    :param int data['parent_file']: the ``id`` value of an audio/video file model.
    :param float/int data['start']: the start of the interval in seconds.
    :param float/int data['end']: the end of the interval in seconds.
    :returns: an SQLAlchemy model object representing the file.

    A value for ``data['name']`` may also be supplied.

    """
    data['name'] = data.get('name') or u''
    schema = FileSubintervalReferencingSchema()
    state = h.State()
    state.full_dict = data
    state.user = session['user']
    data = schema.to_python(data, state)

    file = File()

    # Data unique to referencing subinterval files
    file.parent_file = data['parent_file']
    file.name = h.normalize(data['name']) or file.parent_file.filename   # Name defaults to the parent file's filename if nothing provided by user
    file.start = data['start']
    file.end = data['end']
    file.MIME_type = file.parent_file.MIME_type

    file = add_standard_metadata(file, data)
    file = restrict_file_by_forms(file)

    return file
コード例 #3
0
ファイル: files.py プロジェクト: dativebase/old
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
コード例 #4
0
ファイル: files.py プロジェクト: dativebase/old
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