anggrayudi / anggrayudi/SimpleStorage

CreateMode.REPLACE not respected on malformed input

Đang mở
#159 0 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
Kotlin
Star
867
Fork
104
Merge trung bình
22 ngày 6 giờ
Pull request đã merge (30 ngày)
1

Mô tả

CreateMode.REPLACE not respected on malformed input

When testing MediaStoreCompat.createDownload I found a bug caused by the OS renaming the name when it has invalid input.

Assume you have some regular function to create a file.

```kotlin
fun testDuplicate(context : Context) {
val file = MediaStoreCompat.createDownload(
context = context,
file = FileDescription(
name = "hello",
"test",
mimeType = "text/plain"
),
mode = CreateMode.REPLACE
)?.toStorageFile(context)

file?.openOutputStream()?.writer()?.use { writer ->
writer.write("Hello World!")
writer.flush()
}
}
```

This just creates a file named "hello.txt" with the content "Hello World!". And if called twice it replaces the file.
```
testDuplicate(context)
testDuplicate(context)
```
-> only 1 file

This is expected behavior, and everything is fine.

However if I now choose the name = "hello?" it creates the file fine, but the OS renames it to hello_, therefore the subsequent call to testDuplicate does not replace "hello_" because it is searching for "hello?" and therefore creates a new file called "hello_(1)".

The expected behavior is to either fail or replace the file named "hello_". However the current implementation creates another file even with the mode CreateMode.REPLACE.

This issue is fixed if the createDownload function also sanitized the file name like the os with e.g. `.replace("?","_")`, however I get that this is a wonky solution. I think it is sane to either document this as an issue, sanitize the filename, or even detect the creation of the wrong file name.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.