Beispiel #1
0
def _load_metadata(collection):
    """Unpack the common, pre-defined metadata for all the uploaded files."""
    try:
        meta = json.loads(request.form.get('meta', '{}'))
    except Exception as ex:
        raise BadRequest(unicode(ex))

    validate_data(meta, DocumentCreateSchema)
    foreign_id = stringify(meta.get('foreign_id'))

    # If a foreign_id is specified, we cannot upload multiple files at once.
    if len(request.files) > 1 and foreign_id is not None:
        raise BadRequest(response=jsonify(
            {
                'status': 'error',
                'message': 'Multiple files with one foreign_id'
            },
            status=400))

    if not len(request.files) and foreign_id is None:
        raise BadRequest(response=jsonify(
            {
                'status': 'error',
                'message': 'Directories need to have a foreign_id'
            },
            status=400))
    return meta, foreign_id
Beispiel #2
0
def ingest_upload(id):
    collection = obj_or_404(Collection.by_id(id))
    require(request.authz.can_write(collection.id))

    try:
        meta = json.loads(request.form.get('meta', '{}'))
    except Exception as ex:
        raise BadRequest(unicode(ex))
    validate_data(meta, DocumentSchema)

    documents = []
    for storage in request.files.values():
        sec_fn = os.path.join(upload_folder, secure_filename(storage.filename))
        storage.save(sec_fn)
        content_hash = checksum(sec_fn)
        document = Document.by_keys(collection=collection,
                                    content_hash=content_hash)
        document.mime_type = storage.mimetype
        document.file_name = storage.filename
        document.update(meta)
        ingest_document(document, sec_fn, role_id=request.authz.id)
        os.unlink(sec_fn)
        documents.append(document)

    return jsonify({
        'status':
        'ok',
        'documents': [DocumentSchema().dump(d).data for d in documents]
    })
Beispiel #3
0
def _load_metadata():
    """Unpack the common, pre-defined metadata for all the uploaded files."""
    try:
        meta = json.loads(request.form.get('meta', '{}'))
    except Exception as ex:
        raise BadRequest(str(ex))

    validate_data(meta, DocumentCreateSchema)
    foreign_id = stringify(meta.get('foreign_id'))
    if not len(request.files) and foreign_id is None:
        raise BadRequest(response=jsonify({
            'status': 'error',
            'message': 'Directories need to have a foreign_id'
        }, status=400))
    return meta, foreign_id
Beispiel #4
0
def _load_metadata():
    """Unpack the common, pre-defined metadata for all the uploaded files."""
    try:
        meta = json.loads(request.form.get('meta', '{}'))
    except Exception as ex:
        raise BadRequest(str(ex))

    validate_data(meta, DocumentCreateSchema)
    foreign_id = stringify(meta.get('foreign_id'))
    if not len(request.files) and foreign_id is None:
        raise BadRequest(response=jsonify({
            'status': 'error',
            'message': 'Directories need to have a foreign_id'
        }, status=400))
    return meta, foreign_id