Search code examples
ruby-on-railsrubymicrosoft-edgerefile

RoR/Refile Gem - Images not loading in Microsoft Edge


We are using the refile gem to display the images in our platform, they do work well on different browsers except for Microsoft Edge. Is there a different format or limitation for Microsoft Edge that I should know about it?

(I don't have Microsoft Edge, so can't test it directly)

Any help would be amazing. Thanks.


Solution

  • I've checked with MS Edge 25.10586.0.0 / EdgeHTML 13.10586 and images have not been displayed.

    I suppose this happens because images are sent as application/octet-stream and Edge doesn't have enough informations to display them (need to be confirmed).

    But on refile github page you could see it's possible to add metadata for each file loaded like:

    class StoreMetadata < ActiveRecord::Migration
      def change
        add_column :users, :profile_image_filename, :string
        add_column :users, :profile_image_size, :integer
        add_column :users, :profile_image_content_type, :string
      end
    end
    

    These fields will be filled automatically after a file load and fixes the issue on my refile example app.

    Disclaimer: Be carefull with the following actions, please make some tests before doing this on a production environment

    It's possible to add missing informations to your existing files.

    Currently Refile seems to only use filename extension to extract the content-type. Therefore we need to extract the content-type with file content and create a filename with the corresponding extension for each uploaded file.

    There is probably many ways to do that. I'll describe a method I used on my refile application.

    Here is my user model

    class User < ActiveRecord::Base
      attachment :profile_image
    end
    

    First run the previous migration to add missing fields.

    In the gemfile add the gem mimemagic and run bundel install. This one can determine content-type of a file by the content.

    Then for each User extract profile_image's content-type and add a correct filename.

    User.all.each do |u|
      subtype = MimeMagic.by_magic(u.profile_image.read).subtype
      u.profile_image_filename = "profile_image.#{subtype}" if u.profile_image_filename.nil?
      u.save
     end
    

    And that's all.