Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gitlab (rails) "raw" file mime type for .svg files is 'text/plain'. Can it be configured to output as 'image/svg+xml'?

I've installed Gitlab on a local server for evaluation, and one key feature for me will be to have svg files display as images in the wiki. I have it working if I drag a file into the wiki editor, which produces a url like so:

in wiki markdown:

![my svg diagram](http://server/my-group/my-project/uploads/90cdd5d76a05957ab7cf8854c55a38b8/my-diagram.svg)

results in page html:

<img src="http://server/my-group/my-project/uploads/90cdd5d76a05957ab7cf8854c55a38b8/my-diagram.svg" alt="my svg diagram">

and in order to get that to work, i had to edit /opt/gitlab/embedded/service/gitlab-rails/config/initializers/mime_types.rb to add the line:

Mime::Type.register_alias "image/svg+xml", :svg

and that's great if i want to drag an svg file into the wiki and have it stored in this 'uploads' section of the project, BUT, what i really want to do is display an svg image from the git repository. (so that, for example a when a svg diagram changes, i dont have to hunt for it in the wiki, it will just pick it up from the master branch)

i have found that, for example, this is a link to a repo file:

http://server/my-group/my-project/raw/master/docsfolder/my-drawing.svg

however, the mime type apears to be text/plain for svg files referenced this way, and images with that as their src won't display. Is there a way to make them image/svg+xml? it seems the mime_types.rb config file does not affect this "raw" output. or is there another way i can link to the repo file and get the specified mime type?

i also tried <object type="image/svg+xml" data="... and <embed type="image/svg+xml" src=" and even iframe, but none of those work either.

Note: it does work great for jpg images from the repo, just not svg images.

like image 727
101chris Avatar asked May 01 '15 05:05

101chris


2 Answers

Found it. seems the content type is set in ruby code in the raw_controller.rb file. so i edit

/opt/gitlab/embedded/service/gitlab-rails/app/controllers/projects/raw_controller.rb

change this:

  def get_blob_type
    if @blob.text?
      'text/plain; charset=utf-8'
    else
      'application/octet-stream'
    end
  end

to something like this:

  def get_blob_type
    extn = File.extname(@blob.name).downcase
    if @blob.text?
      if extn == ".svg"
        'image/svg+xml'
      else 
        'text/plain; charset=utf-8'
      end
    else
      case extn
        when ".jpg", ".jpeg"
          'image/jpeg'
        when ".gif"
          'image/gif'
        when ".png"
          'image/png'
        when ".bmp"
          'image/bmp'
        when ".tiff"
           'image/tiff'
        else
          'application/octet-stream'
      end
    end
  end

then sudo gitlab-ctrl restart

now in my markdown, if i do this:

![my-diagram](http://server/my-group/my-project/raw/master/docsfolder/my-drawing.svg)

it works!

I've never used ruby before, so maybe there's a better way, but so far works for me.

like image 129
101chris Avatar answered Nov 12 '22 15:11

101chris


101chris's proposal works fine on 7.x, but needs some rework on gitlab 8.x. Here's my update tested on 8.14.4:

/opt/gitlab/embedded/service/gitlab-rails/app/helpers/blob_helper.rb :

  def safe_content_type(blob)
    if blob.text?
      case File.extname(blob.name).downcase
      when '.html'
        'text/html'
      when '.css'
        'text/css'
      when '.xml'
        'text/xml'
      when '.js'
        'application/javascript'
      else
        'text/plain; charset=utf-8'
      end
    elsif blob.image?
      blob.content_type
    else
      'application/octet-stream'
    end
  end
like image 43
Derivation Bud Avatar answered Nov 12 '22 13:11

Derivation Bud