Package utils.common
Class IOUtils
- java.lang.Object
-
- utils.common.IOUtils
-
@Singleton public class IOUtils extends java.lang.ObjectUtility class that handles access to the system's file system.
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringREGEX_ILLEGAL_IN_FILENAMERegular expression of illegal characters or strings in file or directory names '/', '\n', '\r', * '//', '\t', '\0', '\f', '`', '?', '*', '\\', '<', '>', '|', '\"', ':', '~', '!', '§', '$', '%', '&'static java.lang.StringREGEX_ILLEGAL_IN_PATH
-
Constructor Summary
Constructors Constructor Description IOUtils()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleancheckFileInStudyAssetsDirExists(java.lang.String dirName, java.lang.String filePath)static booleancheckFilename(java.lang.String filename)static booleancheckPath(java.lang.String path)booleancheckStudyAssetsDirExists(java.lang.String dirName)java.lang.StringcloneComponentHtmlFile(java.lang.String studyAssetsDirName, java.lang.String htmlFilePath)Copies a component's HTML file.java.lang.StringcloneStudyAssetsDirectory(java.lang.String srcDirName)Copies study assets' directory.static voidcopyRecursively(java.nio.file.Path source, java.nio.file.Path target)static voidcopyRecursively(java.nio.file.Path source, java.nio.file.Path target, java.nio.file.DirectoryStream.Filter<java.nio.file.Path> filter)voidcreateStudyAssetsDir(java.lang.String dirName)Creates a study assets dir.static voiddeleteRecursively(java.nio.file.Path root)static voiddeleteRecursivelyIfExists(java.nio.file.Path root)booleanexistsAndSecure(java.lang.String path, java.lang.String filePath)Checks filePath for path traversal attacks and existencejava.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.StringfindNonExistingStudyAssetsDirName(java.lang.String dirName)java.lang.StringgenerateFileName(java.lang.String rawName)Generates a filename from a name in a specified length.java.lang.StringgenerateFileName(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.PathgenerateStudyAssetsPath(java.lang.String dirName)Generates a study assets directory path.java.nio.file.PathgetExistingFileInStudyAssetsDir(java.lang.String dirName, java.lang.String filePath)Gets the File object which resides under filePath within the study assets' directory.java.nio.file.PathgetExistingFileSecurely(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.PathgetFileInStudyAssetsDir(java.lang.String dirName, java.lang.String filePath)Gets the File object which resides under filePath within the study assets' directory.static java.lang.StringgetResultsPathForJson(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 JSONstatic java.lang.StringgetResultsPathForZip(java.lang.Long studyResultId, java.lang.Long componentResultId)Path to result files in a zip package with '/' as file separatorlonggetResultUploadDirSize(java.lang.Long studyResultId)Returns the disk size in Bytes of all uploaded files belonging to the given study result ID.java.nio.file.PathgetResultUploadFileSecurely(java.lang.Long studyResultId, java.lang.Long componentResultId, java.lang.String filename)static java.nio.file.PathgetResultUploadsDir(java.lang.Long studyResultId)Path to the result uploads folder where JATOS stores the uploaded result filesstatic java.nio.file.PathgetResultUploadsDir(java.lang.Long studyResultId, java.lang.Long componentResultId)Path to the result uploads folder where JATOS stores the uploaded result filesjava.nio.file.PathgetStudyAssetsDir(java.lang.String dirName)Gets the study assets with the given directory name.longgetStudyAssetsDirSize(java.lang.String dirName)Returns the disk size in Bytes of all files inside the given study assets directory.static booleanmoveAndDetectOverwrite(java.nio.file.Path source, java.nio.file.Path target)voidmoveStudyAssetsDir(java.nio.file.Path srcDir, java.lang.String targetDirName)Moves study assets dir into the assets root dir.voidremoveResultUploadsDir(java.lang.Long studyResultId)voidremoveResultUploadsDir(java.lang.Long studyResultId, java.lang.Long componentResultId)voidremoveStudyAssetsDir(java.lang.String dirName)Remove study assets' directory if exists.voidrenameHtmlFile(java.lang.String oldHtmlFilePath, java.lang.String newHtmlFilePath, java.lang.String studyAssetName)Renames a component's HTML file.voidrenameStudyAssetsDir(java.lang.String oldDirName, java.lang.String newDirName)Renames a study assets dir.static java.nio.file.PathtmpDir()
-
-
-
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
-
-
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.IOExceptionGets 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.IOExceptionGets 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.IOExceptionGets 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.IOExceptionRemove 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.IOExceptionCopies a component's HTML file.- Parameters:
studyAssetsDirName- Name of the study assetshtmlFilePath- 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.IOExceptionCopies 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.IOExceptionMoves 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.IOExceptionCreates 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.IOExceptionReturns 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.IOExceptionReturns all directories within this directory.- Throws:
java.io.IOException
-
renameStudyAssetsDir
public void renameStudyAssetsDir(java.lang.String oldDirName, java.lang.String newDirName) throws java.io.IOExceptionRenames a study assets dir.- Throws:
java.io.IOException
-
getStudyAssetsDirSize
public long getStudyAssetsDirSize(java.lang.String dirName) throws java.io.IOExceptionReturns 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.IOExceptionRenames 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 assetsnewHtmlFilePath- The new local file path within the study assetsstudyAssetName- 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
-
-