Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -581,15 +581,18 @@ public static long getContainerUsableSpace(final Path path) {

// The invalid character list is derived from this Stackoverflow page.
// https://stackoverflow.com/questions/1155107/is-there-a-cross-platform-java-method-to-remove-filename-special-chars
// The space character (32) is intentionally omitted: spaces are legal on every major file system, so they are
// preserved rather than replaced.
private static final int[] INVALID_CHARS = {34, 60, 62, 124, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 58, 42, 63, 92, 47, 32};
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 58, 42, 63, 92, 47};

static {
Arrays.sort(INVALID_CHARS);
}

/**
* Replaces invalid characters for a file system name within a given filename string to underscore '_'.
* Spaces are permitted and preserved.
* Be careful not to pass a file path as this method replaces path delimiter characters (i.e forward/back slashes).
* @param filename The filename to clean
* @return sanitized filename
Expand All @@ -602,10 +605,10 @@ public static String getSanitizedFilename(String filename) {
return "";
}

int codePointCount = filename.codePointCount(0, filename.length());
final int codePointCount = filename.codePointCount(0, filename.length());
final StringBuilder cleanName = new StringBuilder();
for (int i = 0; i < codePointCount; i++) {
int c = filename.codePointAt(i);
final int c = filename.codePointAt(i);
if (Arrays.binarySearch(INVALID_CHARS, c) < 0) {
cleanName.appendCodePoint(c);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.nifi.util.file;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

public class TestFileUtils {

@Test
public void testGetSanitizedFilenameNullAndEmpty() {
assertNull(FileUtils.getSanitizedFilename(null));
assertEquals("", FileUtils.getSanitizedFilename(""));
}

@Test
public void testGetSanitizedFilenameReplacesInvalidCharacters() {
assertEquals("a_b_c", FileUtils.getSanitizedFilename("a/b\\c"));
assertEquals("name_", FileUtils.getSanitizedFilename("name:"));
assertEquals("a_b", FileUtils.getSanitizedFilename("a\tb"));
assertEquals("_", FileUtils.getSanitizedFilename("*"));
}

@Test
public void testGetSanitizedFilenamePreservesSpaces() {
assertEquals("driver (1).jar", FileUtils.getSanitizedFilename("driver (1).jar"));
assertEquals("my report.txt", FileUtils.getSanitizedFilename("my report.txt"));
assertEquals("driver (1).jar", FileUtils.getSanitizedFilename("driver (1).jar"));
assertEquals(" driver (1).jar ", FileUtils.getSanitizedFilename(" driver (1).jar "));
}

@Test
public void testGetSanitizedFilenamePreservesDots() {
assertEquals("report...", FileUtils.getSanitizedFilename("report..."));
assertEquals(".env", FileUtils.getSanitizedFilename(".env"));
}
}
Loading