# HG changeset patch # User Chris Cannam # Date 1350466968 -3600 # Node ID 19884e9d5effa63f115b9004951e409c0d3aa882 # Parent 35782bf2eb588cc1dfd8a62b2df22aadca38c172 Only count downloads from non-bots diff -r 35782bf2eb58 -r 19884e9d5eff app/controllers/attachments_controller.rb --- a/app/controllers/attachments_controller.rb Tue Oct 16 15:34:00 2012 +0100 +++ b/app/controllers/attachments_controller.rb Wed Oct 17 10:42:48 2012 +0100 @@ -17,6 +17,9 @@ class AttachmentsController < ApplicationController + include AttachmentsHelper + helper :attachments + before_filter :find_project before_filter :file_readable, :read_authorize, :except => :destroy before_filter :delete_authorize, :only => :destroy @@ -50,8 +53,10 @@ def download # cc: formerly this happened only if "@attachment.container.is_a?(Version)" - # or Project. Not good for us, we want to tally all downloads - @attachment.increment_download + # or Project. Not good for us, we want to tally all downloads [by humans] + if not user_is_search_bot? + @attachment.increment_download + end # images are sent inline send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename), diff -r 35782bf2eb58 -r 19884e9d5eff app/helpers/attachments_helper.rb --- a/app/helpers/attachments_helper.rb Tue Oct 16 15:34:00 2012 +0100 +++ b/app/helpers/attachments_helper.rb Wed Oct 17 10:42:48 2012 +0100 @@ -42,4 +42,11 @@ api.created_on attachment.created_on end end + + # Returns true if user agent appears (approximately) to be a search + # bot or crawler + def user_is_search_bot? + agent = request.env['HTTP_USER_AGENT'] + agent and agent =~ /(bot|slurp|crawler|spider)\b/i + end end