From 770c1faeb1b66d5a821cfb5e83358b4e83de4fab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Thu, 13 Jun 2013 01:43:50 +0200 Subject: [PATCH] added a reverse index of user uploads + rake task --- app/models/post.rb | 3 +- app/models/upload.rb | 2 ++ ...612200846_create_post_upload_join_table.rb | 12 +++++++ lib/tasks/images.rake | 36 +++++++++++++++++++ spec/models/post_spec.rb | 2 ++ spec/models/upload_spec.rb | 4 ++- 6 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20130612200846_create_post_upload_join_table.rb diff --git a/app/models/post.rb b/app/models/post.rb index 08991e6eb80..c180d3a10a8 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -17,7 +17,6 @@ class Post < ActiveRecord::Base rate_limit - belongs_to :user belongs_to :topic, counter_cache: :posts_count belongs_to :reply_to_user, class_name: "User" @@ -26,6 +25,8 @@ class Post < ActiveRecord::Base has_many :replies, through: :post_replies has_many :post_actions + has_and_belongs_to_many :upload + has_one :post_search_data validates_presence_of :raw, :user_id, :topic_id diff --git a/app/models/upload.rb b/app/models/upload.rb index ebcde0bc6ec..ca3674552ce 100644 --- a/app/models/upload.rb +++ b/app/models/upload.rb @@ -7,6 +7,8 @@ class Upload < ActiveRecord::Base belongs_to :user belongs_to :topic + has_and_belongs_to_many :post + validates_presence_of :filesize validates_presence_of :original_filename diff --git a/db/migrate/20130612200846_create_post_upload_join_table.rb b/db/migrate/20130612200846_create_post_upload_join_table.rb new file mode 100644 index 00000000000..5af4fe5b3d7 --- /dev/null +++ b/db/migrate/20130612200846_create_post_upload_join_table.rb @@ -0,0 +1,12 @@ +class CreatePostUploadJoinTable < ActiveRecord::Migration + def change + create_table :posts_uploads, id: false do |t| + t.integer :post_id + t.integer :upload_id + end + + add_index :posts_uploads, :post_id + add_index :posts_uploads, :upload_id + add_index :posts_uploads, [:post_id, :upload_id], unique: true + end +end diff --git a/lib/tasks/images.rake b/lib/tasks/images.rake index cdcb2b78da4..f41185e4548 100644 --- a/lib/tasks/images.rake +++ b/lib/tasks/images.rake @@ -10,3 +10,39 @@ task "images:compress" => :environment do end end +desc "updates reverse index of image uploads" +task "images:reindex" => :environment do + RailsMultisite::ConnectionManagement.each_connection do |db| + puts "Reindexing #{db}" + Post.select([:id, :cooked]).find_each do |p| + doc = Nokogiri::HTML::fragment(p.cooked) + doc.search("img").each do |img| + src = img['src'] + if src.present? && has_been_uploaded?(src) && m = uploaded_regex.match(src) + begin + Post.exec_sql("INSERT INTO posts_uploads (post_id, upload_id) VALUES (?, ?)", p.id, m[:upload_id]) + rescue ActiveRecord::RecordNotUnique + end + end + end + putc "." + end + end + puts "\ndone." +end + +def uploaded_regex + /\/uploads\/#{RailsMultisite::ConnectionManagement.current_db}\/(?\d+)\/[0-9a-f]{16}\.(png|jpg|jpeg|gif|tif|tiff|bmp)/ +end + +def has_been_uploaded?(url) + url =~ /^\/[^\/]/ || url.start_with?(base_url) || (asset_host.present? && url.start_with?(asset_host)) +end + +def base_url + asset_host.present? ? asset_host : Discourse.base_url_no_prefix +end + +def asset_host + ActionController::Base.asset_host +end diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index 7b2b573919f..2890aff3109 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -26,6 +26,8 @@ describe Post do it { should have_many :post_replies } it { should have_many :replies } + it { should have_and_belong_to_many :upload } + it { should rate_limit } let(:topic) { Fabricate(:topic) } diff --git a/spec/models/upload_spec.rb b/spec/models/upload_spec.rb index 2e53f69fdf4..50a88681bbc 100644 --- a/spec/models/upload_spec.rb +++ b/spec/models/upload_spec.rb @@ -5,6 +5,8 @@ describe Upload do it { should belong_to :user } it { should belong_to :topic } + it { should have_and_belong_to_many :post } + it { should validate_presence_of :original_filename } it { should validate_presence_of :filesize } @@ -38,7 +40,7 @@ describe Upload do end context "s3" do - before(:each) do + before(:each) do SiteSetting.stubs(:enable_s3_uploads?).returns(true) S3.stubs(:store_file).returns(url) end