Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Files: Implements methods isHidden and isMetadata to check if a path is either hidden or a type of metadata file #544

Merged
merged 23 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
cff6289
Creates a new list that contains metadata file names and extensions
scireum-mbo Dec 10, 2024
f379ec4
Implements methods to check whether a file path is hidden or is a met…
scireum-mbo Dec 10, 2024
9d9a8c4
Renames method to contain "considered"
scireum-mbo Dec 10, 2024
1493a71
Changes list of metadata files to only contain files that are created…
scireum-mbo Dec 10, 2024
bb0119a
Adds a contains to check if the hidden file is inside a folder
scireum-mbo Dec 10, 2024
1799a26
Changes metadata check from contains to startsWith for a more precise…
scireum-mbo Dec 10, 2024
9940d07
Changes check to always look for the last part of the filepath
scireum-mbo Dec 10, 2024
9969325
Implements tests for the new methods
scireum-mbo Dec 10, 2024
09174a4
Changes Thumbs.db to the correct case
scireum-mbo Dec 10, 2024
c347e08
Fixes misaligned text
scireum-mbo Dec 10, 2024
c7388a0
Changes the startsWith back to contains to check for metadata files i…
scireum-mbo Dec 10, 2024
5cd36cb
Updates test for the isConsideredMetadata test
scireum-mbo Dec 10, 2024
c77f07a
Adds a method to retrieve all parts of a given path as a stream
scireum-mbo Dec 10, 2024
9fd3c8b
Adapts return logic to implement the newly added method
scireum-mbo Dec 10, 2024
3fb0896
Formats file accordingly
scireum-mbo Dec 10, 2024
d616b10
Makes streamPath public to make it accessible to other classes
scireum-mbo Dec 10, 2024
dddfa51
Changes testcases to match functionality
scireum-mbo Dec 10, 2024
2bd2043
Renames parameter and changes logic to check if the filename is hidden
scireum-mbo Dec 10, 2024
4e138f5
Adds a test for the new streamPath method
scireum-mbo Dec 10, 2024
97f5cf1
Removes empty string check, due to it already being executed in strea…
scireum-mbo Dec 10, 2024
e93e144
Changes parameter name and comparison logic
scireum-mbo Dec 10, 2024
34b3f95
Formats file accordingly
scireum-mbo Dec 10, 2024
b44bcab
Removes some testcases for isConsideredMetadata test
scireum-mbo Dec 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 62 additions & 2 deletions src/main/java/sirius/kernel/commons/Files.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;
import java.util.stream.Stream;

/**
* Helperclass for handling files in Java 8.
Expand All @@ -28,6 +30,11 @@ public class Files {

private static final Pattern NON_PATH_CHARACTERS = Pattern.compile("[^a-zA-Z0-9\\-.]");

/**
* Contains a list of file names and endings which are considered to be metadata.
*/
private static final List<String> METADATA_FILES = List.of("__MACOSX", ".DS_Store", "Thumbs.db");

private Files() {
}

Expand Down Expand Up @@ -147,8 +154,39 @@ public static String getFilenameWithoutExtension(@Nullable String path) {
}

/**
* If the given file is not null and exists, tries to delete that file and logs when a file cannot be deleted. T
* his is useful for error reporting and to diagnose why a file cannot be deleted.
* Determines if the given path is hidden.
* <p>
* A path is considered hidden if it starts with a dot.
*
* @param fileOrDirectoryName the path to check
* @return <tt>true</tt> if the path is hidden, <tt>false</tt> otherwise
*/
public static boolean isConsideredHidden(@Nullable String fileOrDirectoryName) {
if (Strings.isEmpty(fileOrDirectoryName)) {
return false;
}
return fileOrDirectoryName.startsWith(".");
}

/**
* Determines if the given path is a metadata file.
* <p>
* A metadata file is a file which is not part of the actual content but rather contains metadata or is used by
* the operating system or other tools.
*
* @param path the path to check
* @return <tt>true</tt> if the path is a metadata file that is listed in the METADATA_FILES list, <tt>false</tt> otherwise
*/
public static boolean isConsideredMetadata(@Nullable String path) {
if (Strings.isEmpty(path)) {
return false;
}
return streamPath(path).anyMatch(METADATA_FILES::contains);
scireum-mbo marked this conversation as resolved.
Show resolved Hide resolved
scireum-mbo marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* If the given file is not null and exists, tries to delete that file and logs when a file cannot be deleted.
* This is useful for error reporting and to diagnose why a file cannot be deleted.
*
* @param file the file to delete
*/
Expand Down Expand Up @@ -213,4 +251,26 @@ public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOEx
}
});
}

/**
* Returns a stream of all parts of the given path.
* <p>
* The stream will contain the filename, the parent directory, the parent of the parent and so on.
*
* @param path the path to split
* @return a stream of all parts of the given path
*/
public static Stream<String> streamPath(String path) {
if(Strings.isEmpty(path)) {
scireum-mbo marked this conversation as resolved.
Show resolved Hide resolved
return Stream.empty();
}
Stream.Builder<String> builder = Stream.builder();
builder.add(Files.getFilenameAndExtension(path));
String parent = Files.getBasepath(path);
while (Strings.isFilled(parent)) {
builder.add(Files.getFilenameAndExtension(parent));
parent = Files.getBasepath(parent);
}
return builder.build();
}
}
35 changes: 35 additions & 0 deletions src/test/kotlin/sirius/kernel/commons/FilesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,39 @@ internal class FilesTest {
assertNull(Files.toSaneFileName(" ").orElse(null))
assertNull(Files.toSaneFileName("").orElse(null))
}

@Test
fun `isConsideredHidden works as expected`(){
assertEquals(true, Files.isConsideredHidden(".test"));
assertEquals(true, Files.isConsideredHidden(".test.tmp"));
assertEquals(false, Files.isConsideredHidden("test"));
assertEquals(false, Files.isConsideredHidden("test.tmp"));
assertEquals(false,Files.isConsideredHidden(null));
}

@Test
fun `isConsideredMetadata works as expected`(){
assertEquals(true, Files.isConsideredMetadata("__MACOSX"));
assertEquals(true, Files.isConsideredMetadata("__MACOSX/test"));
assertEquals(true, Files.isConsideredMetadata("__MACOSX/folder1/test"));
assertEquals(true, Files.isConsideredMetadata("folder1/__MACOSX"));
assertEquals(true, Files.isConsideredMetadata("folder1/__MACOSX/folder2/test.pdf"));
assertEquals(true, Files.isConsideredMetadata(".DS_Store"));
assertEquals(true, Files.isConsideredMetadata("folder1/.DS_Store"));
assertEquals(true, Files.isConsideredMetadata("Thumbs.db"));
assertEquals(true, Files.isConsideredMetadata("folder1/Thumbs.db"));
assertEquals(false, Files.isConsideredMetadata("test"));
assertEquals(false, Files.isConsideredMetadata("test.tmp"));
assertEquals(false, Files.isConsideredMetadata("thumbs.db"));
assertEquals(false, Files.isConsideredMetadata(null));
}

@Test
fun `streamPath works as expected`() {
assert(Files.streamPath("test1").toList().equals(listOf("test1")));
assertEquals(listOf("test2", "test1"), Files.streamPath("/test1/test2").toList());
assertEquals(listOf("test3", ".test2", "test1"), Files.streamPath("/test1/.test2/test3").toList());
assertEquals(listOf("test4.png", "test3", "test2", "test1"), Files.streamPath("/test1/test2/test3/test4.png").toList());
assertEquals(listOf(null, "test2", "test1"), Files.streamPath("/test1/test2/").toList());
}
}
Loading