From 8c445e9f17f6ba51a5698a3e948ad46d339615ee Mon Sep 17 00:00:00 2001 From: Jakub Macina Date: Tue, 4 Jul 2017 17:50:08 +0200 Subject: [PATCH] Fix backend code for searching by a filetype as a combination of uploads and topic links. Add rspec test for extracting file extension in upload. --- app/models/upload.rb | 4 ---- lib/file_store/base_store.rb | 2 +- lib/search.rb | 6 +++++- spec/models/upload_spec.rb | 5 +++++ 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/app/models/upload.rb b/app/models/upload.rb index d6e1916c720..f0eae0896ba 100644 --- a/app/models/upload.rb +++ b/app/models/upload.rb @@ -52,10 +52,6 @@ class Upload < ActiveRecord::Base end end - def extension - File.extname(original_filename) - end - def self.generate_digest(path) Digest::SHA1.file(path).hexdigest end diff --git a/lib/file_store/base_store.rb b/lib/file_store/base_store.rb index b79282fe681..b4c8bfe47c6 100644 --- a/lib/file_store/base_store.rb +++ b/lib/file_store/base_store.rb @@ -95,7 +95,7 @@ module FileStore end def get_path_for_upload(upload) - get_path_for("original".freeze, upload.id, upload.sha1, upload.extension) + get_path_for("original".freeze, upload.id, upload.sha1, File.extname(upload.original_filename)) end def get_path_for_optimized_image(optimized_image) diff --git a/lib/search.rb b/lib/search.rb index f3ea5ccd824..159de71b8d0 100644 --- a/lib/search.rb +++ b/lib/search.rb @@ -462,7 +462,11 @@ class Search posts.where("posts.id IN ( SELECT post_id FROM topic_links WHERE extension IN (?) - )", file_extensions) + UNION + SELECT post_uploads.post_id FROM uploads + JOIN post_uploads ON post_uploads.upload_id = uploads.id + WHERE lower(uploads.extension) IN (?) + )", file_extensions, file_extensions) end private diff --git a/spec/models/upload_spec.rb b/spec/models/upload_spec.rb index c187ddbe675..ceef5bf669b 100644 --- a/spec/models/upload_spec.rb +++ b/spec/models/upload_spec.rb @@ -46,6 +46,11 @@ describe Upload do end + it "extracts file extension" do + created_upload = UploadCreator.new(image, image_filename).create_for(user_id) + expect(created_upload.extension).to eq("png") + end + context ".get_from_url" do let(:url) { "/uploads/default/original/3X/1/0/10f73034616a796dfd70177dc54b6def44c4ba6f.png" } let(:upload) { Fabricate(:upload, url: url) }