Skip to content

Commit 12a2f8f

Browse files
authored
Accept a str UPLOAD_DIR when UPLOAD_KEEP_FILENAME is set (#335)
1 parent 95f6376 commit 12a2f8f

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

‎python_multipart/multipart.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ def _get_disk_file(self) -> BufferedRandom:
539539
# TODO: what happens if we don't have a filename?
540540
fname = self._file_base + self._ext if keep_extensions else self._file_base
541541

542-
path = os.path.join(file_dir, fname) # type: ignore[arg-type]
542+
path = os.path.join(os.fsencode(file_dir), fname)
543543
try:
544544
self.logger.info("Opening file: %r", path)
545545
tmp_file = open(path, "w+b")

‎tests/test_file.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,21 @@ def test_upload_dir_with_leading_slash_in_filename(tmp_path: Path) -> None:
2424
assert not file.in_memory
2525
assert Path(upload_dir / "foo.txt").exists()
2626
assert Path(upload_dir / "foo.txt").read_bytes() == b"123456789012"
27+
28+
29+
def test_str_upload_dir_with_keep_filename(tmp_path: Path) -> None:
30+
file = File(
31+
b"foo.txt",
32+
config={
33+
"UPLOAD_DIR": str(tmp_path),
34+
"UPLOAD_KEEP_FILENAME": True,
35+
"UPLOAD_KEEP_EXTENSIONS": True,
36+
"MAX_MEMORY_FILE_SIZE": 10,
37+
},
38+
)
39+
file.write(b"123456789012")
40+
file.close()
41+
42+
assert not file.in_memory
43+
assert file.actual_file_name == b"foo.txt"
44+
assert (tmp_path / "foo.txt").read_bytes() == b"123456789012"

0 commit comments

Comments
 (0)