Skip to content

Conversation

@metachris
Copy link
Contributor

Try to auto-detect mime-type based on filename.

@metachris
Copy link
Contributor Author

What do you think about this?

Browsers are complaining if we send eg. css files without the correct content type. This auto-detect is particularly useful if serving static files like this:

@app.route('/webfiles-static/<fn>')
async def images(req, resp, fn):
    await resp.send_file('webfiles-static/{}'.format(fn))

@belyalov
Copy link
Owner

Hey @metachris

Sorry for late reply - got some Christmas related mess last weeks.. ))

I was considering adding this before - the problem was increased footprint, and, as I remember it would work for esp8266, but leaving only feeew of RAM free..

Since it is not widely used I decided not to implement it.

P.S. For the same reason I've omitted adding response status codes like (200 OK, 404 Not Found).

@metachris
Copy link
Contributor Author

I understand. Didn't know tinyweb code itself already pushes the limit so far :)

@keredson
Copy link
Contributor

keredson commented Mar 1, 2022

the code length memory issue is (thankfully) likely to soon be a thing of the past: micropython/micropython#8191 perhaps we can revisit this then?

@eyJhb
Copy link
Contributor

eyJhb commented Jan 5, 2023

It would be nice, if this could be a separate function, that could be used for detection the mime type based on a filename.
The reason to put as an external function, would be in the case of gzipped files, ie. main.js.gz.

contentType = web.guessContentType(filename)
res.send_file(filename, content_type = contentType)

Something like the above usecase.

Also, would be nice to use filename.rfind(b'.', 1)[-1] instead, as that would decrease how much of the string is searched.
Besides this, all the . in front of the MIME_TYPES_PER_EXT could be removed, so that string concatenation is not required.

@keredson luckily it is merged and now a thing of the past AFAIK :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants