diff options
author | Tomeu Vizoso <tomeu@sugarlabs.org> | 2009-01-07 12:42:40 (GMT) |
---|---|---|
committer | Tomeu Vizoso <tomeu@sugarlabs.org> | 2009-01-07 12:42:40 (GMT) |
commit | baeeace5dc3cf874f78435726dab0c3f3435a300 (patch) | |
tree | 4809d49135621f4d4f2e1a003ebc8a4a495819cb /src/jarabe/journal/model.py | |
parent | d74afc73928d33200c917f74efc7775a2df63a87 (diff) |
Sanitize the file name when we copy to removable devices
Diffstat (limited to 'src/jarabe/journal/model.py')
-rw-r--r-- | src/jarabe/journal/model.py | 39 |
1 files changed, 35 insertions, 4 deletions
diff --git a/src/jarabe/journal/model.py b/src/jarabe/journal/model.py index fc890e1..19fcaf1 100644 --- a/src/jarabe/journal/model.py +++ b/src/jarabe/journal/model.py @@ -364,7 +364,10 @@ def write(metadata, file_path='', update_mtime=True): if not os.path.exists(file_path): raise ValueError('Entries without a file cannot be copied to ' 'removable devices') + file_name = _get_file_name(metadata['title'], metadata['mime_type']) + file_name = _get_unique_file_name(metadata['mountpoint'], file_name) + destination_path = os.path.join(metadata['mountpoint'], file_name) shutil.copy(file_path, destination_path) object_id = destination_path @@ -372,10 +375,38 @@ def write(metadata, file_path='', update_mtime=True): return object_id def _get_file_name(title, mime_type): - # TODO: sanitize title for common filesystems - # TODO: make as robust as possible, this function should never fail. - # TODO: don't append the same extension again and again - return '%s.%s' % (title, mime.get_primary_extension(mime_type)) + file_name = title + + extension = '.' + mime.get_primary_extension(mime_type) + if not file_name.endswith(extension): + file_name += extension + + # Invalid characters in VFAT filenames. From + # http://en.wikipedia.org/wiki/File_Allocation_Table + invalid_chars = ['/', '\\', ':', '*', '?', '"', '<', '>', '|', '\x7F'] + invalid_chars.extend([chr(x) for x in range(0, 32)]) + for char in invalid_chars: + file_name = file_name.replace(char, '_') + + # FAT limit is 255, leave some space for uniqueness + max_len = 250 + if len(file_name) > max_len: + name, extension = os.path.splitext(file_name) + file_name = name[0:max_len - extension] + extension + + return file_name + +def _get_unique_file_name(mount_point, file_name): + if os.path.exists(os.path.join(mount_point, file_name)): + i = 1 + while len(file_name) <= 255: + name, extension = os.path.splitext(file_name) + file_name = name + '_' + str(i) + extension + if not os.path.exists(os.path.join(mount_point, file_name)): + break + i += 1 + + return file_name created = dispatch.Signal() updated = dispatch.Signal() |