Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

send_file calls max_age with Path instead of str #2119

Closed
tunetheweb opened this issue May 12, 2021 · 4 comments · Fixed by #2121
Closed

send_file calls max_age with Path instead of str #2119

tunetheweb opened this issue May 12, 2021 · 4 comments · Fixed by #2121
Labels
Milestone

Comments

@tunetheweb
Copy link

We use the following code to override the SEND_FILE_MAX_AGE_DEFAULT for certain file types:

# Set WOFF and WOFF2 caching to return 1 year as they should never change
class MyServer(Flask):
    def get_send_file_max_age(self, name):
        if name.lower().endswith('.woff') or name.lower().endswith('.woff2'):
            return 31536000
        return Flask.get_send_file_max_age(self, name)


# Initialize The Server
app = MyServer(__name__, template_folder=TEMPLATES_DIR, static_folder=STATIC_DIR)

As of the newly released Flask 2.0 this is failing with the following error:

  File "/Users/barry/almanac.httparchive.org/src/server/__init__.py", line 19, in get_send_file_max_age
    if filename.lower().endswith('.woff') or filename.lower().endswith('.woff2'):
AttributeError: 'PosixPath' object has no attribute 'lower'

So we have to change this to this to convert the name to a string:

# Set WOFF and WOFF2 caching to return 1 year as they should never change
class MyServer(Flask):
    def get_send_file_max_age(self, name):
        if str(name).lower().endswith('.woff') or str(name).lower().endswith('.woff2'):
            return 31536000
        return Flask.get_send_file_max_age(self, name)


# Initialize The Server
app = MyServer(__name__, template_folder=TEMPLATES_DIR, static_folder=STATIC_DIR)

Is this an intended behaviour change as the documentation still says this uses a string as an argument rather than a PosixPath?

Environment:

  • Python version: 3.8
  • Flask version: 2.0
@davidism
Copy link
Member

davidism commented May 12, 2021

You'll need to call name = os.fspath(name) first if your code only expects to be working with strings and not Path objects.

@tunetheweb
Copy link
Author

Then should the documentation be updated to note it's now a Path object and not a string: https://flask.palletsprojects.com/en/2.0.x/api/#flask.Flask.get_send_file_max_age

Parameters: filename (str) –

@davidism davidism transferred this issue from pallets/flask May 12, 2021
@davidism davidism changed the title get_send_file_max_age parameters changed in Flask 2.0 send_file calls max_age with Path instead of str May 12, 2021
@davidism
Copy link
Member

Note that your implementation already has an issue, as get_send_file_max_age could be called with None when sending file objects instead of paths. I'll change send_file to always pass a string or None, and update the annotations too.

@davidism davidism reopened this May 12, 2021
@tunetheweb
Copy link
Author

Good spot on the None issue. Will update my code.

BTW I was happy to leave as Path but just wanted to check it was an intentional change. Then again I'm probably not the only one who will experience this as that code snippet is all over Stack Overflow (think that's where I got it from!) so maybe best to revert as you say.

Thanks for the quick response btw!

@davidism davidism mentioned this issue May 13, 2021
3 tasks
@davidism davidism added this to the 2.0.1 milestone May 13, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 28, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants