-1

So, I'm trying to make a simple web app with Flask. My app will receive an uploaded image from user and return a zip that contains some edited images (adding some funny emojis for that image or something like that). My zip was created fine with Python. My question is: how do I generate a download url for that zip after I've generated it completely? I think storing that zip on server side is bad (just my objective thinking) because my app is public, I mean users have not to sign in or up, just upload the file and receive the zip.

I've tried to search, and some recommend that using base64, is there any way better than that?

nwice13
  • 419
  • 4
  • 11
  • You can generate a link and present it after the upload to the user so that only the user with the link can download the zip – geckos Mar 26 '19 at 10:21
  • You mean store that zip on server side with a specific name and present the url for that zip for user ? – nwice13 Mar 26 '19 at 10:38
  • Yeah, something like this. You can send it to s3 or a FTP server to. What you afraid of by saving the file in the server? – geckos Mar 26 '19 at 12:41
  • cause i think these files are just used once, storing it is a bit wasting :D – nwice13 Mar 26 '19 at 12:46

1 Answers1

0

You can create the zip file directly during render time. The request time will suffer however.

It may be possible to use a kind of middleware, but I can not tell you.

Maybe you return a JWT token containing the address of the requested image. Upon request, the zip archive is created. Of course you can also pass the image address as a POST or GET parameter.

@app.route('/get-image')
def get_image():
  image_file = request.files['image_addr']

  # load and process image here
  image_data = ...

  # generate zip file 
  mem = io.BytesIO()
  mem.write(gzip.compress(image_data))
  mem.seek(0)
  return send_file(mem, as_attachment=True,
                     attachment_filename='archive.gzip',
                     mimetype='application/gzip')

Is that what you are looking for?

nwice13
  • 419
  • 4
  • 11
  • Quite like this, but it worked for me if your code should replace request.form with 'request.files', and replace buffer with 'mem'. Thanks for help! – nwice13 Mar 26 '19 at 14:07