Skip to content

Commit

Permalink
FIX: Regenerate optimized images instead of migrating from old scheme.
Browse files Browse the repository at this point in the history
`OptimizedImage.migrate_to_new_scheme` was optimizing optimized images
which we don't need to do. Regnerating the optimized image is way easier.
  • Loading branch information
tgxworld committed Apr 3, 2019
1 parent c3047a9 commit feb731b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 71 deletions.
15 changes: 10 additions & 5 deletions app/jobs/scheduled/migrate_upload_scheme.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def execute(args)

# migrate uploads to new scheme
problems = Upload.migrate_to_new_scheme(50)

problems.each do |hash|
upload_id = hash[:upload].id
Discourse.handle_job_exception(hash[:ex], error_context(args, "Migrating upload id #{upload_id}", upload_id: upload_id))
Expand All @@ -24,12 +25,16 @@ def execute(args)
# Clean up orphan optimized images
OptimizedImage.where("upload_id NOT IN (SELECT id FROM uploads)").destroy_all

# migrate optimized_images to new scheme
problems = OptimizedImage.migrate_to_new_scheme(50)
# Clean up optimized images that needs to be regenerated
OptimizedImage.joins(:upload)
.where("optimized_images.url NOT LIKE '%/optimized/_X/%'")
.where("uploads.url LIKE '%/original/_X/%'")
.limit(50)
.find_each do |optimized_image|

problems.each do |hash|
image = OptimizedImage.find_by(id: hash[:optimized_image].id)
image.destroy! if image
upload = optimized_image.upload
optimized_image.destroy!
upload.rebake_posts_on_old_scheme
end
end

Expand Down
66 changes: 0 additions & 66 deletions app/models/optimized_image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -350,72 +350,6 @@ def self.convert_with(instructions, to, opts = {})
false
end
end

def self.migrate_to_new_scheme(limit = nil)
problems = []

if SiteSetting.migrate_to_new_scheme
max_file_size_kb = SiteSetting.max_image_size_kb.kilobytes
local_store = FileStore::LocalStore.new

scope = OptimizedImage.includes(:upload)
.where("url NOT LIKE '%/optimized/_X/%'")
.order(id: :desc)

scope.limit(limit) if limit

scope.each do |optimized_image|
begin
# keep track of the url
previous_url = optimized_image.url.dup
# where is the file currently stored?
external = previous_url =~ /^\/\//
# download if external
if external
url = SiteSetting.scheme + ":" + previous_url
file = FileHelper.download(
url,
max_file_size: max_file_size_kb,
tmp_file_name: "discourse",
follow_redirect: true
) rescue nil
path = file.path
else
path = local_store.path_for(optimized_image)
file = File.open(path)
end
# compute SHA if missing
if optimized_image.sha1.blank?
optimized_image.sha1 = Upload.generate_digest(path)
end
# optimize if image
FileHelper.optimize_image!(path)
# store to new location & update the filesize
File.open(path) do |f|
optimized_image.url = Discourse.store.store_optimized_image(f, optimized_image)
optimized_image.save
end
# remap the URLs
DbHelper.remap(UrlHelper.absolute(previous_url), optimized_image.url) unless external
DbHelper.remap(previous_url, optimized_image.url)
# remove the old file (when local)
unless external
FileUtils.rm(path, force: true)
end
rescue => e
problems << { optimized_image: optimized_image, ex: e }
# just ditch the optimized image if there was any errors
optimized_image.destroy
ensure
file&.close
file&.unlink if file&.respond_to?(:unlink)
end
end
end

problems
end

end

# == Schema Information
Expand Down
9 changes: 9 additions & 0 deletions app/models/upload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,9 @@ def self.migrate_to_new_scheme(limit = nil)
# remap the URLs
DbHelper.remap(UrlHelper.absolute(previous_url), upload.url) unless external
DbHelper.remap(previous_url, upload.url)

upload.optimized_images.find_each(&:destroy!)
upload.rebake_posts_on_old_scheme
# remove the old file (when local)
unless external
FileUtils.rm(path, force: true)
Expand All @@ -288,6 +291,12 @@ def self.migrate_to_new_scheme(limit = nil)
problems
end

private

def rebake_posts_on_old_scheme
self.posts.where("cooked LIKE '%/_optimized/%'").find_each(&:rebake!)
end

end

# == Schema Information
Expand Down

0 comments on commit feb731b

Please sign in to comment.