Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
length_in_bytes = len(encoded_name)
if length_in_bytes < 1:
raise UnusableFileName("Filename must be at least 1 character.")
if length_in_bytes > 1024:
raise UnusableFileName("Filename is too long (can be at most 1024 bytes).")
lowest_unicode_value = ord(min(filename))
if lowest_unicode_value < 32:
message = u"Filename \"{0}\" contains code {1} (hex {2:02x}), less than 32.".format(
self.unprintable_to_hex(filename), lowest_unicode_value, lowest_unicode_value
)
raise UnusableFileName(message)
# No DEL for you.
if '\x7f' in filename:
raise UnusableFileName("DEL character (0x7f) not allowed.")
if filename[0] == '/' or filename[-1] == '/':
raise UnusableFileName("Filename may not start or end with '/'.")
if '//' in filename:
raise UnusableFileName("Filename may not contain \"//\".")
long_segment = max([len(segment.encode('utf-8')) for segment in filename.split('/')])
if long_segment > 250:
raise UnusableFileName("Filename segment too long (maximum 250 bytes in utf-8).")
lowest_unicode_value = ord(min(filename))
if lowest_unicode_value < 32:
message = u"Filename \"{0}\" contains code {1} (hex {2:02x}), less than 32.".format(
self.unprintable_to_hex(filename), lowest_unicode_value, lowest_unicode_value
)
raise UnusableFileName(message)
# No DEL for you.
if '\x7f' in filename:
raise UnusableFileName("DEL character (0x7f) not allowed.")
if filename[0] == '/' or filename[-1] == '/':
raise UnusableFileName("Filename may not start or end with '/'.")
if '//' in filename:
raise UnusableFileName("Filename may not contain \"//\".")
long_segment = max([len(segment.encode('utf-8')) for segment in filename.split('/')])
if long_segment > 250:
raise UnusableFileName("Filename segment too long (maximum 250 bytes in utf-8).")
:param filename: a proposed filename in unicode
:return: None if the filename is usable
"""
encoded_name = filename.encode('utf-8')
length_in_bytes = len(encoded_name)
if length_in_bytes < 1:
raise UnusableFileName("Filename must be at least 1 character.")
if length_in_bytes > 1024:
raise UnusableFileName("Filename is too long (can be at most 1024 bytes).")
lowest_unicode_value = ord(min(filename))
if lowest_unicode_value < 32:
message = u"Filename \"{0}\" contains code {1} (hex {2:02x}), less than 32.".format(
self.unprintable_to_hex(filename), lowest_unicode_value, lowest_unicode_value
)
raise UnusableFileName(message)
# No DEL for you.
if '\x7f' in filename:
raise UnusableFileName("DEL character (0x7f) not allowed.")
if filename[0] == '/' or filename[-1] == '/':
raise UnusableFileName("Filename may not start or end with '/'.")
if '//' in filename:
raise UnusableFileName("Filename may not contain \"//\".")
long_segment = max([len(segment.encode('utf-8')) for segment in filename.split('/')])
if long_segment > 250:
raise UnusableFileName("Filename segment too long (maximum 250 bytes in utf-8).")
def check_b2_filename(self, filename):
"""
Raise an appropriate exception with details if the filename is unusable.
See https://www.backblaze.com/b2/docs/files.html for the rules.
:param filename: a proposed filename in unicode
:return: None if the filename is usable
"""
encoded_name = filename.encode('utf-8')
length_in_bytes = len(encoded_name)
if length_in_bytes < 1:
raise UnusableFileName("Filename must be at least 1 character.")
if length_in_bytes > 1024:
raise UnusableFileName("Filename is too long (can be at most 1024 bytes).")
lowest_unicode_value = ord(min(filename))
if lowest_unicode_value < 32:
message = u"Filename \"{0}\" contains code {1} (hex {2:02x}), less than 32.".format(
self.unprintable_to_hex(filename), lowest_unicode_value, lowest_unicode_value
)
raise UnusableFileName(message)
# No DEL for you.
if '\x7f' in filename:
raise UnusableFileName("DEL character (0x7f) not allowed.")
if filename[0] == '/' or filename[-1] == '/':
raise UnusableFileName("Filename may not start or end with '/'.")
if '//' in filename:
raise UnusableFileName("Filename may not contain \"//\".")
long_segment = max([len(segment.encode('utf-8')) for segment in filename.split('/')])
if long_segment > 250:
raise UnusableFileName("Filename segment too long (maximum 250 bytes in utf-8).")
def check_b2_filename(self, filename):
"""
Raise an appropriate exception with details if the filename is unusable.
See https://www.backblaze.com/b2/docs/files.html for the rules.
:param filename: a proposed filename in unicode
:return: None if the filename is usable
"""
encoded_name = filename.encode('utf-8')
length_in_bytes = len(encoded_name)
if length_in_bytes < 1:
raise UnusableFileName("Filename must be at least 1 character.")
if length_in_bytes > 1024:
raise UnusableFileName("Filename is too long (can be at most 1024 bytes).")
lowest_unicode_value = ord(min(filename))
if lowest_unicode_value < 32:
message = u"Filename \"{0}\" contains code {1} (hex {2:02x}), less than 32.".format(
self.unprintable_to_hex(filename), lowest_unicode_value, lowest_unicode_value
)
raise UnusableFileName(message)
# No DEL for you.
if '\x7f' in filename:
raise UnusableFileName("DEL character (0x7f) not allowed.")
if filename[0] == '/' or filename[-1] == '/':
raise UnusableFileName("Filename may not start or end with '/'.")
if '//' in filename:
raise UnusableFileName("Filename may not contain \"//\".")
long_segment = max([len(segment.encode('utf-8')) for segment in filename.split('/')])
"""
encoded_name = filename.encode('utf-8')
length_in_bytes = len(encoded_name)
if length_in_bytes < 1:
raise UnusableFileName("Filename must be at least 1 character.")
if length_in_bytes > 1024:
raise UnusableFileName("Filename is too long (can be at most 1024 bytes).")
lowest_unicode_value = ord(min(filename))
if lowest_unicode_value < 32:
message = u"Filename \"{0}\" contains code {1} (hex {2:02x}), less than 32.".format(
self.unprintable_to_hex(filename), lowest_unicode_value, lowest_unicode_value
)
raise UnusableFileName(message)
# No DEL for you.
if '\x7f' in filename:
raise UnusableFileName("DEL character (0x7f) not allowed.")
if filename[0] == '/' or filename[-1] == '/':
raise UnusableFileName("Filename may not start or end with '/'.")
if '//' in filename:
raise UnusableFileName("Filename may not contain \"//\".")
long_segment = max([len(segment.encode('utf-8')) for segment in filename.split('/')])
if long_segment > 250:
raise UnusableFileName("Filename segment too long (maximum 250 bytes in utf-8).")
raise UnusableFileName("Filename must be at least 1 character.")
if length_in_bytes > 1024:
raise UnusableFileName("Filename is too long (can be at most 1024 bytes).")
lowest_unicode_value = ord(min(filename))
if lowest_unicode_value < 32:
message = u"Filename \"{0}\" contains code {1} (hex {2:02x}), less than 32.".format(
self.unprintable_to_hex(filename), lowest_unicode_value, lowest_unicode_value
)
raise UnusableFileName(message)
# No DEL for you.
if '\x7f' in filename:
raise UnusableFileName("DEL character (0x7f) not allowed.")
if filename[0] == '/' or filename[-1] == '/':
raise UnusableFileName("Filename may not start or end with '/'.")
if '//' in filename:
raise UnusableFileName("Filename may not contain \"//\".")
long_segment = max([len(segment.encode('utf-8')) for segment in filename.split('/')])
if long_segment > 250:
raise UnusableFileName("Filename segment too long (maximum 250 bytes in utf-8).")