Wednesday, 1 August 2018

Saving and sending back files elegantly with blueprints/config

I have the following file structure:

.
├── app
│   ├── api_routes
│   │   ├── forms.py
│   │   ├── __init__.py
│   │   └── routes.py
│   ├── __init__.py
│   ├── main_routes
│   │   ├── forms.py
│   │   ├── __init__.py
│   │   └── routes.py
│   ├── models.py
│   ├── static
│   │   └── styles.css
│   ├── templates
│   │   └── base.html
│   └── uploads
│       └── 10_0_0.jpg
├── application.py
└── config.py

In my config.py I have this:

class Config(object):
    UPLOAD_FOLDER = 'uploads/'

When I'm saving an uploaded file, and then sending it back to the user (just as example) I'm using:

fname = 'foo.jpg'
fname_save = os.path.join(current_app.root_path, current_app.config['UPLOAD_FOLDER'], fname)
fname_retr = os.path.join(current_app.config['UPLOAD_FOLDER'], fname)
file.save(fname_save)
return send_from_directory(os.path.dirname(fname_retr),
                           os.path.basename(fname_retr))

Having different names for the upload folder in the cwd (where the file is saved), and the folder the flask module is running (app/), is a bit tedious. Are there any more elegant solutions than my current one to solve this issue?



from Saving and sending back files elegantly with blueprints/config

No comments:

Post a Comment