-
Notifications
You must be signed in to change notification settings - Fork 731
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8925 from christianrowlands/bugfix/cmr/extended-c…
…haracter-filename #6449 Extended file name support to include characters from multiple languages, including Cyrillic and Han scripts
- Loading branch information
Showing
4 changed files
with
170 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Extended file name support to include characters from multiple languages, including Cyrillic and Han scripts. ([#6449](https://github.com/element-hq/element-android/issues/6449)) |
121 changes: 121 additions & 0 deletions
121
matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/internal/util/FileUtilTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* | ||
* Copyright (c) 2024 The Matrix.org Foundation C.I.C. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.matrix.android.sdk.internal.util | ||
|
||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.matrix.android.sdk.InstrumentedTest | ||
import org.matrix.android.sdk.internal.session.DefaultFileService.Companion.DEFAULT_FILENAME | ||
import org.matrix.android.sdk.internal.util.file.safeFileName | ||
|
||
/** | ||
* These tests are run on an Android device because they need to use the static | ||
* MimeTypeMap#getSingleton() method, which was failing in the unit test directory. | ||
*/ | ||
@RunWith(AndroidJUnit4::class) | ||
class FileUtilTest : InstrumentedTest { | ||
|
||
@Test | ||
fun shouldReturnOriginalFilenameWhenValidCharactersAreUsed() { | ||
val fileName = "validFileName.txt" | ||
val mimeType = "text/plain" | ||
val result = safeFileName(fileName, mimeType) | ||
assertEquals("validFileName.txt", result) | ||
} | ||
|
||
@Test | ||
fun shouldReplaceInvalidCharactersWithUnderscores() { | ||
val fileName = "invalid/filename:with*chars?.txt" | ||
val mimeType = "text/plain" | ||
val result = safeFileName(fileName, mimeType) | ||
assertEquals("invalid/filename_with_chars_.txt", result) | ||
} | ||
|
||
@Test | ||
fun shouldAllowCyrillicCharactersInTheFilename() { | ||
val fileName = "тестовыйФайл.txt" | ||
val mimeType = "text/plain" | ||
val result = safeFileName(fileName, mimeType) | ||
assertEquals("тестовыйФайл.txt", result) | ||
} | ||
|
||
@Test | ||
fun shouldAllowHanCharactersInTheFilename() { | ||
val fileName = "测试文件.txt" | ||
val mimeType = "text/plain" | ||
val result = safeFileName(fileName, mimeType) | ||
assertEquals("测试文件.txt", result) | ||
} | ||
|
||
@Test | ||
fun shouldReturnDefaultFilenameWhenInputIsNull() { | ||
val fileName = null | ||
val mimeType = "text/plain" | ||
val result = safeFileName(fileName, mimeType) | ||
assertEquals("$DEFAULT_FILENAME.txt", result) | ||
} | ||
|
||
@Test | ||
fun shouldAddTheCorrectExtensionWhenMissing() { | ||
val fileName = "myDocument" | ||
val mimeType = "application/pdf" | ||
val result = safeFileName(fileName, mimeType) | ||
assertEquals("myDocument.pdf", result) | ||
} | ||
|
||
@Test | ||
fun shouldReplaceInvalidCharactersAndAddTheCorrectExtension() { | ||
val fileName = "my*docu/ment" | ||
val mimeType = "application/pdf" | ||
val result = safeFileName(fileName, mimeType) | ||
assertEquals("my_docu/ment.pdf", result) | ||
} | ||
|
||
@Test | ||
fun shouldNotModifyTheExtensionIfItMatchesTheMimeType() { | ||
val fileName = "report.pdf" | ||
val mimeType = "application/pdf" | ||
val result = safeFileName(fileName, mimeType) | ||
assertEquals("report.pdf", result) | ||
} | ||
|
||
@Test | ||
fun shouldReplaceSpacesWithUnderscores() { | ||
val fileName = "my report.doc" | ||
val mimeType = "application/msword" | ||
val result = safeFileName(fileName, mimeType) | ||
assertEquals("my_report.doc", result) | ||
} | ||
|
||
@Test | ||
fun shouldAppendExtensionIfFileNameHasNoneAndMimeTypeIsValid() { | ||
val fileName = "newfile" | ||
val mimeType = "image/jpeg" | ||
val result = safeFileName(fileName, mimeType) | ||
assertEquals("newfile.jpg", result) | ||
} | ||
|
||
@Test | ||
fun shouldKeepHyphenatedNamesIntact() { | ||
val fileName = "my-file-name" | ||
val mimeType = "application/octet-stream" | ||
val result = safeFileName(fileName, mimeType) | ||
assertEquals("my-file-name.bin", result) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/util/file/FileUtil.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright (c) 2024 The Matrix.org Foundation C.I.C. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.matrix.android.sdk.internal.util.file | ||
|
||
import android.webkit.MimeTypeMap | ||
import org.matrix.android.sdk.internal.session.DefaultFileService.Companion.DEFAULT_FILENAME | ||
|
||
/** | ||
* Remove any characters from the file name that are not supported by the Android OS, | ||
* and update the file extension to match the mimeType. | ||
*/ | ||
fun safeFileName(fileName: String?, mimeType: String?): String { | ||
return buildString { | ||
// filename has to be safe for the Android System | ||
val result = fileName | ||
?.replace("[\\\\?%*:|\"<>\\s]".toRegex(), "_") | ||
?.takeIf { it.isNotEmpty() } | ||
?: DEFAULT_FILENAME | ||
append(result) | ||
// Check that the extension is correct regarding the mimeType | ||
val extensionFromMime = mimeType?.let { MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType) } | ||
if (extensionFromMime != null) { | ||
// Compare | ||
val fileExtension = result.substringAfterLast(delimiter = ".", missingDelimiterValue = "") | ||
if (fileExtension.isEmpty() || fileExtension != extensionFromMime) { | ||
// Missing extension, or diff in extension, add the one provided by the mimetype | ||
append(".") | ||
append(extensionFromMime) | ||
} | ||
} | ||
} | ||
} |