Class FileUploadUtils


  • public class FileUploadUtils
    extends Object
    Utilities for FileUpload components.
    • Method Detail

      • getValidFilename

        public static String getValidFilename​(String filename)
      • isSystemWindows

        public static boolean isSystemWindows()
      • isValidType

        public static boolean isValidType​(PrimeApplicationContext context,
                                          FileUpload fileUpload,
                                          UploadedFile uploadedFile)
        Check if an uploaded file meets all specifications regarding its filename and content type. It evaluates FileUploadBase.getAllowTypes() as well as FileUploadBase.getAccept() and uses the installed FileTypeDetector implementation. For most reliable content type checking it's recommended to plug in Apache Tika as an implementation.
        Parameters:
        fileUpload - the fileUpload component
        uploadedFile - the details of the uploaded file
        Returns:
        true, if all validations regarding filename and content type passed, false else
      • convertJavaScriptRegex

        protected static String convertJavaScriptRegex​(String jsRegex)
        Converts a JavaScript regular expression like '/(\.|\/)(gif|jpe?g|png)$/i' to the Java usable format '(\\.|\\/)(gif|jpe?g|png)$'
        Parameters:
        jsRegex - the client side JavaScript regex
        Returns:
        the Java converted version of the regex
      • tryValidateFile

        public static void tryValidateFile​(javax.faces.context.FacesContext context,
                                           FileUpload fileUpload,
                                           UploadedFile uploadedFile)
                                    throws javax.faces.validator.ValidatorException
        Throws:
        javax.faces.validator.ValidatorException
      • tryValidateFiles

        public static void tryValidateFiles​(javax.faces.context.FacesContext context,
                                            FileUpload fileUpload,
                                            List<UploadedFile> files)
      • checkPathTraversal

        public static String checkPathTraversal​(String relativePath)
        OWASP prevent directory path traversal of "../../image.png".
        Parameters:
        relativePath - the relative path to check for path traversal
        Returns:
        the relative path
        Throws:
        javax.faces.FacesException - if any error is detected
        See Also:
        https://owasp.org/www-community/attacks/Path_Traversal
      • listChunks

        public static List<Path> listChunks​(Path path)
      • listChunks

        public static <T extends javax.servlet.http.HttpServletRequest> List<Path> listChunks​(T request)
      • getFileUploadChunkDecoder

        public static <T extends javax.servlet.http.HttpServletRequest> FileUploadChunkDecoder<T> getFileUploadChunkDecoder​(T request)
      • getChunkDir

        public static <T extends javax.servlet.http.HttpServletRequest> Path getChunkDir​(T request)