-
Notifications
You must be signed in to change notification settings - Fork 4
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 #20 from Coreoz/fix/content-disposition-utf8
Fix Content-Disposition header to handle UTF8 file names
- Loading branch information
Showing
8 changed files
with
73 additions
and
15 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
plume-file-core/src/main/java/com/coreoz/plume/file/cleaning/FileExtensionCleaning.java
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,39 @@ | ||
package com.coreoz.plume.file.cleaning; | ||
|
||
import javax.annotation.Nullable; | ||
import java.util.regex.Pattern; | ||
|
||
public class FileExtensionCleaning { | ||
private static final Pattern fileExtensionExcludePattern = Pattern.compile("[^a-zA-Z0-9]"); | ||
|
||
private FileExtensionCleaning() { | ||
// empty constructor | ||
} | ||
|
||
/** | ||
* Transform to extension to lower case and | ||
* remove all characters that are not numbers or between 'a' and 'z' | ||
* | ||
* @param fileExtension the file name extension, e.g. <code>jpg</code> | ||
* @return the clean file extension, null if fileExtension is null | ||
*/ | ||
@Nullable | ||
public static String cleanExtensionName(String fileExtension) { | ||
if (fileExtension == null) { | ||
return null; | ||
} | ||
return fileExtensionExcludePattern.matcher(fileExtension).replaceAll("").toLowerCase(); | ||
} | ||
|
||
@Nullable | ||
public static String parseFileNameExtension(String fileName) { | ||
if (fileName == null) { | ||
return null; | ||
} | ||
int dotIndex = fileName.lastIndexOf("."); | ||
if (dotIndex == -1) { | ||
return null; | ||
} | ||
return fileName.substring(dotIndex + 1); | ||
} | ||
} |
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
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
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
12 changes: 6 additions & 6 deletions
12
...oreoz/plume/file/utils/FileNamesTest.java → ...e/cleaning/FileExtensionCleaningTest.java
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 |
---|---|---|
@@ -1,31 +1,31 @@ | ||
package com.coreoz.plume.file.utils; | ||
package com.coreoz.plume.file.cleaning; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class FileNamesTest { | ||
public class FileExtensionCleaningTest { | ||
@Test | ||
public void test_get_extension_from_jpg_should_return_jpg() { | ||
assertThat(FileNames.parseFileNameExtension("toto.jpg")) | ||
assertThat(FileExtensionCleaning.parseFileNameExtension("toto.jpg")) | ||
.isEqualTo("jpg"); | ||
} | ||
|
||
@Test | ||
public void test_get_extension_from_no_extension_should_return_empty() { | ||
assertThat(FileNames.parseFileNameExtension("toto")) | ||
assertThat(FileExtensionCleaning.parseFileNameExtension("toto")) | ||
.isNull(); | ||
} | ||
|
||
@Test | ||
public void test_clean_extension_from_jpg_should_return_jpg() { | ||
assertThat(FileNames.cleanExtensionName(".jpg")) | ||
assertThat(FileExtensionCleaning.cleanExtensionName(".jpg")) | ||
.isEqualTo("jpg"); | ||
} | ||
|
||
@Test | ||
public void cleanExtensionName__verify_that_non_basic_chars_are_removed() { | ||
assertThat(FileNames.cleanExtensionName(" éi+$. \np")) | ||
assertThat(FileExtensionCleaning.cleanExtensionName(" éi+$. \np")) | ||
.isEqualTo("ip"); | ||
} | ||
} |
File renamed without changes.
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
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