Skip to content

Commit

Permalink
remove operating system forbidden chars from suggested file name (#83)
Browse files Browse the repository at this point in the history
* remove operating system forbidden chars from suggested file name

* adapt functionality to work differently for windows and unix based operating systems
  • Loading branch information
c-eg authored Jun 30, 2024
1 parent a5122ab commit 71a564a
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ dependencies {

// util
implementation 'commons-io:commons-io:2.15.1'
implementation 'org.apache.commons:commons-lang3:3.14.0'
implementation 'uk.co.conoregan:themoviedbapi:2.1.1'
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
requires javafx.fxml;
requires javafx.controls;
requires org.apache.commons.io;
requires org.apache.commons.lang3;
requires org.slf4j;

opens uk.co.conoregan.showrenamer to javafx.fxml, javafx.graphics;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Optional;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.SystemUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import uk.co.conoregan.showrenamer.api.ShowResultProvider;
Expand Down Expand Up @@ -124,6 +125,13 @@ public Optional<File> getImprovedName(final File file) {
.replace("{year}", String.valueOf(showResult.get().date().getYear()));
}

if (SystemUtils.IS_OS_WINDOWS) {
newFileName = StringUtils.removeForbiddenCharsWindows(newFileName);
}
else if (SystemUtils.IS_OS_UNIX) {
newFileName = StringUtils.removeForbiddenCharsUnix(newFileName);
}

return Optional.of(new File(StringUtils.replaceLastOccurrence(file.getAbsolutePath(), fileName, newFileName)));
}
}
22 changes: 22 additions & 0 deletions src/main/java/uk/co/conoregan/showrenamer/util/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,26 @@ public static String replaceLastOccurrence(final String original, final String t

return original;
}

/**
* Removes forbidden chars from the text. For Windows based Operating Systems.
* A forbidden char in this context is a char that is not allowed to be used within a filename.
*
* @param text the text to remove forbidden chars from.
* @return the text with forbidden chars removed.
*/
public static String removeForbiddenCharsWindows(final String text) {
return text.replaceAll("[<>:\"/\\\\|?*]", "");
}

/**
* Removes forbidden chars from the text. For Unix based Operating Systems.
* A forbidden char in this context is a char that is not allowed to be used within a filename.
*
* @param text the text to remove forbidden chars from.
* @return the text with forbidden chars removed.
*/
public static String removeForbiddenCharsUnix(final String text) {
return text.replaceAll("/", "");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,30 @@ public void testReplaceLastOccurrenceToReplaceNotExisting() {

Assertions.assertEquals("TestTest", result);
}

@Test
public void testRemoveForbiddenCharsWindows() {
String testString = "test <>:\"/\\|?*";

Assertions.assertTrue(StringUtils.removeForbiddenCharsWindows(testString).contains("test"));

Assertions.assertFalse(StringUtils.removeForbiddenCharsWindows(testString).contains("<"));
Assertions.assertFalse(StringUtils.removeForbiddenCharsWindows(testString).contains(">"));
Assertions.assertFalse(StringUtils.removeForbiddenCharsWindows(testString).contains(":"));
Assertions.assertFalse(StringUtils.removeForbiddenCharsWindows(testString).contains("\""));
Assertions.assertFalse(StringUtils.removeForbiddenCharsWindows(testString).contains("/"));
Assertions.assertFalse(StringUtils.removeForbiddenCharsWindows(testString).contains("\\"));
Assertions.assertFalse(StringUtils.removeForbiddenCharsWindows(testString).contains("|"));
Assertions.assertFalse(StringUtils.removeForbiddenCharsWindows(testString).contains("?"));
Assertions.assertFalse(StringUtils.removeForbiddenCharsWindows(testString).contains("*"));
}

@Test
public void testRemoveForbiddenCharsUnix() {
String testString = "test /";

Assertions.assertTrue(StringUtils.removeForbiddenCharsWindows(testString).contains("test"));

Assertions.assertFalse(StringUtils.removeForbiddenCharsWindows(testString).contains("/"));
}
}

0 comments on commit 71a564a

Please sign in to comment.