Skip to content

Instantly share code, notes, and snippets.

@ivarne
Created December 17, 2012 07:45
Show Gist options
  • Save ivarne/4316481 to your computer and use it in GitHub Desktop.
Save ivarne/4316481 to your computer and use it in GitHub Desktop.
"""
Somewhat minimal example demonstrating how the Python dropbox api has inconcistent UTF-8 handeling
for filenames and paths.
To simpelify upload of many files i let users copy their files to a upload folder under an
application specific dropox folder named upload. Then I download theese files to my server and makes
them availible to the application.
My problem was that if filenames contains non ASCII characters like æøå I got an encoding error when
I tried to delete the file. The problem was resolved when i encoded f['path'] to utf-8, but I have
problems understanding why I have to encode for client.file_delete() but not when I use the same path
in client.get_file()
"""
def get_files_from_dropbox( dropbox_client):
"""
Utility function to download files from the users dropbox/upload folder and store them on
the server
"""
files = dropbox_client.metadata('upload')['contents']
for f in files:
fname = slugify(name)
with open(os.path.join(settings.MEDIA_ROOT, 'upload', fname), "wb+") as tmp_file:
d_file = dropbox_client.get_file(f['path'])
tmp_file.write(d_file.read())
dropbox_client.file_delete(f['path'].encode("utf-8"))
"""
This example failes if a file in /upload contains non ascii characters (like æøå)
"""
def get_files_from_dropbox( dropbox_client):
"""
Utility function to download files from the users dropbox/upload folder and store them on
the server
"""
files = dropbox_client.metadata('upload')['contents']
for f in files:
fname = slugify(name)
with open(os.path.join(settings.MEDIA_ROOT, 'upload', fname), "wb+") as tmp_file:
d_file = dropbox_client.get_file(f['path'])
tmp_file.write(d_file.read())
dropbox_client.file_delete(f['path'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment