Skip to content

Commit fe7ca10

Browse files
committed
Poskrbi, da se v zipu ne izgubijo datoteke z enakimi imeni
Closes #59
1 parent 10a5cca commit fe7ca10

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

nadlogar/documents/views.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,16 @@ def copy_document(request, group_id: int, document_id: int, new_group_id: int):
8686
def _zip_archive(archive_name, files):
8787
string_buffer = io.BytesIO()
8888
archive = zipfile.ZipFile(string_buffer, "w", zipfile.ZIP_DEFLATED)
89+
name_counter = {}
8990
for file_name, file_contents in files:
91+
# If there are multiple files with the same name, add a number
92+
# before the file extension.
93+
if file_name in name_counter:
94+
name_counter[file_name] += 1
95+
base_name, extension = file_name.rsplit(".", 1)
96+
file_name = f"{base_name}_{name_counter[file_name]}.{extension}"
97+
else:
98+
name_counter[file_name] = 1
9099
archive.writestr(file_name, file_contents)
91100
archive.close()
92101
response = HttpResponse(string_buffer.getvalue(), content_type="application/zip")

0 commit comments

Comments
 (0)