This commit is contained in:
Fabio Sinibaldi 2023-01-12 15:53:25 +01:00
parent b7788c063e
commit a58bd458f7
2 changed files with 15 additions and 7 deletions

View File

@ -92,12 +92,12 @@ public class Files {
if(toFix.startsWith("\\."))
toFix=toFix.replaceFirst("\\.","_");
String extension="";
if(toFix.contains(".")) {
int extensionIndex=toFix.lastIndexOf(".");
if(extensionIndex>0) {
//preserve extension
int index=toFix.indexOf(".");
extension=toFix.substring(index);
extension=toFix.substring(extensionIndex);
//only escape before extension
toFix=toFix.substring(0,toFix.indexOf("."));
toFix=toFix.substring(0,extensionIndex);
}
return toFix.toLowerCase().
replaceAll("[\\-\\*\\+\\/\\\\ \\[\\]\\(\\)\\.\\\"\\:\\;\\|\\=]","_")+extension.toLowerCase();

View File

@ -6,6 +6,8 @@ import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
@ -18,11 +20,17 @@ public class FilesTests {
@Test
public void testNames(){
for(String name : baseFolder.list()){
ArrayList<String> toTestStrings=new ArrayList<>(Arrays.asList(baseFolder.list()));
toTestStrings.add("e. gna_topografia_timponedellamotta.qmd");
for(String name : toTestStrings){
if(name.contains(".")) {
String originalExtension = name.substring(name.indexOf("."));
int extensionIndex = name.lastIndexOf(".");
String originalExtension = extensionIndex>0?name.substring(extensionIndex):null;
String obtained = Files.fixFilename(name);
log.info(name + "->" + obtained + "[" + originalExtension + "]");
if(originalExtension!=null)
assertTrue(obtained.endsWith(originalExtension));
}
}