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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With