Question

I have a following situation. I created a simple backend in Flask that handles file uploads. With files received, Flask does something (uploads them), and returns the data to the caller. There are two scenarios with the app, to upload one image and multiple images. When uploading one image, I can simply get the response and voila, I'm all set.

However, I am stuck on handling multiple file uploads. I can use the same handler for the actual file upload, but the issue is that all of those files need to be stored into a list or something, then processed, and after doing that, a single link (album) containing all those images, needs to be delivered.

Here is my upload handling code:

@app.route('/uploadv3', methods=['POST'])
def upload():
    if request.method == 'POST':
        data_file = request.files["file"]
        file_name = data_file.filename
        path_to_save_to = os.path.join(app.config['UPLOAD_FOLDER'], file_name)
        data_file.save(path_to_save_to)
        file_url = upload_image_to_image_host(path_to_save_to)
        return file_url

I was experimenting with session in flask, but I dont know can I create a list of items under one key, like session['links'], and then get all those, and clear it after doing the work. Or is there some other simpler solution?

I assume that I could probably do this via key for each image, like session["link1"], and so on, but that would impose a limit on the images (depending on how much of those I create), would make the code very ugly, make the iteration over each in order to generate a list that is passed to an album building method problematic, and session clearing would be tedious.

Some code that I wrote for getting the actual link at the end and clearing the session follows (this assume that session['link'] has a list of urls, which I can't really achieve with my knowledge of session management in Flask:

def create_album(images):
    session.pop('link', None)
    new_album = im.create_album(images)
    return new_album.link

@app.route('/get_album_link')
def get_album_link():
    return create_album(session['link'])

Thanks in advance for your time!

Was it helpful?

Solution 2

To clarify what I have done to make this work. At the end, it appeared that the uploading images and adding them to the album anonymously had to be done "reversely", so not adding images to an album object, but uploading an image object to an album id.

I made a method that gets the album link and puts it in the session: @app.route('/get_album_link')

def get_album_link():
    im = pyimgur.Imgur(CLIENT_ID)
    new_album = im.create_album()
    session.clear()
    session['album'] = new_album.deletehash
    session['album_link'] = new_album.link
    return new_album.link

Later on, when handling uploads, I just add the image to the album and voila, all set :)

uploaded_image = im.upload_image(path_of_saved_image, album=session['album'])
file_url = uploaded_image.link
return file_url

One caveat is that the image should be added to the "deleteahash" value passed as the album value, not the album ID (which is covered by the imgur api documentation).

OTHER TIPS

You can assign anything to a session including individual value or list/dictionary etc. If you know the links, you can store them in the session as follows:

session['links'] = ['link1','link2'...and so on]

This way, you have a list of all the links. You can now access a link by:

if 'links' in session:
    for link in session['links']:
        print link

Once you are done with them, you can clear the session as:

if 'links' in session:
    del session['links']
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top