Serve publications by UUID.

def fetch_by_uuid(uuid):
    """
    Serve publications by UUID.
    """
    # fetch all - private and public - publications
    all_pubs = [
        pub
        for pub in search_publications(DBPublication(uuid=uuid))
    ]

    if not all_pubs:
        abort(404, "Dokument s UUID '%s' není dostupný." % (uuid))

    public_pubs = [
        pub
        for pub in all_pubs
        if pub.is_public
    ]

    if not public_pubs:
        name = all_pubs[0].title
        raise HTTPError(
            403,
            body="Forbidden!",
            traceback={"name": name.decode("utf-8"), "uuid": uuid}
        )

    if len(public_pubs) > 1:
        abort(500, "Inkonzistence databáze - vráceno vícero UUID.")

    pub = public_pubs[0]

    if settings.PUBLIC_DIR not in pub.file_pointer:
        abort(500, "Pokus o neoprávněný přístup!")

    down_fn = os.path.basename(pub.filename)
    local_fn = pub.file_pointer.replace(settings.PUBLIC_DIR, "")

    mime_type = mime.Types.of(down_fn)[0].content_type
    if mime_type == "inode/symlink":
        mime_type = "auto"

    response = static_file(
        local_fn,
        root=settings.PUBLIC_DIR,
        download=down_fn,
        mimetype=mime_type,
    )

    # Bottle doesn't allow you to specify custom filename AND to say, that you
    # want to have this file for view in browser instead of download. This
    # happens because it uses `download` parameter as both bool flag and
    # alternative filename. - this hack allows it.
    if mime_type == "application/pdf":
        disposition = 'inline; filename="%s"' % down_fn
        response.headers[str("Content-Disposition")] = str(disposition)

    return response