general03 / flask-autoindex

Generates index page like mod_autoindex
https://flask-autoindex.readthedocs.io
MIT License
114 stars 34 forks source link

How to set mimetype based on file extension #56

Closed t-i-r closed 4 years ago

t-i-r commented 4 years ago

Hi -

I would like to set MIME for e.g. yaml documents to text/plain so that yaml files can be directly viewed in the web browser rather than downloaded.

I tried e.g.

idx.add_icon_rule('my_yaml_icon.png', ext='yaml', mimetype='text/plain')

but the response header returned by flask still defaults to application/octet-stream. Can you please share example how to properly set mime based on file extension?

Thank you, TIR

general03 commented 4 years ago

Hi,

The add_icon_rule method enable to display the icon choose for extension OR mimetype. It does not set the mimetype

To do this you need to force the Content-Type header with Flask like

resp = make_response(idx.render_autoindex('.'))
resp.headers['Content-Type'] = 'text/plain'
return resp