예제 #1
0
def serveStaticResource(resource):
    """ Return a static image or resource """

    # ban MJ12BOT, it ignores robots.txt
    user_agent = request.headers.get('User-Agent')
    if user_agent and user_agent.find('MJ12BOT') != -1:
        abort(403)

    # log certain kinds of files
    if resource.endswith('.cab'):
        try:
            db = LvfsDatabase(os.environ)
            clients = LvfsDatabaseClients(db)
            clients.log(datetime.date.today(), LvfsDownloadKind.FIRMWARE)
            clients.increment(_get_client_address(),
                              os.path.basename(resource),
                              user_agent)
        except CursorError as e:
            print str(e)

    # firmware blobs are stored on S3 now
    if resource.startswith('downloads/'):
        return redirect(os.path.join(CDN_URI, resource), 301)

    # static files served locally
    return send_from_directory('static/', resource)
예제 #2
0
def serveStaticResource(resource):
    """ Return a static image or resource """

    # log certain kinds of files
    kind = None
    if resource.endswith('.cab'):
        kind = LvfsDownloadKind.FIRMWARE
    elif resource.endswith('.xml.gz.asc'):
        kind = LvfsDownloadKind.SIGNING
    elif resource.endswith('.xml.gz'):
        kind = LvfsDownloadKind.METADATA
    if kind is not None:
        try:
            filename = os.path.basename(resource)
            db = LvfsDatabase(os.environ)
            clients = LvfsDatabaseClients(db)
            clients.increment(_get_client_address(),
                              kind,
                              filename,
                              request.headers.get('User-Agent'))
        except CursorError as e:
            print str(e)

    # use apache for the static file so we can scale
    if 'OPENSHIFT_APP_DNS' in os.environ:
        if resource.startswith('download/'):

            # if the file does not exist get it from the database
            # (which means we can scale on OpenShift)
            if not os.path.exists(resource):
                db = LvfsDatabase(os.environ)
                db_cache = LvfsDatabaseCache(db)
                db_cache.to_file(resource)

            uri = "https://%s/static/%s" % (os.environ['OPENSHIFT_APP_DNS'], resource)
            return redirect(uri, 301)

    return send_from_directory('static/', resource)