DEV: use upload id to save in theme setting instead of URL. (#14341)

When we use URL instead it creates the problem while changing the CDN hostname.
This commit is contained in:
Vinoth Kannan 2021-09-16 07:58:53 +05:30 committed by GitHub
parent 91453dd3fc
commit a6de4a5ce9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 89 additions and 6 deletions

View File

@ -38,7 +38,6 @@ module Jobs
encoded_sha = Base62.encode(upload.sha1.hex) encoded_sha = Base62.encode(upload.sha1.hex)
next if ReviewableQueuedPost.pending.where("payload->>'raw' LIKE '%#{upload.sha1}%' OR payload->>'raw' LIKE '%#{encoded_sha}%'").exists? next if ReviewableQueuedPost.pending.where("payload->>'raw' LIKE '%#{upload.sha1}%' OR payload->>'raw' LIKE '%#{encoded_sha}%'").exists?
next if Draft.where("data LIKE '%#{upload.sha1}%' OR data LIKE '%#{encoded_sha}%'").exists? next if Draft.where("data LIKE '%#{upload.sha1}%' OR data LIKE '%#{encoded_sha}%'").exists?
next if ThemeSetting.where(data_type: ThemeSetting.types[:upload]).where("value LIKE ?", "%#{upload.sha1}%").exists?
if defined?(ChatMessage) && if defined?(ChatMessage) &&
ChatMessage.where("message LIKE ? OR message LIKE ?", "%#{upload.sha1}%", "%#{encoded_sha}%").exists? ChatMessage.where("message LIKE ? OR message LIKE ?", "%#{upload.sha1}%", "%#{encoded_sha}%").exists?
next next

View File

@ -90,6 +90,12 @@ class Upload < ActiveRecord::Base
.where("g.flair_upload_id IS NULL") .where("g.flair_upload_id IS NULL")
.joins("LEFT JOIN badges b ON b.image_upload_id = uploads.id") .joins("LEFT JOIN badges b ON b.image_upload_id = uploads.id")
.where("b.image_upload_id IS NULL") .where("b.image_upload_id IS NULL")
.joins(<<~SQL)
LEFT JOIN theme_settings ts
ON NULLIF(ts.value, '')::integer = uploads.id
AND ts.data_type = #{ThemeSetting.types[:upload].to_i}
SQL
.where("ts.value IS NULL")
if SiteSetting.selectable_avatars.present? if SiteSetting.selectable_avatars.present?
scope = scope.where.not(id: SiteSetting.selectable_avatars.map(&:id)) scope = scope.where.not(id: SiteSetting.selectable_avatars.map(&:id))

View File

@ -0,0 +1,19 @@
# frozen_string_literal: true
class UpdateValueOnThemeSettingForUploadType < ActiveRecord::Migration[6.1]
def up
execute <<~SQL
UPDATE theme_settings
SET value = (SELECT id FROM uploads WHERE uploads.url = theme_settings.value)
WHERE data_type = 6
SQL
end
def down
execute <<~SQL
UPDATE theme_settings
SET value = (SELECT url FROM uploads WHERE uploads.id = theme_settings.value)
WHERE data_type = 6
SQL
end
end

View File

@ -22,7 +22,7 @@ class ThemeSettingsManager
end end
def value def value
has_record? ? db_record.value : @default has_record? ? db_record.value : default
end end
def type_name def type_name
@ -174,9 +174,48 @@ class ThemeSettingsManager
class Upload < self class Upload < self
def value def value
val = super cdn_url(super)
Discourse.store.cdn_url(val)
end end
def default
upload_id = default_upload_id
return if upload_id.blank?
cdn_url(upload_id)
end
def value=(new_value)
if new_value.present?
if new_value == default
new_value = default_upload_id
else
upload = ::Upload.find_by(url: new_value)
new_value = upload.id if upload.present?
end
end
super(new_value)
end
private
def cdn_url(upload_id)
return if upload_id.blank?
upload = ::Upload.find_by_id(upload_id.to_i)
return if upload.blank?
Discourse.store.cdn_url(upload.url)
end
def default_upload_id
theme_field = theme.theme_fields.find_by(
name: @default,
type_id: ThemeField.types[:theme_upload_var]
)
return if theme_field.blank?
theme_field.upload_id
end
end end
end end

View File

@ -149,4 +149,24 @@ describe ThemeSettingsManager do
expect(list_setting.list_type).to eq("compact") expect(list_setting.list_type).to eq("compact")
end end
end end
context "Upload" do
let!(:upload) { Fabricate(:upload) }
it "saves the upload id" do
upload_setting = find_by_name(:upload_setting)
upload_setting.value = upload.url
theme.reload
expect(ThemeSetting.exists?(theme_id: theme.id, name: "upload_setting", value: upload.id.to_s)).to be_truthy
end
it "returns the CDN URL" do
upload_setting = find_by_name(:upload_setting)
upload_setting.value = upload.url
theme.reload
expect(upload_setting.value).to eq(Discourse.store.cdn_url(upload.url))
end
end
end end

View File

@ -288,7 +288,7 @@ describe Jobs::CleanUpUploads do
it "does not delete theme setting uploads" do it "does not delete theme setting uploads" do
theme = Fabricate(:theme) theme = Fabricate(:theme)
theme_upload = fabricate_upload theme_upload = fabricate_upload
ThemeSetting.create!(theme: theme, data_type: ThemeSetting.types[:upload], value: theme_upload.url, name: "my_setting_name") ThemeSetting.create!(theme: theme, data_type: ThemeSetting.types[:upload], value: theme_upload.id.to_s, name: "my_setting_name")
Jobs::CleanUpUploads.new.execute(nil) Jobs::CleanUpUploads.new.execute(nil)

View File

@ -542,7 +542,7 @@ HTML
default: "" default: ""
YAML YAML
ThemeSetting.create!(theme: theme, data_type: ThemeSetting.types[:upload], value: upload.url, name: "my_upload") ThemeSetting.create!(theme: theme, data_type: ThemeSetting.types[:upload], value: upload.id.to_s, name: "my_upload")
theme.save! theme.save!
json = JSON.parse(cached_settings(theme.id)) json = JSON.parse(cached_settings(theme.id))