mirror of
https://github.com/discourse/discourse.git
synced 2025-01-19 06:52:46 +08:00
Add RECOVER_FROM_S3
to uploads:list_posts_with_broken_images
rake task.
This commit is contained in:
parent
04d26c65e2
commit
8496537590
|
@ -9,6 +9,8 @@ module FileStore
|
|||
class S3Store < BaseStore
|
||||
TOMBSTONE_PREFIX ||= "tombstone/"
|
||||
|
||||
attr_reader :s3_helper
|
||||
|
||||
def initialize(s3_helper = nil)
|
||||
@s3_helper = s3_helper || S3Helper.new(s3_bucket, TOMBSTONE_PREFIX)
|
||||
end
|
||||
|
|
|
@ -718,15 +718,17 @@ end
|
|||
|
||||
task "uploads:list_posts_with_broken_images" => :environment do
|
||||
if ENV["RAILS_DB"]
|
||||
list_broken_posts
|
||||
list_broken_posts(recover_from_s3: ENV["RECOVER_MISSING"])
|
||||
else
|
||||
RailsMultisite::ConnectionManagement.each_connection do |db|
|
||||
list_broken_posts
|
||||
list_broken_posts(recover_from_s3: ENV["RECOVER_MISSING"])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def list_broken_posts
|
||||
def list_broken_posts(recover_from_s3: false)
|
||||
object_keys = nil
|
||||
|
||||
Post.where("raw LIKE '%upload:\/\/%'").find_each do |post|
|
||||
begin
|
||||
begin
|
||||
|
@ -743,6 +745,22 @@ def list_broken_posts
|
|||
|
||||
if img["data-orig-src"]
|
||||
puts "#{post.full_url} #{img["data-orig-src"]}"
|
||||
|
||||
if recover_from_s3 && Discourse.store.external?
|
||||
object_keys ||= begin
|
||||
s3_helper = Discourse.store.s3_helper
|
||||
|
||||
s3_helper.list("original").map(&:key).concat(
|
||||
s3_helper.list("#{FileStore::S3Store::TOMBSTONE_PREFIX}original").map(&:key)
|
||||
)
|
||||
end
|
||||
|
||||
recover_from_s3_by_sha1(
|
||||
post: post,
|
||||
sha1: Upload.sha1_from_short_url(img["data-orig-src"]),
|
||||
object_keys: object_keys
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
rescue => e
|
||||
|
@ -750,3 +768,28 @@ def list_broken_posts
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
def recover_from_s3_by_sha1(post:, sha1:, object_keys: [])
|
||||
object_keys.each do |key|
|
||||
if key =~ /#{sha1}/
|
||||
url = "https:#{SiteSetting.Upload.absolute_base_url}/#{key}"
|
||||
|
||||
begin
|
||||
tmp = FileHelper.download(
|
||||
url,
|
||||
max_file_size: SiteSetting.max_image_size_kb.kilobytes,
|
||||
tmp_file_name: "recover_from_s3"
|
||||
)
|
||||
|
||||
upload = UploadCreator.new(
|
||||
tmp,
|
||||
File.basename(key)
|
||||
).create_for(post.user_id)
|
||||
|
||||
post.rebake! if upload.persisted?
|
||||
ensure
|
||||
tmp&.close
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue
Block a user