Skip to content

Commit b3028fa

Browse files
committed
Safer filepath truncation
1 parent c1fae7e commit b3028fa

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

minet/fs.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,13 @@ def __call__(self, filename, url, **kwargs):
140140
# NOTE: dealing with typical max filename length
141141
for i in range(len(final_path)):
142142
item = final_path[i]
143+
item_bytes = item.encode()
143144

144-
if len(item) > 255:
145-
final_path[i] = item[:255]
145+
if len(item_bytes) > 255:
146+
try:
147+
final_path[i] = item_bytes[:255].decode()
148+
except UnicodeDecodeError as e:
149+
final_path[i] = item_bytes[: e.start].decode()
146150

147151
return join(os.sep.join(final_path), filename)
148152

0 commit comments

Comments
 (0)