changeset 967:19884e9d5eff bug_521

Only count downloads from non-bots
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Wed, 17 Oct 2012 10:42:48 +0100
parents 35782bf2eb58
children 6b978669cdac 492ff72268e3
files app/controllers/attachments_controller.rb app/helpers/attachments_helper.rb
diffstat 2 files changed, 14 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- 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),
--- 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