Add onceoff job for uploads migration of column extension. Simplify filetype search and related rspec tests.

This commit is contained in:
Jakub Macina 2017-07-06 19:11:32 +02:00
parent 8c445e9f17
commit 677267ae78
4 changed files with 21 additions and 12 deletions

View File

@ -0,0 +1,11 @@
module Jobs
class MigrateUploadExtensions < Jobs::Onceoff
def execute_onceoff(args)
Upload.find_each do |upload|
upload.extension = File.extname(upload.original_filename)[1..10]
upload.save
end
end
end
end

View File

@ -95,7 +95,7 @@ module FileStore
end
def get_path_for_upload(upload)
get_path_for("original".freeze, upload.id, upload.sha1, File.extname(upload.original_filename))
get_path_for("original".freeze, upload.id, upload.sha1, "."+upload.extension)
end
def get_path_for_optimized_image(optimized_image)

View File

@ -457,16 +457,16 @@ class Search
end
advanced_filter(/filetypes?:([a-zA-Z0-9,\-_]+)/) do |posts, match|
file_extensions = match.split(",")
file_extensions = match.split(",").map(&:downcase)
posts.where("posts.id IN (
SELECT post_id FROM topic_links
WHERE extension IN (?)
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)
WHERE lower(uploads.extension) IN (:file_extensions)
)", {file_extensions: file_extensions})
end
private

View File

@ -705,21 +705,19 @@ describe Search do
end
it "can find posts which contains filetypes" do
# Must be posts with real images
post1 = Fabricate(:post,
raw: "https://www.discourse.org/a/img/favicon.png")
raw: "http://example.com/image.png")
post2 = Fabricate(:post,
raw: "Discourse logo\n"\
"https://www.discourse.org/a/img/favicon.png\n"\
"https://www.discourse.org/a/img/trust-1x.jpg")
post_with_upload = Fabricate(:post)
post_with_upload.uploads = [Fabricate(:upload)]
"http://example.com/logo.png\n"\
"http://example.com/vector_image.svg")
post_with_upload = Fabricate(:post, uploads: [Fabricate(:upload)])
Fabricate(:post)
TopicLink.extract_from(post1)
TopicLink.extract_from(post2)
expect(Search.execute('filetype:jpg').posts.map(&:id)).to eq([post2.id])
expect(Search.execute('filetype:svg').posts).to eq([post2])
expect(Search.execute('filetype:png').posts.map(&:id)).to contain_exactly(post1.id, post2.id, post_with_upload.id)
expect(Search.execute('logo filetype:png').posts.map(&:id)).to eq([post2.id])
end