Generating missing optimized images with S3

Team, I got stucked in the situation related with the Profile Picture and it wasted around 6 hours of me :frowning: but I found the perfect solution, so sharing here.

STORAGE: Using AWS S3 with CDN in front.

Problem.1: I was able to upload the profile pic from the change profile pic area but it was not reflecting in the profile. I updated my code to the latest Discourse and it started reflecting the profile picture on the UI :slight_smile:

OLD CODE - Was not showing the profile pic


module FileStore

  class S3Store < BaseStore
     ...
    def path_for(upload)
      url = upload&.url
      if url && url[/^\/[^\/]/]
        FileStore::LocalStore.new.path_for(upload)
      else
        url
      end
    end
     ...
  end
end

SOLUTION:LATEST CODE - Started showing the profile pic for new uploads

module FileStore

  class S3Store < BaseStore
     ...
    def path_for(upload)
      url = upload&.url
      FileStore::LocalStore.new.path_for(upload) if url && url[/^\/[^\/]/]
    end
     ...
  end
end

Note: Please guys keep updating your own code with the Discourse upstream on regular basis and it will keep our code updated with security fixes and also, bug free.

Problem.2: Me and my team tried changing profile pictures before fixing the above bug mentioned in the Problem.1. And, it created lots of entries in the uploads table with unknown as the value in the extension column. The code was not correctly processing the profile picture in the image optimization process (MODEL: OptimizedImage) due to wrong s3 file path.

So, I also found a workaround to fix these type of entries those happenend before my code fix.

Solution

Multi Site
We can run the below code directly on the production rails console - or using a rake task.

RailsMultisite::ConnectionManagement.each_connection do |db|
  Upload.where(extension: "unknown").each do |upload|
    upload.update(extension: File.extname(upload.url).gsub(".",""))
  end
  OptimizedImage.destroy_all
  UserAvatar.all.each {|ua| uaid = ua.gravatar_upload_id || ua.custom_upload_id; 
  Jobs::CreateAvatarThumbnails.perform_async(upload_id: uaid) if uaid }
end

Normal - Single Site
We can run the below code directly on the production rails console - or using a rake task.

Upload.where(extension: "unknown").each do |upload|
  upload.update(extension: File.extname(upload.url).gsub(".",""))
end
OptimizedImage.destroy_all
UserAvatar.all.each {|ua| uaid = ua.gravatar_upload_id || ua.custom_upload_id; 
Jobs::CreateAvatarThumbnails.perform_async(upload_id: uaid) if uaid }

I think, it will help lots of people :slight_smile:

Thank You