mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FEATURE: Create upload_references table (#16146)
This table holds associations between uploads and other models. This can be used to prevent removing uploads that are still in use. * DEV: Create upload_references * DEV: Use UploadReference instead of PostUpload * DEV: Use UploadReference for SiteSetting * DEV: Use UploadReference for Badge * DEV: Use UploadReference for Category * DEV: Use UploadReference for CustomEmoji * DEV: Use UploadReference for Group * DEV: Use UploadReference for ThemeField * DEV: Use UploadReference for ThemeSetting * DEV: Use UploadReference for User * DEV: Use UploadReference for UserAvatar * DEV: Use UploadReference for UserExport * DEV: Use UploadReference for UserProfile * DEV: Add method to extract uploads from raw text * DEV: Use UploadReference for Draft * DEV: Use UploadReference for ReviewableQueuedPost * DEV: Use UploadReference for UserProfile's bio_raw * DEV: Do not copy user uploads to upload references * DEV: Copy post uploads again after deploy * DEV: Use created_at and updated_at from uploads table * FIX: Check if upload site setting is empty * DEV: Copy user uploads to upload references * DEV: Make upload extraction less strict
This commit is contained in:
@@ -20,13 +20,11 @@ class Upload < ActiveRecord::Base
|
||||
Post.unscoped { super }
|
||||
end
|
||||
|
||||
has_many :post_uploads, dependent: :destroy
|
||||
has_many :posts, through: :post_uploads
|
||||
|
||||
has_many :post_hotlinked_media, dependent: :destroy, class_name: "PostHotlinkedMedia"
|
||||
|
||||
has_many :optimized_images, dependent: :destroy
|
||||
has_many :user_uploads, dependent: :destroy
|
||||
has_many :upload_references, dependent: :destroy
|
||||
has_many :posts, through: :upload_references, source: :target, source_type: 'Post'
|
||||
has_many :topic_thumbnails
|
||||
|
||||
attr_accessor :for_group_message
|
||||
@@ -87,43 +85,9 @@ class Upload < ActiveRecord::Base
|
||||
end
|
||||
|
||||
def self.with_no_non_post_relations
|
||||
scope = self
|
||||
.joins(<<~SQL)
|
||||
LEFT JOIN site_settings ss
|
||||
ON NULLIF(ss.value, '')::integer = uploads.id
|
||||
AND ss.data_type = #{SiteSettings::TypeSupervisor.types[:upload].to_i}
|
||||
SQL
|
||||
.where("ss.value IS NULL")
|
||||
.joins("LEFT JOIN users u ON u.uploaded_avatar_id = uploads.id")
|
||||
.where("u.uploaded_avatar_id IS NULL")
|
||||
.joins("LEFT JOIN user_avatars ua ON ua.gravatar_upload_id = uploads.id OR ua.custom_upload_id = uploads.id")
|
||||
.where("ua.gravatar_upload_id IS NULL AND ua.custom_upload_id IS NULL")
|
||||
.joins("LEFT JOIN user_profiles up ON up.profile_background_upload_id = uploads.id OR up.card_background_upload_id = uploads.id")
|
||||
.where("up.profile_background_upload_id IS NULL AND up.card_background_upload_id IS NULL")
|
||||
.joins("LEFT JOIN categories c ON c.uploaded_logo_id = uploads.id OR c.uploaded_background_id = uploads.id")
|
||||
.where("c.uploaded_logo_id IS NULL AND c.uploaded_background_id IS NULL")
|
||||
.joins("LEFT JOIN custom_emojis ce ON ce.upload_id = uploads.id")
|
||||
.where("ce.upload_id IS NULL")
|
||||
.joins("LEFT JOIN theme_fields tf ON tf.upload_id = uploads.id")
|
||||
.where("tf.upload_id IS NULL")
|
||||
.joins("LEFT JOIN user_exports ue ON ue.upload_id = uploads.id")
|
||||
.where("ue.upload_id IS NULL")
|
||||
.joins("LEFT JOIN groups g ON g.flair_upload_id = uploads.id")
|
||||
.where("g.flair_upload_id IS NULL")
|
||||
.joins("LEFT JOIN badges b ON b.image_upload_id = uploads.id")
|
||||
.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?
|
||||
scope = scope.where.not(id: SiteSetting.selectable_avatars.map(&:id))
|
||||
end
|
||||
|
||||
scope
|
||||
self
|
||||
.joins("LEFT JOIN upload_references ur ON ur.upload_id = uploads.id AND ur.target_type != 'Post'")
|
||||
.where("ur.upload_id IS NULL")
|
||||
end
|
||||
|
||||
def to_s
|
||||
@@ -542,6 +506,22 @@ class Upload < ActiveRecord::Base
|
||||
problems
|
||||
end
|
||||
|
||||
def self.extract_upload_ids(raw)
|
||||
return [] if raw.blank?
|
||||
|
||||
sha1s = []
|
||||
|
||||
raw.scan(/\/(\h{40})/).each do |match|
|
||||
sha1s << match[0]
|
||||
end
|
||||
|
||||
raw.scan(/\/([a-zA-Z0-9]{27})/).each do |match|
|
||||
sha1s << Upload.sha1_from_base62_encoded(match[0])
|
||||
end
|
||||
|
||||
Upload.where(sha1: sha1s.uniq).pluck(:id)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def short_url_basename
|
||||
|
||||
Reference in New Issue
Block a user