0

My python/flask app hosted on heroku (linux environment) encounters some issues when I upload a zip file from a windows client and trying to extract it. I get the following error :

Traceback (most recent call last):
2022-05-27T18:53:45.200198+00:00 app[web.1]:   File "/app/app.py", line 2884, in verify_file
2022-05-27T18:53:45.200199+00:00 app[web.1]:     with zipfile.ZipFile(file_path, mode="r") as zf:
2022-05-27T18:53:45.200199+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/zipfile.py", line 1131, in __init__
2022-05-27T18:53:45.200199+00:00 app[web.1]:     self._RealGetContents()
2022-05-27T18:53:45.200202+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/zipfile.py", line 1216, in _RealGetContents
2022-05-27T18:53:45.200202+00:00 app[web.1]:     fp.seek(self.start_dir, 0)
2022-05-27T18:53:45.200202+00:00 app[web.1]: OSError: [Errno 22] Invalid argument

this error is triggered when my app trying to unzip the uploaded file :

ROOT_DIR_FOR_TEMPORARY_FILES = 'tmp'
if not os.path.exists(ROOT_DIR_FOR_TEMPORARY_FILES):
        os.mkdir(ROOT_DIR_FOR_TEMPORARY_FILES)
    file_uuid = uuid.uuid4().hex
    file_dir = os.path.join(ROOT_DIR_FOR_TEMPORARY_FILES,file_uuid)
    file_path = os.path.join(ROOT_DIR_FOR_TEMPORARY_FILES,file_uuid+".zip")
 
    with open(file_path, "wb") as binary_file:
        binary_file.write(file_content)
      
    
with zipfile.ZipFile(file_path, mode="r") as zf:
          zf.extractall(ROOT_DIR_FOR_TEMPORARY_FILES+"/"+file_uuid)
          zf.close()

Yet, this works without problem when I do it from a Mac OS client machine. I don't know why this error occurs when I upload from Windows.

I am using request.data to get the content of the uploaded file and then pass to file_content.

Any suggestion ?

John doe
  • 3,680
  • 7
  • 31
  • 65
  • So, seems like the `file_path` arg is invalid, at least in terms of being passed to `ZipFile()`. what's actually in it? – Random Davis May 27 '22 at 19:16
  • @RandomDavis the `file_path` looks like this when I do the `print` : `tmp/b182de7b1c694cc7b8f0b690088907ff.zip` . I don't understand why it's invalid, yet it works without problem when uploading from `Mac OS` client. – John doe May 27 '22 at 19:19
  • What if you build the full, explicit path and pass that in, rather than just the relative path? – Random Davis May 27 '22 at 19:23
  • How can I do that ? The problem is the app must be able to handle all types of clients : Mac OS, Windows and Linux. I am not sure how I can write a full path that handle the three. Any suggestion ? – John doe May 27 '22 at 19:26
  • [this post](https://stackoverflow.com/questions/918154/relative-paths-in-python) seems to cover that; that should answer your question about a platform-agnostic way of building paths. – Random Davis May 27 '22 at 19:27
  • Just tried the above solution with `dirname` but still doesn't work. Now I have a file_path that looks like `/app/tmp/c1ac28aeff144eaf8f4125bfff96b683.zip`, but still the same error. – John doe May 27 '22 at 19:52
  • Yeah that's still not the full path obvious as it doesn't contain a drive letter. I think you'll just have to do some research and/or follow tutorials about paths in Python. – Random Davis May 27 '22 at 20:04

0 Answers0