Merge pull request #5166 from eugene7646/utf8_sanitize_5416

Utf8 sanitize 5416
This commit is contained in:
Richard Cordovano 2019-09-11 14:56:14 -04:00 committed by GitHub
commit ba3c898161
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -20,6 +20,8 @@ package org.sleuthkit.autopsy.coreutils;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.logging.Level;
import org.openide.filesystems.FileObject;
import java.nio.file.Files;
@ -171,6 +173,18 @@ public class FileUtil {
//with underscores. We are only keeping \ as it could be part of the path.
return fileName.replaceAll("[\\p{Cntrl}/:\"*?<>|]+", "_");
}
/**
* UTF-8 sanitize and escape special characters in a file name or a file name component
*
* @param fileName to escape
*
* @return Sanitized string
*/
public static String utf8SanitizeFileName(String fileName) {
Charset charset = StandardCharsets.UTF_8;
return charset.decode(charset.encode(escapeFileName(fileName))).toString();
}
/**
* Test if the current user has read and write access to the dirPath.