Package utils.common

Class IOUtils


  • @Singleton
    public class IOUtils
    extends java.lang.Object
    Utility class that handles access to the system's file system.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String REGEX_ILLEGAL_IN_FILENAME
      Regular expression of illegal characters or strings in file or directory names '/', '\n', '\r', * '//', '\t', '\0', '\f', '`', '?', '*', '\\', '<', '>', '|', '\"', ':', '~', '!', '§', '$', '%', '&'
      static java.lang.String REGEX_ILLEGAL_IN_PATH  
    • Constructor Summary

      Constructors 
      Constructor Description
      IOUtils()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean checkFileInStudyAssetsDirExists​(java.lang.String dirName, java.lang.String filePath)  
      static boolean checkFilename​(java.lang.String filename)  
      static boolean checkPath​(java.lang.String path)  
      boolean checkStudyAssetsDirExists​(java.lang.String dirName)  
      java.lang.String cloneComponentHtmlFile​(java.lang.String studyAssetsDirName, java.lang.String htmlFilePath)
      Copies a component's HTML file.
      java.lang.String cloneStudyAssetsDirectory​(java.lang.String srcDirName)
      Copies study assets' directory.
      static void copyRecursively​(java.nio.file.Path source, java.nio.file.Path target)  
      static void copyRecursively​(java.nio.file.Path source, java.nio.file.Path target, java.nio.file.DirectoryStream.Filter<java.nio.file.Path> filter)  
      void createStudyAssetsDir​(java.lang.String dirName)
      Creates a study assets dir.
      static void deleteRecursively​(java.nio.file.Path root)  
      static void deleteRecursivelyIfExists​(java.nio.file.Path root)  
      boolean existsAndSecure​(java.lang.String path, java.lang.String filePath)
      Checks filePath for path traversal attacks and existence
      java.nio.file.Path[] findDirectories​(java.nio.file.Path dir)
      Returns all directories within this directory.
      java.nio.file.Path[] findFiles​(java.nio.file.Path dir, java.lang.String prefix, java.lang.String suffix)
      Returns all files within this directory that have the prefix and the suffix.
      java.lang.String findNonExistingStudyAssetsDirName​(java.lang.String dirName)  
      java.lang.String generateFileName​(java.lang.String rawName)
      Generates a filename from a name in a specified length.
      java.lang.String generateFileName​(java.lang.String rawName, java.lang.String suffix)
      Generates a filename from a name in a specified length and adds the suffix.
      static java.nio.file.Path generateStudyAssetsPath​(java.lang.String dirName)
      Generates a study assets directory path.
      java.nio.file.Path getExistingFileInStudyAssetsDir​(java.lang.String dirName, java.lang.String filePath)
      Gets the File object which resides under filePath within the study assets' directory.
      java.nio.file.Path getExistingFileSecurely​(java.lang.String pathStr, java.lang.String filePathStr)
      Gets the File object while preventing a path traversal attack and checks whether the file exists and is no directory.
      java.nio.file.Path getFileInStudyAssetsDir​(java.lang.String dirName, java.lang.String filePath)
      Gets the File object which resides under filePath within the study assets' directory.
      static java.lang.String getResultsPathForJson​(java.lang.Long studyResultId, java.lang.Long componentResultId)
      Path to result files in the file system with '/' as a file separator meant to be used in JSON
      static java.lang.String getResultsPathForZip​(java.lang.Long studyResultId, java.lang.Long componentResultId)
      Path to result files in a zip package with '/' as file separator
      long getResultUploadDirSize​(java.lang.Long studyResultId)
      Returns the disk size in Bytes of all uploaded files belonging to the given study result ID.
      java.nio.file.Path getResultUploadFileSecurely​(java.lang.Long studyResultId, java.lang.Long componentResultId, java.lang.String filename)  
      static java.nio.file.Path getResultUploadsDir​(java.lang.Long studyResultId)
      Path to the result uploads folder where JATOS stores the uploaded result files
      static java.nio.file.Path getResultUploadsDir​(java.lang.Long studyResultId, java.lang.Long componentResultId)
      Path to the result uploads folder where JATOS stores the uploaded result files
      java.nio.file.Path getStudyAssetsDir​(java.lang.String dirName)
      Gets the study assets with the given directory name.
      long getStudyAssetsDirSize​(java.lang.String dirName)
      Returns the disk size in Bytes of all files inside the given study assets directory.
      static boolean moveAndDetectOverwrite​(java.nio.file.Path source, java.nio.file.Path target)  
      void moveStudyAssetsDir​(java.nio.file.Path srcDir, java.lang.String targetDirName)
      Moves study assets dir into the assets root dir.
      void removeResultUploadsDir​(java.lang.Long studyResultId)  
      void removeResultUploadsDir​(java.lang.Long studyResultId, java.lang.Long componentResultId)  
      void removeStudyAssetsDir​(java.lang.String dirName)
      Remove study assets' directory if exists.
      void renameHtmlFile​(java.lang.String oldHtmlFilePath, java.lang.String newHtmlFilePath, java.lang.String studyAssetName)
      Renames a component's HTML file.
      void renameStudyAssetsDir​(java.lang.String oldDirName, java.lang.String newDirName)
      Renames a study assets dir.
      static java.nio.file.Path tmpDir()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • REGEX_ILLEGAL_IN_FILENAME

        public static final java.lang.String REGEX_ILLEGAL_IN_FILENAME
        Regular expression of illegal characters or strings in file or directory names '/', '\n', '\r', * '//', '\t', '\0', '\f', '`', '?', '*', '\\', '<', '>', '|', '\"', ':', '~', '!', '§', '$', '%', '&'
        See Also:
        Constant Field Values
      • REGEX_ILLEGAL_IN_PATH

        public static final java.lang.String REGEX_ILLEGAL_IN_PATH
        See Also:
        Constant Field Values
    • Constructor Detail

      • IOUtils

        public IOUtils()
    • Method Detail

      • tmpDir

        public static java.nio.file.Path tmpDir()
      • copyRecursively

        public static void copyRecursively​(java.nio.file.Path source,
                                           java.nio.file.Path target)
                                    throws java.io.IOException
        Throws:
        java.io.IOException
      • copyRecursively

        public static void copyRecursively​(java.nio.file.Path source,
                                           java.nio.file.Path target,
                                           java.nio.file.DirectoryStream.Filter<java.nio.file.Path> filter)
                                    throws java.io.IOException
        Throws:
        java.io.IOException
      • deleteRecursively

        public static void deleteRecursively​(java.nio.file.Path root)
                                      throws java.io.IOException
        Throws:
        java.io.IOException
      • deleteRecursivelyIfExists

        public static void deleteRecursivelyIfExists​(java.nio.file.Path root)
                                              throws java.io.IOException
        Throws:
        java.io.IOException
      • checkStudyAssetsDirExists

        public boolean checkStudyAssetsDirExists​(java.lang.String dirName)
      • checkFileInStudyAssetsDirExists

        public boolean checkFileInStudyAssetsDirExists​(java.lang.String dirName,
                                                       java.lang.String filePath)
      • getExistingFileSecurely

        public java.nio.file.Path getExistingFileSecurely​(java.lang.String pathStr,
                                                          java.lang.String filePathStr)
                                                   throws java.io.IOException
        Gets the File object while preventing a path traversal attack and checks whether the file exists and is no directory.
        Throws:
        java.io.IOException
      • existsAndSecure

        public boolean existsAndSecure​(java.lang.String path,
                                       java.lang.String filePath)
        Checks filePath for path traversal attacks and existence
      • getFileInStudyAssetsDir

        public java.nio.file.Path getFileInStudyAssetsDir​(java.lang.String dirName,
                                                          java.lang.String filePath)
                                                   throws java.io.IOException
        Gets the File object which resides under filePath within the study assets' directory.
        Throws:
        java.io.IOException
      • getExistingFileInStudyAssetsDir

        public java.nio.file.Path getExistingFileInStudyAssetsDir​(java.lang.String dirName,
                                                                  java.lang.String filePath)
        Gets the File object which resides under filePath within the study assets' directory.
      • getStudyAssetsDir

        public java.nio.file.Path getStudyAssetsDir​(java.lang.String dirName)
                                             throws java.io.IOException
        Gets the study assets with the given directory name.
        Throws:
        java.io.IOException
      • generateFileName

        public java.lang.String generateFileName​(java.lang.String rawName,
                                                 java.lang.String suffix)
        Generates a filename from a name in a specified length and adds the suffix.
      • checkFilename

        public static boolean checkFilename​(java.lang.String filename)
      • checkPath

        public static boolean checkPath​(java.lang.String path)
      • generateFileName

        public java.lang.String generateFileName​(java.lang.String rawName)
        Generates a filename from a name in a specified length.
      • generateStudyAssetsPath

        public static java.nio.file.Path generateStudyAssetsPath​(java.lang.String dirName)
        Generates a study assets directory path.
      • removeStudyAssetsDir

        public void removeStudyAssetsDir​(java.lang.String dirName)
                                  throws java.io.IOException
        Remove study assets' directory if exists.
        Throws:
        java.io.IOException
      • cloneComponentHtmlFile

        public java.lang.String cloneComponentHtmlFile​(java.lang.String studyAssetsDirName,
                                                       java.lang.String htmlFilePath)
                                                throws java.io.IOException
        Copies a component's HTML file.
        Parameters:
        studyAssetsDirName - Name of the study assets
        htmlFilePath - Local file path to the HTML file. The file can be in a subdirectory of the study assets directory.
        Returns:
        Name of the new file.
        Throws:
        java.io.IOException
      • cloneStudyAssetsDirectory

        public java.lang.String cloneStudyAssetsDirectory​(java.lang.String srcDirName)
                                                   throws java.io.IOException
        Copies study assets' directory. Adds suffix '_clone' to the name of the new assets dir. If a dir with suffix '_clone' already exists, it adds '_' + number instead.
        Throws:
        java.io.IOException
      • findNonExistingStudyAssetsDirName

        public java.lang.String findNonExistingStudyAssetsDirName​(java.lang.String dirName)
      • moveStudyAssetsDir

        public void moveStudyAssetsDir​(java.nio.file.Path srcDir,
                                       java.lang.String targetDirName)
                                throws java.io.IOException
        Moves study assets dir into the assets root dir.
        Parameters:
        srcDir - The source dir File can be anywhere in the file system.
        targetDirName - Name of the target dir within the assets root dir
        Throws:
        java.io.IOException
      • createStudyAssetsDir

        public void createStudyAssetsDir​(java.lang.String dirName)
                                  throws java.io.IOException
        Creates a study assets dir.
        Parameters:
        dirName - Name of the new study assets dir.
        Throws:
        java.io.IOException
      • findFiles

        public java.nio.file.Path[] findFiles​(java.nio.file.Path dir,
                                              java.lang.String prefix,
                                              java.lang.String suffix)
                                       throws java.io.IOException
        Returns all files within this directory that have the prefix and the suffix.
        Throws:
        java.io.IOException
      • findDirectories

        public java.nio.file.Path[] findDirectories​(java.nio.file.Path dir)
                                             throws java.io.IOException
        Returns all directories within this directory.
        Throws:
        java.io.IOException
      • renameStudyAssetsDir

        public void renameStudyAssetsDir​(java.lang.String oldDirName,
                                         java.lang.String newDirName)
                                  throws java.io.IOException
        Renames a study assets dir.
        Throws:
        java.io.IOException
      • getStudyAssetsDirSize

        public long getStudyAssetsDirSize​(java.lang.String dirName)
                                   throws java.io.IOException
        Returns the disk size in Bytes of all files inside the given study assets directory. It does not count the size of directories themselves (e.g., on Linux, each directory takes 4kB).
        Throws:
        java.io.IOException
      • renameHtmlFile

        public void renameHtmlFile​(java.lang.String oldHtmlFilePath,
                                   java.lang.String newHtmlFilePath,
                                   java.lang.String studyAssetName)
                            throws java.io.IOException
        Renames a component's HTML file. This file can be in a subdirectory of the study assets directory.
        Parameters:
        oldHtmlFilePath - The current local file path within the study assets
        newHtmlFilePath - The new local file path within the study assets
        studyAssetName - The name (not the path) of the study assets
        Throws:
        java.io.IOException
      • getResultUploadsDir

        public static java.nio.file.Path getResultUploadsDir​(java.lang.Long studyResultId)
        Path to the result uploads folder where JATOS stores the uploaded result files
      • getResultUploadsDir

        public static java.nio.file.Path getResultUploadsDir​(java.lang.Long studyResultId,
                                                             java.lang.Long componentResultId)
        Path to the result uploads folder where JATOS stores the uploaded result files
      • getResultsPathForZip

        public static java.lang.String getResultsPathForZip​(java.lang.Long studyResultId,
                                                            java.lang.Long componentResultId)
        Path to result files in a zip package with '/' as file separator
      • getResultsPathForJson

        public static java.lang.String getResultsPathForJson​(java.lang.Long studyResultId,
                                                             java.lang.Long componentResultId)
        Path to result files in the file system with '/' as a file separator meant to be used in JSON
      • getResultUploadDirSize

        public long getResultUploadDirSize​(java.lang.Long studyResultId)
        Returns the disk size in Bytes of all uploaded files belonging to the given study result ID. It does not count the size of directories themselves (e.g. on Linux each directory takes 4kB).
      • getResultUploadFileSecurely

        public java.nio.file.Path getResultUploadFileSecurely​(java.lang.Long studyResultId,
                                                              java.lang.Long componentResultId,
                                                              java.lang.String filename)
                                                       throws java.io.IOException
        Throws:
        java.io.IOException
      • removeResultUploadsDir

        public void removeResultUploadsDir​(java.lang.Long studyResultId)
                                    throws java.io.IOException
        Throws:
        java.io.IOException
      • removeResultUploadsDir

        public void removeResultUploadsDir​(java.lang.Long studyResultId,
                                           java.lang.Long componentResultId)
                                    throws java.io.IOException
        Throws:
        java.io.IOException
      • moveAndDetectOverwrite

        public static boolean moveAndDetectOverwrite​(java.nio.file.Path source,
                                                     java.nio.file.Path target)
                                              throws java.io.IOException
        Throws:
        java.io.IOException