Skip to content
Advertisement

Can i have two different sets of app configurations?

I have made a flask server REST API that does an upload to a specific folder. The upload must comply to a specific file extension, and must be in a specific folder.

So i made these two app.configs:

app.config['UPLOAD_EXTENSIONS'] = ['.stp', '.step']
app.config['UPLOAD_PATH'] = 'uploads'

However, i want to make a new route, for a new upload of a specific file extension to another folder.

So can i have two sets of app.config['UPLOAD_EXTENSIONS'] and app.config['UPLOAD_PATH']?

One set will be for extension1 in folder1 and the other set for extension2 in folder2.

Advertisement

Answer

Try using the extension Flask-Uploads .

Or, proceeding from the file format, form a subdirectory in your UPLOAD_PATH.

import os


def select_directory(filename: str) -> str:
    file_format = filename.split('.')[1]
    your_path1 = 'path1'
    your_path2 = 'path2'

    if file_format in ('your format1', 'your format2'):
        full_path = os.path.join(app.config['UPLOAD_FOLDER'], your_path1, filename)
    else:
        full_path = os.path.join(app.config['UPLOAD_FOLDER'], your_path2, filename)

    return full_path

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement