# Generating missing optimized images with S3

**URL:** <https://meta.discourse.org/t/generating-missing-optimized-images-with-s3/35847>\
**Category:** Product Support\
**Created:** [20 november 2015 om 17:24 UTC](https://meta.discourse.org/t/generating-missing-optimized-images-with-s3/35847 "2015-11-20T17:24:35Z")\
**Posts on this page:** 1\
**Showing post:** 9

<div class="post-metadata">

**Author:** ![ashishprajapati](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ashishprajapati/32/179871_2.png) [@ashishprajapati](https://meta.discourse.org/u/ashishprajapati)\
**Post date:** [9 mei 2020 om 22:26 UTC](https://meta.discourse.org/t/generating-missing-optimized-images-with-s3/35847/9 "2020-05-09T22:26:53Z")

</div>

> [@supermathie](#):
>
> I’m not sure if the following is complete, but this rails console command removes all optimized images then regenerates the ones used for avatars:
> 
> ```plaintext
> 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 }
> 
> ```

Team, I got stucked in the situation related with the **Profile Picture** and it wasted around 6 hours of me ☹ 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 🙂

**OLD CODE - Was not showing the profile pic**

```plaintext

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**

```plaintext
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.

```plaintext
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.

```plaintext
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 🙂

Thank You

---

_[View the full topic](https://meta.discourse.org/t/generating-missing-optimized-images-with-s3/35847)._
