def download_document(service_id, document_id):
    if 'key' not in request.args:
        return jsonify(error='Missing decryption key'), 400

    filename = request.args.get('filename')
    sending_method = request.args.get('sending_method')

    try:
        key = base64_to_bytes(request.args['key'])
    except ValueError:
        return jsonify(error='Invalid decryption key'), 400

    try:
        document = document_store.get(service_id, document_id, key,
                                      sending_method)
    except DocumentStoreError as e:
        current_app.logger.info('Failed to download document: {}'.format(e),
                                extra={
                                    'service_id': service_id,
                                    'document_id': document_id,
                                })
        return jsonify(error=str(e)), 400

    response = make_response(
        send_file(
            document['body'],
            mimetype=document['mimetype'],
            # as_attachment can only be `True` if the filename is set
            as_attachment=(filename is not None),
            attachment_filename=filename,
        ))
    response.headers['Content-Length'] = document['size']
    response.headers['X-Robots-Tag'] = 'noindex, nofollow'

    return response
def get_document_metadata(service_id, document_id):
    if 'key' not in request.args:
        return jsonify(error='Missing decryption key'), 400

    try:
        key = base64_to_bytes(request.args['key'])
    except ValueError:
        return jsonify(error='Invalid decryption key'), 400

    try:
        metadata = document_store.get_document_metadata(
            service_id, document_id, key)
    except DocumentStoreError as e:
        current_app.logger.warning(
            'Failed to get document metadata: {}'.format(e),
            extra={
                'service_id': service_id,
                'document_id': document_id,
            })
        return jsonify(error=str(e)), 400

    if metadata:
        document = {
            'direct_file_url':
            get_direct_file_url(
                service_id=service_id,
                document_id=document_id,
                key=key,
                mimetype=metadata['mimetype'],
            )
        }
    else:
        document = None

    response = make_response({
        'file_exists': str(bool(metadata)),
        'document': document,
    })

    response.headers['X-Robots-Tag'] = 'noindex, nofollow'
    return response
Example #3
0
def download_document(service_id, document_id, extension=None):
    if 'key' not in request.args:
        return jsonify(error='Missing decryption key'), 400

    try:
        key = base64_to_bytes(request.args['key'])
    except ValueError:
        return jsonify(error='Invalid decryption key'), 400

    try:
        document = document_store.get(service_id, document_id, key)
    except DocumentStoreError as e:
        current_app.logger.info('Failed to download document: {}'.format(e),
                                extra={
                                    'service_id': service_id,
                                    'document_id': document_id,
                                })
        return jsonify(error=str(e)), 400

    mimetype = document['mimetype']
    send_file_kwargs = {'mimetype': mimetype}
    extension = current_app.config['ALLOWED_FILE_TYPES'][mimetype]

    if extension in FILE_TYPES_TO_FORCE_DOWNLOAD_FOR:
        # Give CSV files the 'Content-Disposition' header to ensure they are downloaded
        # rather than shown as raw text in the users browser
        send_file_kwargs.update({
            'download_name': f'{document_id}.{extension}',
            'as_attachment': True,
        })

    response = make_response(send_file(
        document['body'],
        **send_file_kwargs,
    ))
    response.headers['Content-Length'] = document['size']
    response.headers['X-Robots-Tag'] = 'noindex, nofollow'

    return response
Example #4
0
def test_bytes_to_base64_to_bytes():
    b = os.urandom(32)
    b64 = bytes_to_base64(b)
    assert base64_to_bytes(b64) == b