2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-01-20 00:51:42 +08:00
|
|
|
require "db_helper"
|
2013-06-15 17:29:20 +08:00
|
|
|
require "digest/sha1"
|
2018-01-20 00:51:42 +08:00
|
|
|
require "base62"
|
2013-06-15 17:29:20 +08:00
|
|
|
|
2016-04-12 02:42:40 +08:00
|
|
|
################################################################################
|
|
|
|
# gather #
|
|
|
|
################################################################################
|
|
|
|
|
2019-11-18 09:25:42 +08:00
|
|
|
require_dependency "rake_helpers"
|
|
|
|
|
2016-04-12 02:42:40 +08:00
|
|
|
task "uploads:gather" => :environment do
|
2016-04-12 22:00:25 +08:00
|
|
|
ENV["RAILS_DB"] ? gather_uploads : gather_uploads_for_all_sites
|
|
|
|
end
|
|
|
|
|
|
|
|
def gather_uploads_for_all_sites
|
|
|
|
RailsMultisite::ConnectionManagement.each_connection { gather_uploads }
|
|
|
|
end
|
|
|
|
|
2016-04-13 22:33:00 +08:00
|
|
|
def file_exists?(path)
|
|
|
|
File.exists?(path) && File.size(path) > 0
|
|
|
|
rescue
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2016-04-12 22:00:25 +08:00
|
|
|
def gather_uploads
|
2016-04-12 02:42:40 +08:00
|
|
|
public_directory = "#{Rails.root}/public"
|
|
|
|
current_db = RailsMultisite::ConnectionManagement.current_db
|
|
|
|
|
|
|
|
puts "", "Gathering uploads for '#{current_db}'...", ""
|
|
|
|
|
2016-04-12 03:17:33 +08:00
|
|
|
Upload.where("url ~ '^\/uploads\/'")
|
2017-07-28 09:20:09 +08:00
|
|
|
.where("url !~ '^\/uploads\/#{current_db}'")
|
|
|
|
.find_each do |upload|
|
2016-04-12 02:42:40 +08:00
|
|
|
begin
|
|
|
|
old_db = upload.url[/^\/uploads\/([^\/]+)\//, 1]
|
|
|
|
from = upload.url.dup
|
|
|
|
to = upload.url.sub("/uploads/#{old_db}/", "/uploads/#{current_db}/")
|
|
|
|
source = "#{public_directory}#{from}"
|
|
|
|
destination = "#{public_directory}#{to}"
|
|
|
|
|
2016-04-13 22:33:00 +08:00
|
|
|
# create destination directory & copy file unless it already exists
|
|
|
|
unless file_exists?(destination)
|
|
|
|
`mkdir -p '#{File.dirname(destination)}'`
|
|
|
|
`cp --link '#{source}' '#{destination}'`
|
|
|
|
end
|
|
|
|
|
2016-04-12 02:42:40 +08:00
|
|
|
# ensure file has been succesfuly copied over
|
2016-04-13 22:33:00 +08:00
|
|
|
raise unless file_exists?(destination)
|
|
|
|
|
2016-04-12 02:42:40 +08:00
|
|
|
# remap links in db
|
|
|
|
DbHelper.remap(from, to)
|
|
|
|
rescue
|
|
|
|
putc "!"
|
|
|
|
else
|
|
|
|
putc "."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
puts "", "Done!"
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2015-05-25 23:59:00 +08:00
|
|
|
################################################################################
|
|
|
|
# backfill_shas #
|
|
|
|
################################################################################
|
|
|
|
|
2013-06-15 17:29:20 +08:00
|
|
|
task "uploads:backfill_shas" => :environment do
|
|
|
|
RailsMultisite::ConnectionManagement.each_connection do |db|
|
2015-06-10 23:19:58 +08:00
|
|
|
puts "Backfilling #{db}..."
|
|
|
|
Upload.where(sha1: nil).find_each do |u|
|
|
|
|
begin
|
|
|
|
path = Discourse.store.path_for(u)
|
2020-01-16 11:50:27 +08:00
|
|
|
sha1 = Upload.generate_digest(path)
|
|
|
|
u.sha1 = u.secure? ? SecureRandom.hex(20) : sha1
|
|
|
|
u.original_sha1 = u.secure? ? sha1 : nil
|
2015-06-10 23:19:58 +08:00
|
|
|
u.save!
|
2013-06-15 17:29:20 +08:00
|
|
|
putc "."
|
2016-08-23 15:05:37 +08:00
|
|
|
rescue => e
|
2016-08-29 10:30:10 +08:00
|
|
|
puts "Skipping #{u.original_filename} (#{u.url}) #{e.message}"
|
2013-06-15 17:29:20 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-06-10 23:19:58 +08:00
|
|
|
puts "", "Done"
|
2013-06-15 17:29:20 +08:00
|
|
|
end
|
2014-06-24 21:35:15 +08:00
|
|
|
|
2015-05-25 23:59:00 +08:00
|
|
|
################################################################################
|
|
|
|
# migrate_from_s3 #
|
|
|
|
################################################################################
|
|
|
|
|
2014-06-24 21:35:15 +08:00
|
|
|
task "uploads:migrate_from_s3" => :environment do
|
2016-07-25 18:12:10 +08:00
|
|
|
ENV["RAILS_DB"] ? migrate_from_s3 : migrate_all_from_s3
|
|
|
|
end
|
2014-06-24 21:35:15 +08:00
|
|
|
|
2016-07-25 18:12:10 +08:00
|
|
|
def guess_filename(url, raw)
|
|
|
|
begin
|
|
|
|
uri = URI.parse("http:#{url}")
|
|
|
|
f = uri.open("rb", read_timeout: 5, redirect: true, allow_redirections: :all)
|
|
|
|
filename = if f.meta && f.meta["content-disposition"]
|
|
|
|
f.meta["content-disposition"][/filename="([^"]+)"/, 1].presence
|
|
|
|
end
|
|
|
|
filename ||= raw[/<a class="attachment" href="(?:https?:)?#{Regexp.escape(url)}">([^<]+)<\/a>/, 1].presence
|
|
|
|
filename ||= File.basename(url)
|
|
|
|
filename
|
|
|
|
rescue
|
2017-07-28 09:20:09 +08:00
|
|
|
nil
|
2016-07-25 18:12:10 +08:00
|
|
|
ensure
|
|
|
|
f.try(:close!) rescue nil
|
|
|
|
end
|
|
|
|
end
|
2015-03-19 01:23:55 +08:00
|
|
|
|
2016-07-25 18:12:10 +08:00
|
|
|
def migrate_all_from_s3
|
|
|
|
RailsMultisite::ConnectionManagement.each_connection { migrate_from_s3 }
|
|
|
|
end
|
2015-03-19 01:23:55 +08:00
|
|
|
|
2016-07-25 18:12:10 +08:00
|
|
|
def migrate_from_s3
|
|
|
|
require "file_store/s3_store"
|
2014-06-24 21:35:15 +08:00
|
|
|
|
2016-07-25 18:12:10 +08:00
|
|
|
# make sure S3 is disabled
|
2018-01-22 08:42:09 +08:00
|
|
|
if SiteSetting.Upload.enable_s3_uploads
|
2016-07-25 18:12:10 +08:00
|
|
|
puts "You must disable S3 uploads before running that task."
|
|
|
|
return
|
|
|
|
end
|
2014-06-24 21:35:15 +08:00
|
|
|
|
2016-07-25 18:12:10 +08:00
|
|
|
db = RailsMultisite::ConnectionManagement.current_db
|
2014-06-24 21:35:15 +08:00
|
|
|
|
2016-07-25 18:12:10 +08:00
|
|
|
puts "Migrating uploads from S3 to local storage for '#{db}'..."
|
2018-11-13 19:49:26 +08:00
|
|
|
|
2018-11-13 19:27:38 +08:00
|
|
|
max_file_size = [SiteSetting.max_image_size_kb, SiteSetting.max_attachment_size_kb].max.kilobytes
|
2014-06-24 21:35:15 +08:00
|
|
|
|
2018-08-02 04:58:46 +08:00
|
|
|
Post
|
|
|
|
.where("user_id > 0")
|
|
|
|
.where("raw LIKE '%.s3%.amazonaws.com/%' OR raw LIKE '%(upload://%'")
|
|
|
|
.find_each do |post|
|
2018-01-24 01:48:06 +08:00
|
|
|
begin
|
|
|
|
updated = false
|
|
|
|
|
2018-01-24 20:22:07 +08:00
|
|
|
post.raw.gsub!(/(\/\/[\w.-]+amazonaws\.com\/(original|optimized)\/([a-z0-9]+\/)+\h{40}([\w.-]+)?)/i) do |url|
|
2016-07-25 18:12:10 +08:00
|
|
|
begin
|
|
|
|
if filename = guess_filename(url, post.raw)
|
2018-11-13 19:27:38 +08:00
|
|
|
file = FileHelper.download("http:#{url}", max_file_size: max_file_size, tmp_file_name: "from_s3", follow_redirect: true)
|
2018-01-24 20:22:07 +08:00
|
|
|
sha1 = Upload.generate_digest(file)
|
|
|
|
origin = nil
|
|
|
|
|
|
|
|
existing_upload = Upload.find_by(sha1: sha1)
|
|
|
|
if existing_upload&.url&.start_with?("//")
|
|
|
|
filename = existing_upload.original_filename
|
|
|
|
origin = existing_upload.origin
|
|
|
|
existing_upload.destroy
|
|
|
|
end
|
|
|
|
|
|
|
|
new_upload = UploadCreator.new(file, filename, origin: origin).create_for(post.user_id || -1)
|
2018-01-24 01:48:06 +08:00
|
|
|
if new_upload&.save
|
|
|
|
updated = true
|
2018-01-24 20:22:07 +08:00
|
|
|
url = new_upload.url
|
2016-07-25 18:12:10 +08:00
|
|
|
end
|
|
|
|
end
|
2018-01-24 01:48:06 +08:00
|
|
|
|
|
|
|
url
|
|
|
|
rescue
|
|
|
|
url
|
2016-07-25 18:12:10 +08:00
|
|
|
end
|
|
|
|
end
|
2018-01-24 01:48:06 +08:00
|
|
|
|
2018-08-02 04:58:46 +08:00
|
|
|
post.raw.gsub!(/(upload:\/\/[0-9a-zA-Z]+\.\w+)/) do |url|
|
|
|
|
begin
|
|
|
|
if sha1 = Upload.sha1_from_short_url(url)
|
|
|
|
if upload = Upload.find_by(sha1: sha1)
|
2018-08-02 16:04:41 +08:00
|
|
|
if upload.url.start_with?("//")
|
2018-11-13 19:27:38 +08:00
|
|
|
file = FileHelper.download("http:#{upload.url}", max_file_size: max_file_size, tmp_file_name: "from_s3", follow_redirect: true)
|
2018-08-02 16:04:41 +08:00
|
|
|
filename = upload.original_filename
|
|
|
|
origin = upload.origin
|
|
|
|
upload.destroy
|
|
|
|
|
|
|
|
new_upload = UploadCreator.new(file, filename, origin: origin).create_for(post.user_id || -1)
|
|
|
|
if new_upload&.save
|
|
|
|
updated = true
|
|
|
|
url = new_upload.url
|
|
|
|
end
|
2018-08-02 04:58:46 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
url
|
|
|
|
rescue
|
|
|
|
url
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-24 01:48:06 +08:00
|
|
|
if updated
|
|
|
|
post.save!
|
|
|
|
post.rebake!
|
|
|
|
putc "#"
|
|
|
|
else
|
|
|
|
putc "."
|
|
|
|
end
|
2018-01-24 20:22:07 +08:00
|
|
|
|
2018-01-24 01:48:06 +08:00
|
|
|
rescue
|
|
|
|
putc "X"
|
2014-06-24 21:35:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-25 18:12:10 +08:00
|
|
|
puts "Done!"
|
2014-06-24 21:35:15 +08:00
|
|
|
end
|
2014-09-30 00:31:53 +08:00
|
|
|
|
2015-05-25 23:59:00 +08:00
|
|
|
################################################################################
|
|
|
|
# migrate_to_s3 #
|
|
|
|
################################################################################
|
|
|
|
|
|
|
|
task "uploads:migrate_to_s3" => :environment do
|
|
|
|
ENV["RAILS_DB"] ? migrate_to_s3 : migrate_to_s3_all_sites
|
|
|
|
end
|
|
|
|
|
|
|
|
def migrate_to_s3_all_sites
|
2019-05-21 00:25:56 +08:00
|
|
|
RailsMultisite::ConnectionManagement.each_connection do
|
|
|
|
begin
|
|
|
|
migrate_to_s3
|
2019-05-21 00:43:30 +08:00
|
|
|
rescue RuntimeError => e
|
2019-05-21 00:25:56 +08:00
|
|
|
if ENV["SKIP_FAILED"]
|
|
|
|
puts e
|
|
|
|
else
|
|
|
|
raise e unless ENV["SKIP_FAILED"]
|
|
|
|
end
|
2019-05-21 00:43:30 +08:00
|
|
|
end
|
2019-05-21 00:25:56 +08:00
|
|
|
end
|
2015-05-25 23:59:00 +08:00
|
|
|
end
|
|
|
|
|
2020-01-29 05:10:25 +08:00
|
|
|
def create_migration
|
2020-01-13 07:12:27 +08:00
|
|
|
FileStore::ToS3Migration.new(
|
|
|
|
s3_options: FileStore::ToS3Migration.s3_options_from_env,
|
|
|
|
dry_run: !!ENV["DRY_RUN"],
|
|
|
|
migrate_to_multisite: !!ENV["MIGRATE_TO_MULTISITE"],
|
|
|
|
skip_etag_verify: !!ENV["SKIP_ETAG_VERIFY"]
|
2020-01-29 05:10:25 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def migrate_to_s3
|
|
|
|
create_migration.migrate
|
2019-05-22 07:00:32 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
task "uploads:s3_migration_status" => :environment do
|
|
|
|
success = true
|
|
|
|
RailsMultisite::ConnectionManagement.each_connection do
|
2020-01-29 05:10:25 +08:00
|
|
|
success &&= create_migration.migration_successful?
|
2019-05-22 07:00:32 +08:00
|
|
|
end
|
|
|
|
|
2019-05-22 08:04:33 +08:00
|
|
|
queued_jobs = Sidekiq::Stats.new.queues.sum { |_ , x| x }
|
|
|
|
if queued_jobs > 50
|
|
|
|
puts "WARNING: There are #{queued_jobs} jobs queued! Wait till Sidekiq clears backlog prior to migrating site to a new host"
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
|
2019-05-22 10:58:54 +08:00
|
|
|
if !success
|
|
|
|
puts "Site is not ready for migration"
|
|
|
|
exit 1
|
|
|
|
end
|
2019-05-20 21:17:37 +08:00
|
|
|
|
2019-05-22 07:00:32 +08:00
|
|
|
puts "All sites appear to have uploads in order!"
|
2019-05-20 21:17:37 +08:00
|
|
|
end
|
|
|
|
|
2015-05-25 23:59:00 +08:00
|
|
|
################################################################################
|
2018-12-27 00:34:49 +08:00
|
|
|
# clean_up #
|
2015-05-25 23:59:00 +08:00
|
|
|
################################################################################
|
|
|
|
|
2014-09-30 00:31:53 +08:00
|
|
|
task "uploads:clean_up" => :environment do
|
2018-12-27 00:34:49 +08:00
|
|
|
ENV["RAILS_DB"] ? clean_up_uploads : clean_up_uploads_all_sites
|
|
|
|
end
|
|
|
|
|
|
|
|
def clean_up_uploads_all_sites
|
|
|
|
RailsMultisite::ConnectionManagement.each_connection { clean_up_uploads }
|
2016-09-02 14:50:13 +08:00
|
|
|
end
|
2014-09-30 00:31:53 +08:00
|
|
|
|
2016-09-02 14:50:13 +08:00
|
|
|
def clean_up_uploads
|
|
|
|
db = RailsMultisite::ConnectionManagement.current_db
|
2014-09-30 00:31:53 +08:00
|
|
|
|
2016-09-02 14:50:13 +08:00
|
|
|
puts "Cleaning up uploads and thumbnails for '#{db}'..."
|
2014-09-30 00:31:53 +08:00
|
|
|
|
2016-09-02 14:50:13 +08:00
|
|
|
if Discourse.store.external?
|
|
|
|
puts "This task only works for internal storages."
|
|
|
|
exit 1
|
|
|
|
end
|
2014-09-30 00:31:53 +08:00
|
|
|
|
2016-09-02 14:50:13 +08:00
|
|
|
puts <<~OUTPUT
|
|
|
|
This task will remove upload records and files permanently.
|
2014-09-30 00:31:53 +08:00
|
|
|
|
2016-09-02 14:50:13 +08:00
|
|
|
Would you like to take a full backup before the clean up? (Y/N)
|
|
|
|
OUTPUT
|
2014-09-30 00:31:53 +08:00
|
|
|
|
2016-09-02 14:50:13 +08:00
|
|
|
if STDIN.gets.chomp.downcase == 'y'
|
|
|
|
puts "Starting backup..."
|
|
|
|
backuper = BackupRestore::Backuper.new(Discourse.system_user.id)
|
|
|
|
backuper.run
|
|
|
|
exit 1 unless backuper.success
|
|
|
|
end
|
2014-09-30 00:31:53 +08:00
|
|
|
|
2016-09-02 14:50:13 +08:00
|
|
|
public_directory = Rails.root.join("public").to_s
|
2014-09-30 00:31:53 +08:00
|
|
|
|
2016-09-02 14:50:13 +08:00
|
|
|
##
|
|
|
|
## DATABASE vs FILE SYSTEM
|
|
|
|
##
|
2014-09-30 00:31:53 +08:00
|
|
|
|
2016-09-02 14:50:13 +08:00
|
|
|
# uploads & avatars
|
|
|
|
Upload.find_each do |upload|
|
|
|
|
path = File.join(public_directory, upload.url)
|
2014-09-30 00:31:53 +08:00
|
|
|
|
2016-09-02 14:50:13 +08:00
|
|
|
if !File.exists?(path)
|
|
|
|
upload.destroy!
|
|
|
|
putc "#"
|
|
|
|
else
|
|
|
|
putc "."
|
2014-09-30 00:31:53 +08:00
|
|
|
end
|
2016-09-02 14:50:13 +08:00
|
|
|
end
|
2014-09-30 00:31:53 +08:00
|
|
|
|
2016-09-02 14:50:13 +08:00
|
|
|
# optimized images
|
|
|
|
OptimizedImage.find_each do |optimized_image|
|
|
|
|
path = File.join(public_directory, optimized_image.url)
|
|
|
|
|
|
|
|
if !File.exists?(path)
|
|
|
|
optimized_image.destroy!
|
|
|
|
putc "#"
|
|
|
|
else
|
|
|
|
putc "."
|
2014-09-30 00:31:53 +08:00
|
|
|
end
|
2016-09-02 14:50:13 +08:00
|
|
|
end
|
2014-09-30 00:31:53 +08:00
|
|
|
|
2016-09-02 14:50:13 +08:00
|
|
|
##
|
|
|
|
## FILE SYSTEM vs DATABASE
|
|
|
|
##
|
2014-09-30 00:31:53 +08:00
|
|
|
|
2016-09-02 14:50:13 +08:00
|
|
|
uploads_directory = File.join(public_directory, 'uploads', db).to_s
|
|
|
|
|
|
|
|
# avatars (no avatar should be stored in that old directory)
|
|
|
|
FileUtils.rm_rf("#{uploads_directory}/avatars")
|
|
|
|
|
|
|
|
# uploads and optimized images
|
|
|
|
Dir.glob("#{uploads_directory}/**/*.*").each do |file_path|
|
|
|
|
sha1 = Upload.generate_digest(file_path)
|
|
|
|
url = file_path.split(public_directory, 2)[1]
|
|
|
|
|
|
|
|
if (Upload.where(sha1: sha1).empty? &&
|
|
|
|
Upload.where(url: url).empty?) &&
|
|
|
|
(OptimizedImage.where(sha1: sha1).empty? &&
|
|
|
|
OptimizedImage.where(url: url).empty?)
|
|
|
|
|
|
|
|
FileUtils.rm(file_path)
|
|
|
|
putc "#"
|
|
|
|
else
|
|
|
|
putc "."
|
|
|
|
end
|
2014-09-30 00:31:53 +08:00
|
|
|
end
|
|
|
|
|
2016-09-02 14:50:13 +08:00
|
|
|
puts "Removing empty directories..."
|
|
|
|
puts `find #{uploads_directory} -type d -empty -exec rmdir {} \\;`
|
|
|
|
|
|
|
|
puts "Done!"
|
2014-09-30 00:31:53 +08:00
|
|
|
end
|
2015-05-11 08:30:22 +08:00
|
|
|
|
2015-05-25 23:59:00 +08:00
|
|
|
################################################################################
|
2019-05-29 02:00:43 +08:00
|
|
|
# missing files #
|
2015-05-25 23:59:00 +08:00
|
|
|
################################################################################
|
2015-05-11 08:30:22 +08:00
|
|
|
|
|
|
|
# list all missing uploads and optimized images
|
2019-05-29 02:00:43 +08:00
|
|
|
task "uploads:missing_files" => :environment do
|
2016-09-02 10:22:03 +08:00
|
|
|
if ENV["RAILS_DB"]
|
2019-02-15 03:04:35 +08:00
|
|
|
list_missing_uploads(skip_optimized: ENV['SKIP_OPTIMIZED'])
|
2016-09-02 10:22:03 +08:00
|
|
|
else
|
|
|
|
RailsMultisite::ConnectionManagement.each_connection do |db|
|
2019-05-21 14:06:35 +08:00
|
|
|
if ENV["SKIP_EXTERNAL"] == "1" && Discourse.store.external?
|
|
|
|
puts "#{RailsMultisite::ConnectionManagement.current_db} has uploads stored externally skipping!"
|
|
|
|
else
|
2019-05-21 14:11:38 +08:00
|
|
|
if Discourse.store.external?
|
|
|
|
puts "-" * 80
|
|
|
|
puts "WARNING! WARNING! WARNING!"
|
|
|
|
puts "-" * 80
|
|
|
|
puts
|
|
|
|
puts <<~TEXT
|
|
|
|
#{RailsMultisite::ConnectionManagement.current_db} has uploads on S3!
|
|
|
|
validating without inventory is likely to take an enormous amount of time.
|
|
|
|
We recommend you run SKIP_EXTERNAL=1 rake uploads:missing to skip validating if on a multisite.
|
|
|
|
TEXT
|
|
|
|
end
|
2019-05-21 14:06:35 +08:00
|
|
|
list_missing_uploads(skip_optimized: ENV['SKIP_OPTIMIZED'])
|
|
|
|
end
|
2015-05-11 08:30:22 +08:00
|
|
|
end
|
2016-09-02 10:22:03 +08:00
|
|
|
end
|
|
|
|
end
|
2015-05-11 08:30:22 +08:00
|
|
|
|
2019-02-15 03:04:35 +08:00
|
|
|
def list_missing_uploads(skip_optimized: false)
|
|
|
|
Discourse.store.list_missing_uploads(skip_optimized: skip_optimized)
|
2015-05-11 08:30:22 +08:00
|
|
|
end
|
2015-05-11 18:59:50 +08:00
|
|
|
|
2019-05-29 02:00:43 +08:00
|
|
|
task "uploads:missing" => :environment do
|
|
|
|
Rake::Task["uploads:missing_files"].invoke
|
|
|
|
end
|
|
|
|
|
2015-05-25 23:59:00 +08:00
|
|
|
################################################################################
|
|
|
|
# regenerate_missing_optimized #
|
|
|
|
################################################################################
|
|
|
|
|
2015-05-11 18:59:50 +08:00
|
|
|
# regenerate missing optimized images
|
|
|
|
task "uploads:regenerate_missing_optimized" => :environment do
|
2016-09-02 13:06:31 +08:00
|
|
|
if ENV["RAILS_DB"]
|
|
|
|
regenerate_missing_optimized
|
|
|
|
else
|
|
|
|
RailsMultisite::ConnectionManagement.each_connection { regenerate_missing_optimized }
|
|
|
|
end
|
2015-05-11 22:19:16 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def regenerate_missing_optimized
|
2015-05-12 01:07:39 +08:00
|
|
|
db = RailsMultisite::ConnectionManagement.current_db
|
|
|
|
|
|
|
|
puts "Regenerating missing optimized images for '#{db}'..."
|
2015-05-11 18:59:50 +08:00
|
|
|
|
|
|
|
if Discourse.store.external?
|
|
|
|
puts "This task only works for internal storages."
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
public_directory = "#{Rails.root}/public"
|
|
|
|
missing_uploads = Set.new
|
|
|
|
|
2016-08-25 18:29:52 +08:00
|
|
|
avatar_upload_ids = UserAvatar.all.pluck(:custom_upload_id, :gravatar_upload_id).flatten.compact
|
2015-05-11 18:59:50 +08:00
|
|
|
|
2016-08-25 18:29:52 +08:00
|
|
|
default_scope = OptimizedImage.includes(:upload)
|
2015-05-12 01:07:39 +08:00
|
|
|
|
2016-08-25 18:29:52 +08:00
|
|
|
[
|
|
|
|
default_scope
|
|
|
|
.where("optimized_images.upload_id IN (?)", avatar_upload_ids),
|
2015-05-11 21:41:52 +08:00
|
|
|
|
2016-08-25 18:29:52 +08:00
|
|
|
default_scope
|
|
|
|
.where("optimized_images.upload_id NOT IN (?)", avatar_upload_ids)
|
|
|
|
.where("LENGTH(COALESCE(url, '')) > 0")
|
|
|
|
.where("width > 0 AND height > 0")
|
|
|
|
].each do |scope|
|
|
|
|
scope.find_each do |optimized_image|
|
|
|
|
upload = optimized_image.upload
|
2015-05-11 18:59:50 +08:00
|
|
|
|
2016-08-25 18:29:52 +08:00
|
|
|
next unless optimized_image.url =~ /^\/[^\/]/
|
|
|
|
next unless upload.url =~ /^\/[^\/]/
|
|
|
|
|
|
|
|
thumbnail = "#{public_directory}#{optimized_image.url}"
|
|
|
|
original = "#{public_directory}#{upload.url}"
|
|
|
|
|
|
|
|
if !File.exists?(thumbnail) || File.size(thumbnail) <= 0
|
|
|
|
# make sure the original image exists locally
|
|
|
|
if (!File.exists?(original) || File.size(original) <= 0) && upload.origin.present?
|
|
|
|
# try to fix it by redownloading it
|
|
|
|
begin
|
2017-05-25 01:42:52 +08:00
|
|
|
downloaded = FileHelper.download(
|
|
|
|
upload.origin,
|
|
|
|
max_file_size: SiteSetting.max_image_size_kb.kilobytes,
|
|
|
|
tmp_file_name: "discourse-missing",
|
|
|
|
follow_redirect: true
|
|
|
|
) rescue nil
|
2016-08-25 18:29:52 +08:00
|
|
|
if downloaded && downloaded.size > 0
|
|
|
|
FileUtils.mkdir_p(File.dirname(original))
|
|
|
|
File.open(original, "wb") { |f| f.write(downloaded.read) }
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
downloaded.try(:close!) if downloaded.respond_to?(:close!)
|
2015-05-12 01:07:39 +08:00
|
|
|
end
|
2015-05-11 23:03:48 +08:00
|
|
|
end
|
|
|
|
|
2016-08-25 18:29:52 +08:00
|
|
|
if File.exists?(original) && File.size(original) > 0
|
|
|
|
FileUtils.mkdir_p(File.dirname(thumbnail))
|
|
|
|
OptimizedImage.resize(original, thumbnail, optimized_image.width, optimized_image.height)
|
|
|
|
putc "#"
|
|
|
|
else
|
|
|
|
missing_uploads << original
|
|
|
|
putc "X"
|
|
|
|
end
|
2015-05-11 18:59:50 +08:00
|
|
|
else
|
2016-08-25 18:29:52 +08:00
|
|
|
putc "."
|
2015-05-11 18:59:50 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
puts "", "Done"
|
|
|
|
|
|
|
|
if missing_uploads.size > 0
|
|
|
|
puts "Missing uploads:"
|
|
|
|
missing_uploads.sort.each { |u| puts u }
|
|
|
|
end
|
|
|
|
end
|
2015-05-19 18:31:51 +08:00
|
|
|
|
2015-05-25 23:59:00 +08:00
|
|
|
################################################################################
|
2015-06-12 18:02:36 +08:00
|
|
|
# migrate_to_new_scheme #
|
2015-05-25 23:59:00 +08:00
|
|
|
################################################################################
|
|
|
|
|
2015-06-12 18:02:36 +08:00
|
|
|
task "uploads:start_migration" => :environment do
|
|
|
|
SiteSetting.migrate_to_new_scheme = true
|
|
|
|
puts "Migration started!"
|
2015-05-19 18:31:51 +08:00
|
|
|
end
|
|
|
|
|
2015-06-12 18:02:36 +08:00
|
|
|
task "uploads:stop_migration" => :environment do
|
|
|
|
SiteSetting.migrate_to_new_scheme = false
|
|
|
|
puts "Migration stoped!"
|
2015-05-19 18:31:51 +08:00
|
|
|
end
|
2016-09-01 15:19:14 +08:00
|
|
|
|
|
|
|
task "uploads:analyze", [:cache_path, :limit] => :environment do |_, args|
|
|
|
|
now = Time.zone.now
|
|
|
|
current_db = RailsMultisite::ConnectionManagement.current_db
|
|
|
|
|
|
|
|
puts "Analyzing uploads for '#{current_db}'... This may take awhile...\n"
|
|
|
|
cache_path = args[:cache_path]
|
|
|
|
|
|
|
|
current_db = RailsMultisite::ConnectionManagement.current_db
|
|
|
|
uploads_path = Rails.root.join('public', 'uploads', current_db)
|
|
|
|
|
|
|
|
path =
|
|
|
|
if cache_path
|
|
|
|
cache_path
|
|
|
|
else
|
|
|
|
path = "/tmp/#{current_db}-#{now.to_i}-paths.txt"
|
|
|
|
FileUtils.touch("/tmp/#{now.to_i}-paths.txt")
|
|
|
|
`find #{uploads_path} -type f -printf '%s %h/%f\n' > #{path}`
|
|
|
|
path
|
|
|
|
end
|
|
|
|
|
|
|
|
extensions = {}
|
|
|
|
paths_count = 0
|
|
|
|
|
|
|
|
File.readlines(path).each do |line|
|
|
|
|
size, file_path = line.split(" ", 2)
|
|
|
|
|
|
|
|
paths_count += 1
|
|
|
|
extension = File.extname(file_path).chomp.downcase
|
|
|
|
extensions[extension] ||= {}
|
|
|
|
extensions[extension]["count"] ||= 0
|
|
|
|
extensions[extension]["count"] += 1
|
|
|
|
extensions[extension]["size"] ||= 0
|
|
|
|
extensions[extension]["size"] += size.to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
uploads_count = Upload.count
|
|
|
|
optimized_images_count = OptimizedImage.count
|
|
|
|
|
|
|
|
puts <<~REPORT
|
|
|
|
Report for '#{current_db}'
|
|
|
|
-----------#{'-' * current_db.length}
|
|
|
|
Number of `Upload` records in DB: #{uploads_count}
|
|
|
|
Number of `OptimizedImage` records in DB: #{optimized_images_count}
|
|
|
|
**Total DB records: #{uploads_count + optimized_images_count}**
|
|
|
|
|
|
|
|
Number of images in uploads folder: #{paths_count}
|
|
|
|
------------------------------------#{'-' * paths_count.to_s.length}
|
|
|
|
|
|
|
|
REPORT
|
|
|
|
|
|
|
|
helper = Class.new do
|
|
|
|
include ActionView::Helpers::NumberHelper
|
|
|
|
end
|
|
|
|
|
|
|
|
helper = helper.new
|
|
|
|
|
|
|
|
printf "%-15s | %-15s | %-15s\n", 'extname', 'total size', 'count'
|
|
|
|
puts "-" * 45
|
|
|
|
|
|
|
|
extensions.sort_by { |_, value| value['size'] }.reverse.each do |extname, value|
|
|
|
|
printf "%-15s | %-15s | %-15s\n", extname, helper.number_to_human_size(value['size']), value['count']
|
|
|
|
end
|
|
|
|
|
|
|
|
puts "\n"
|
|
|
|
|
|
|
|
limit = args[:limit] || 10
|
|
|
|
|
|
|
|
sql = <<~SQL
|
|
|
|
SELECT
|
|
|
|
users.username,
|
|
|
|
COUNT(uploads.user_id) AS num_of_uploads,
|
|
|
|
SUM(uploads.filesize) AS total_size_of_uploads,
|
|
|
|
COUNT(optimized_images.id) AS num_of_optimized_images
|
|
|
|
FROM users
|
|
|
|
INNER JOIN uploads ON users.id = uploads.user_id
|
|
|
|
INNER JOIN optimized_images ON uploads.id = optimized_images.upload_id
|
|
|
|
GROUP BY users.id
|
|
|
|
ORDER BY total_size_of_uploads DESC
|
|
|
|
LIMIT #{limit}
|
|
|
|
SQL
|
|
|
|
|
|
|
|
puts "Users using the most disk space"
|
|
|
|
puts "-------------------------------\n"
|
|
|
|
printf "%-25s | %-25s | %-25s | %-25s\n", 'username', 'total size of uploads', 'number of uploads', 'number of optimized images'
|
|
|
|
puts "-" * 110
|
|
|
|
|
2018-06-19 14:13:14 +08:00
|
|
|
DB.query_single(sql).each do |username, num_of_uploads, total_size_of_uploads, num_of_optimized_images|
|
2016-09-01 15:19:14 +08:00
|
|
|
printf "%-25s | %-25s | %-25s | %-25s\n", username, helper.number_to_human_size(total_size_of_uploads), num_of_uploads, num_of_optimized_images
|
|
|
|
end
|
|
|
|
|
|
|
|
puts "\n"
|
|
|
|
puts "List of file paths @ #{path}"
|
|
|
|
puts "Duration: #{Time.zone.now - now} seconds"
|
|
|
|
end
|
2018-08-08 13:14:52 +08:00
|
|
|
|
|
|
|
task "uploads:fix_incorrect_extensions" => :environment do
|
2018-08-10 09:28:05 +08:00
|
|
|
UploadFixer.fix_all_extensions
|
2018-08-08 13:14:52 +08:00
|
|
|
end
|
2018-09-05 16:54:15 +08:00
|
|
|
|
2019-03-07 21:15:30 +08:00
|
|
|
task "uploads:recover_from_tombstone" => :environment do
|
|
|
|
Rake::Task["uploads:recover"].invoke
|
|
|
|
end
|
|
|
|
|
2018-09-12 16:51:53 +08:00
|
|
|
task "uploads:recover" => :environment do
|
|
|
|
|
2018-09-12 21:53:01 +08:00
|
|
|
dry_run = ENV["DRY_RUN"].present?
|
2019-08-02 02:24:06 +08:00
|
|
|
stop_on_error = ENV["STOP_ON_ERROR"].present?
|
2018-09-12 21:53:01 +08:00
|
|
|
|
2018-09-05 16:54:15 +08:00
|
|
|
if ENV["RAILS_DB"]
|
2019-08-02 02:24:06 +08:00
|
|
|
UploadRecovery.new(dry_run: dry_run, stop_on_error: stop_on_error).recover
|
2018-09-05 16:54:15 +08:00
|
|
|
else
|
|
|
|
RailsMultisite::ConnectionManagement.each_connection do |db|
|
2019-08-02 02:24:06 +08:00
|
|
|
UploadRecovery.new(dry_run: dry_run, stop_on_error: stop_on_error).recover
|
2018-09-10 15:14:30 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-05-22 13:24:36 +08:00
|
|
|
|
2020-01-07 10:27:24 +08:00
|
|
|
task "uploads:disable_secure_media" => :environment do
|
|
|
|
RailsMultisite::ConnectionManagement.each_connection do |db|
|
|
|
|
unless Discourse.store.external?
|
|
|
|
puts "This task only works for external storage."
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
|
|
|
|
puts "Disabling secure media and resetting uploads to not secure in #{db}...", ""
|
|
|
|
|
|
|
|
SiteSetting.secure_media = false
|
|
|
|
|
|
|
|
secure_uploads = Upload.includes(:posts).where(secure: true)
|
|
|
|
secure_upload_count = secure_uploads.count
|
|
|
|
|
|
|
|
i = 0
|
|
|
|
secure_uploads.find_each(batch_size: 20).each do |upload|
|
|
|
|
Upload.transaction do
|
|
|
|
upload.secure = false
|
|
|
|
|
|
|
|
RakeHelpers.print_status_with_label("Updating ACL for upload #{upload.id}.......", i, secure_upload_count)
|
|
|
|
Discourse.store.update_upload_ACL(upload)
|
|
|
|
|
|
|
|
RakeHelpers.print_status_with_label("Rebaking posts for upload #{upload.id}.......", i, secure_upload_count)
|
|
|
|
upload.posts.each(&:rebake!)
|
|
|
|
upload.save
|
|
|
|
|
|
|
|
i += 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
RakeHelpers.print_status_with_label("Rebaking and updating complete! ", i, secure_upload_count)
|
|
|
|
puts ""
|
|
|
|
end
|
|
|
|
|
|
|
|
puts "Secure media is now disabled!", ""
|
|
|
|
end
|
|
|
|
|
2019-11-18 09:25:42 +08:00
|
|
|
##
|
|
|
|
# Run this task whenever the secure_media or login_required
|
|
|
|
# settings are changed for a Discourse instance to update
|
|
|
|
# the upload secure flag and S3 upload ACLs.
|
|
|
|
task "uploads:ensure_correct_acl" => :environment do
|
|
|
|
RailsMultisite::ConnectionManagement.each_connection do |db|
|
|
|
|
unless Discourse.store.external?
|
|
|
|
puts "This task only works for external storage."
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
|
|
|
|
puts "Ensuring correct ACL for uploads in #{db}...", ""
|
|
|
|
|
|
|
|
Upload.transaction do
|
|
|
|
mark_secure_in_loop_because_no_login_required = false
|
|
|
|
|
2020-02-17 12:21:43 +08:00
|
|
|
# If secure media is enabled we need to first set the access control post of
|
|
|
|
# all post uploads (even uploads that are linked to multiple posts). If the
|
|
|
|
# upload is not set to secure media then this has no other effect on the upload,
|
|
|
|
# but we _must_ know what the access control post is because the with_secure_media?
|
|
|
|
# method is on the post, and this knows about the category security & PM status
|
|
|
|
if SiteSetting.secure_media?
|
|
|
|
update_uploads_access_control_post
|
|
|
|
end
|
|
|
|
|
2019-11-18 09:25:42 +08:00
|
|
|
# First of all only get relevant uploads (supported media).
|
|
|
|
#
|
|
|
|
# Also only get uploads that are not for a theme or a site setting, so only
|
|
|
|
# get post related uploads.
|
2020-02-18 08:35:15 +08:00
|
|
|
uploads_with_supported_media = Upload.includes(:posts, :access_control_post, :optimized_images).where(
|
2019-11-18 09:25:42 +08:00
|
|
|
"LOWER(original_filename) SIMILAR TO '%\.(jpg|jpeg|png|gif|svg|ico|mp3|ogg|wav|m4a|mov|mp4|webm|ogv)'"
|
|
|
|
).joins(:post_uploads)
|
|
|
|
|
|
|
|
puts "There are #{uploads_with_supported_media.count} upload(s) with supported media that could be marked secure.", ""
|
|
|
|
|
|
|
|
# Simply mark all these uploads as secure if login_required because no anons will be able to access them
|
|
|
|
if SiteSetting.login_required?
|
|
|
|
mark_all_as_secure_login_required(uploads_with_supported_media)
|
|
|
|
else
|
|
|
|
|
|
|
|
# If NOT login_required, then we have to go for the other slower flow, where in the loop
|
2020-02-17 12:21:43 +08:00
|
|
|
# we mark the upload secure based on UploadSecurity.should_be_secure?
|
2019-11-18 09:25:42 +08:00
|
|
|
mark_secure_in_loop_because_no_login_required = true
|
|
|
|
puts "Marking posts as secure in the next step because login_required is false."
|
|
|
|
end
|
|
|
|
|
2020-02-17 12:21:43 +08:00
|
|
|
puts "", "Determining which of #{uploads_with_supported_media.count} upload posts need to be marked secure and be rebaked.", ""
|
2019-11-18 09:25:42 +08:00
|
|
|
|
2020-02-17 12:21:43 +08:00
|
|
|
upload_ids_to_mark_as_secure, posts_to_rebake = determine_upload_security_and_posts_to_rebake(
|
2019-11-18 09:25:42 +08:00
|
|
|
uploads_with_supported_media, mark_secure_in_loop_because_no_login_required
|
|
|
|
)
|
|
|
|
|
|
|
|
mark_specific_uploads_as_secure_no_login_required(upload_ids_to_mark_as_secure)
|
2020-02-17 12:21:43 +08:00
|
|
|
|
|
|
|
post_rebake_errors = rebake_upload_posts(posts_to_rebake)
|
|
|
|
log_rebake_errors(post_rebake_errors)
|
2019-11-18 09:25:42 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
puts "", "Done"
|
|
|
|
end
|
|
|
|
|
|
|
|
def mark_all_as_secure_login_required(uploads_with_supported_media)
|
|
|
|
puts "Marking #{uploads_with_supported_media.count} upload(s) as secure because login_required is true.", ""
|
|
|
|
uploads_with_supported_media.update_all(secure: true)
|
|
|
|
puts "Finished marking upload(s) as secure."
|
|
|
|
end
|
|
|
|
|
2020-02-17 12:21:43 +08:00
|
|
|
def log_rebake_errors(rebake_errors)
|
|
|
|
return if rebake_errors.empty?
|
|
|
|
puts "The following post rebakes failed with error:", ""
|
|
|
|
rebake_errors.each do |message|
|
2019-11-18 09:25:42 +08:00
|
|
|
puts message
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def mark_specific_uploads_as_secure_no_login_required(upload_ids_to_mark_as_secure)
|
|
|
|
return if upload_ids_to_mark_as_secure.empty?
|
2020-02-17 12:21:43 +08:00
|
|
|
puts "Marking #{upload_ids_to_mark_as_secure.length} uploads as secure because UploadSecurity determined them to be secure."
|
2019-11-18 09:25:42 +08:00
|
|
|
Upload.where(id: upload_ids_to_mark_as_secure).update_all(secure: true)
|
|
|
|
puts "Finished marking uploads as secure."
|
|
|
|
end
|
|
|
|
|
2020-02-17 12:21:43 +08:00
|
|
|
def update_uploads_access_control_post
|
|
|
|
access_control_post_updates = []
|
|
|
|
uploads_with_post_ids = DB.query(<<-SQL
|
|
|
|
SELECT upload_id, (
|
|
|
|
SELECT string_agg(CAST(post_uploads.post_id AS varchar), ',' ORDER BY post_uploads.id) as post_ids
|
|
|
|
FROM post_uploads
|
|
|
|
WHERE pu.upload_id = post_uploads.upload_id
|
|
|
|
) FROM post_uploads pu
|
|
|
|
SQL
|
|
|
|
)
|
|
|
|
uploads_with_post_ids.each do |row|
|
|
|
|
first_post_id = row.post_ids.split(",").first.to_i
|
|
|
|
access_control_post_updates << "UPDATE uploads SET access_control_post_id = #{first_post_id} WHERE id = #{row.upload_id};"
|
|
|
|
end
|
|
|
|
DB.exec(access_control_post_updates.join("\n"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def rebake_upload_posts(posts_to_rebake)
|
|
|
|
post_rebake_errors = []
|
|
|
|
puts "", "Rebaking #{posts_to_rebake.length} posts with affected uploads.", ""
|
|
|
|
begin
|
|
|
|
i = 0
|
|
|
|
posts_to_rebake.each do |post|
|
|
|
|
RakeHelpers.print_status_with_label("Determining which uploads to mark secure and rebake.....", i, posts_to_rebake.length)
|
|
|
|
post.rebake!
|
|
|
|
i += 1
|
|
|
|
end
|
|
|
|
|
|
|
|
RakeHelpers.print_status_with_label("Rebaking complete! ", i, posts_to_rebake.length)
|
|
|
|
puts ""
|
|
|
|
rescue => e
|
|
|
|
post_rebake_errors << e.message
|
|
|
|
end
|
|
|
|
post_rebake_errors
|
|
|
|
end
|
|
|
|
|
|
|
|
def determine_upload_security_and_posts_to_rebake(uploads_with_supported_media, mark_secure_in_loop_because_no_login_required)
|
2019-11-18 09:25:42 +08:00
|
|
|
upload_ids_to_mark_as_secure = []
|
2020-02-17 12:21:43 +08:00
|
|
|
posts_to_rebake = []
|
2019-11-18 09:25:42 +08:00
|
|
|
|
|
|
|
i = 0
|
|
|
|
uploads_with_supported_media.find_each(batch_size: 50) do |upload_with_supported_media|
|
|
|
|
RakeHelpers.print_status_with_label("Updating ACL for upload.......", i, uploads_with_supported_media.count)
|
|
|
|
|
2020-02-17 12:21:43 +08:00
|
|
|
# we just need to determine the post security here so the ACL is set to the correct thing,
|
|
|
|
# because the update_upload_ACL method uses upload.secure?
|
|
|
|
upload_with_supported_media.secure = UploadSecurity.new(upload_with_supported_media).should_be_secure?
|
2019-11-18 09:25:42 +08:00
|
|
|
Discourse.store.update_upload_ACL(upload_with_supported_media)
|
|
|
|
|
2020-02-17 12:21:43 +08:00
|
|
|
RakeHelpers.print_status_with_label("Determining which uploads to mark secure and rebake.....", i, uploads_with_supported_media.count)
|
|
|
|
upload_with_supported_media.posts.each { |post| posts_to_rebake << post }
|
2019-11-18 09:25:42 +08:00
|
|
|
|
2020-02-17 12:21:43 +08:00
|
|
|
if mark_secure_in_loop_because_no_login_required && upload_with_supported_media.secure?
|
|
|
|
upload_ids_to_mark_as_secure << upload_with_supported_media.id
|
2019-11-18 09:25:42 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
i += 1
|
|
|
|
end
|
2020-02-17 12:21:43 +08:00
|
|
|
RakeHelpers.print_status_with_label("Determination complete! ", i, uploads_with_supported_media.count)
|
2019-11-18 09:25:42 +08:00
|
|
|
puts ""
|
|
|
|
|
2020-02-17 12:21:43 +08:00
|
|
|
[upload_ids_to_mark_as_secure, posts_to_rebake]
|
2019-11-18 09:25:42 +08:00
|
|
|
end
|
|
|
|
|
2019-05-22 13:24:36 +08:00
|
|
|
def inline_uploads(post)
|
|
|
|
replaced = false
|
|
|
|
|
|
|
|
original_raw = post.raw
|
|
|
|
|
|
|
|
post.raw = post.raw.gsub(/(\((\/uploads\S+).*\))/) do
|
|
|
|
upload = Upload.find_by(url: $2)
|
2019-05-22 13:51:09 +08:00
|
|
|
if !upload
|
|
|
|
data = Upload.extract_url($2)
|
|
|
|
if data && sha1 = data[2]
|
|
|
|
upload = Upload.find_by(sha1: sha1)
|
|
|
|
if !upload
|
|
|
|
sha_map = JSON.parse(post.custom_fields["UPLOAD_SHA1_MAP"] || "{}")
|
|
|
|
if mapped_sha = sha_map[sha1]
|
|
|
|
upload = Upload.find_by(sha1: mapped_sha)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-05-22 13:24:36 +08:00
|
|
|
result = $1
|
|
|
|
|
|
|
|
if upload&.id
|
|
|
|
result.sub!($2, upload.short_url)
|
|
|
|
replaced = true
|
|
|
|
else
|
|
|
|
puts "Upload not found #{$2} in Post #{post.id} - #{post.url}"
|
|
|
|
end
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
if replaced
|
2019-05-23 13:09:16 +08:00
|
|
|
puts "Corrected image urls in #{post.full_url} raw backup stored in custom field"
|
2019-05-22 13:24:36 +08:00
|
|
|
post.custom_fields["BACKUP_POST_RAW"] = original_raw
|
|
|
|
post.save_custom_fields
|
2019-05-23 13:09:16 +08:00
|
|
|
post.save!(validate: false)
|
2019-05-22 13:24:36 +08:00
|
|
|
post.rebake!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-23 13:09:16 +08:00
|
|
|
def inline_img_tags(post)
|
|
|
|
replaced = false
|
|
|
|
|
|
|
|
original_raw = post.raw
|
|
|
|
post.raw = post.raw.gsub(/(<img\s+src=["'](\/uploads\/[^'"]*)["'].*>)/i) do
|
|
|
|
next if $2.include?("..")
|
|
|
|
|
|
|
|
upload = Upload.find_by(url: $2)
|
|
|
|
if !upload
|
|
|
|
data = Upload.extract_url($2)
|
|
|
|
if data && sha1 = data[2]
|
|
|
|
upload = Upload.find_by(sha1: sha1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if !upload
|
|
|
|
local_file = File.join(Rails.root, "public", $2)
|
|
|
|
if File.exist?(local_file)
|
2019-05-23 13:28:41 +08:00
|
|
|
File.open(local_file) do |f|
|
|
|
|
upload = UploadCreator.new(f, "image").create_for(post.user_id)
|
2019-05-23 13:09:16 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if upload
|
|
|
|
replaced = true
|
|
|
|
"![image](#{upload.short_url})"
|
|
|
|
else
|
|
|
|
puts "skipping missing upload in #{post.full_url} #{$1}"
|
|
|
|
$1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if replaced
|
|
|
|
puts "Corrected image urls in #{post.full_url} raw backup stored in custom field"
|
|
|
|
post.custom_fields["BACKUP_POST_RAW"] = original_raw
|
|
|
|
post.save_custom_fields
|
|
|
|
post.save!(validate: false)
|
|
|
|
post.rebake!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def fix_relative_links
|
2019-05-22 13:24:36 +08:00
|
|
|
Post.where('raw like ?', '%](/uploads%').find_each do |post|
|
|
|
|
inline_uploads(post)
|
|
|
|
end
|
2019-05-23 13:09:16 +08:00
|
|
|
Post.where("raw ilike ?", '%<img%src=%/uploads/%>%').find_each do |post|
|
|
|
|
inline_img_tags(post)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
task "uploads:fix_relative_upload_links" => :environment do
|
|
|
|
if RailsMultisite::ConnectionManagement.current_db != "default"
|
|
|
|
fix_relative_links
|
|
|
|
else
|
|
|
|
RailsMultisite::ConnectionManagement.each_connection do
|
|
|
|
fix_relative_links
|
|
|
|
end
|
|
|
|
end
|
2019-05-22 13:24:36 +08:00
|
|
|
end
|