Static files are not being served

This is my usual static serving lines of code.

app.static("/static/videos/", "./static/videos/", use_content_range=True)
app.static("/static/documents/", "./static/documents/")
app.static("/static/images/", "./static/images/")

This works perfectly fine on my Ubuntu, but today I tried the same code on my windows machine and it’s not working.

All the files are exactly in the same directories, in the same hierarchy, with no changes to code.

As a workaround to server those files, I am doing this

img_file_names = tuple(*[files for (_, _, files) in os.walk('./static/images/')])

@app.get("/static/images/<img_file_name:path>")
async def serve_img(_, img_file_name):
    if img_file_name in img_file_names:
        return await file('./static/images/' + img_file_name)

    return empty()

I am doing the same for videos and document files

The above code works perfectly fine both on Ubuntu and windows but the first static config is much more convenient.

What’s the reason it’s not working? Thanks in advance :heart:

I’m not sure. This is a new one to me. When you say it doesn’t work, what is the behavior that you observe?

The files which are meant to be sent from server to browser are not being found. 404.

Take a look at the image I uploaded. The .mp4 and .png, .jpg files are 404 (not found).

I don’t know if I made sense in this reply :sweat_smile:

The manual code I wrote works with images and other files, but I need to support range byte header and I don’t know how to do it in the manual code.

This is the manual code

This is what I used to do for videos

Using the use_content_range=True attribute, I achieved the range byte header but I don’t know how can I achieve the same through manual code

and another issue I faced.

This is my JS file. When you gzip it (as we discussed before) using the following code, it works fine in ubuntu, no errors are thrown.

But when I do the same on windows, this error is thrown.

Why TF works on Ubuntu and not on windows :joy: :joy:. This is not related to sanic, I know, but any advice would be awesome :heart:

Check your versions and let me know. I’m AFK, but I’ll try and remember to send you a snippet for the “manual” method later. But take a look at file_stream

Thank you for guiding me, file_streaming works :+1:

My Sanic version is 21.3.4

Can you look at this too please :slightly_smiling_face:

UPDATE: reading the file in utf8 solved the problem. :+1:

1 Like

Thank you very much for your answer. I used app before The static () method always reports a 404 error when registering static resources. Your code successfully solves my problem. Thanks again!

1 Like