mirror of
https://github.com/discourse/discourse.git
synced 2025-09-07 12:02:53 +08:00
Add onceoff job for uploads migration of column extension. Simplify filetype search and related rspec tests.
This commit is contained in:
parent
8c445e9f17
commit
677267ae78
4 changed files with 21 additions and 12 deletions
11
app/jobs/onceoff/migrate_upload_extensions.rb
Normal file
11
app/jobs/onceoff/migrate_upload_extensions.rb
Normal 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
|
|
@ -95,7 +95,7 @@ module FileStore
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_path_for_upload(upload)
|
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
|
end
|
||||||
|
|
||||||
def get_path_for_optimized_image(optimized_image)
|
def get_path_for_optimized_image(optimized_image)
|
||||||
|
|
|
@ -457,16 +457,16 @@ class Search
|
||||||
end
|
end
|
||||||
|
|
||||||
advanced_filter(/filetypes?:([a-zA-Z0-9,\-_]+)/) do |posts, match|
|
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 (
|
posts.where("posts.id IN (
|
||||||
SELECT post_id FROM topic_links
|
SELECT post_id FROM topic_links
|
||||||
WHERE extension IN (?)
|
WHERE extension IN (:file_extensions)
|
||||||
UNION
|
UNION
|
||||||
SELECT post_uploads.post_id FROM uploads
|
SELECT post_uploads.post_id FROM uploads
|
||||||
JOIN post_uploads ON post_uploads.upload_id = uploads.id
|
JOIN post_uploads ON post_uploads.upload_id = uploads.id
|
||||||
WHERE lower(uploads.extension) IN (?)
|
WHERE lower(uploads.extension) IN (:file_extensions)
|
||||||
)", file_extensions, file_extensions)
|
)", {file_extensions: file_extensions})
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
|
@ -705,21 +705,19 @@ describe Search do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "can find posts which contains filetypes" do
|
it "can find posts which contains filetypes" do
|
||||||
# Must be posts with real images
|
|
||||||
post1 = Fabricate(:post,
|
post1 = Fabricate(:post,
|
||||||
raw: "https://www.discourse.org/a/img/favicon.png")
|
raw: "http://example.com/image.png")
|
||||||
post2 = Fabricate(:post,
|
post2 = Fabricate(:post,
|
||||||
raw: "Discourse logo\n"\
|
raw: "Discourse logo\n"\
|
||||||
"https://www.discourse.org/a/img/favicon.png\n"\
|
"http://example.com/logo.png\n"\
|
||||||
"https://www.discourse.org/a/img/trust-1x.jpg")
|
"http://example.com/vector_image.svg")
|
||||||
post_with_upload = Fabricate(:post)
|
post_with_upload = Fabricate(:post, uploads: [Fabricate(:upload)])
|
||||||
post_with_upload.uploads = [Fabricate(:upload)]
|
|
||||||
Fabricate(:post)
|
Fabricate(:post)
|
||||||
|
|
||||||
TopicLink.extract_from(post1)
|
TopicLink.extract_from(post1)
|
||||||
TopicLink.extract_from(post2)
|
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('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])
|
expect(Search.execute('logo filetype:png').posts.map(&:id)).to eq([post2.id])
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue