예제 #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)