Package com.gmt2001
Class JSFileSystem
java.lang.Object
com.gmt2001.JSFileSystem
Provides access to File I/O from JS
Paths must in one of these base folders, relative to the location of PhantomBot.jar,
otherwise the operation fails: ./addons, ./config/audio-hooks,
./config/gif-alerts, ./config/clips, ./config/emotes,
./logs, or ./scripts
If running in Docker, symlinks to the same paths relative to /opt/PhantomBot_data
are also allowed. No other symlinks are allowed unless they still meet the location restrictions
above after being resolved to their real paths
- Author:
- gmt2001
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidDeleteFile(String path) Deletes the specified filestatic booleanFileExists(String path) Indicates if the file or directory existsFindFilesInDirectory(String path, String needle) Returns a list of files and sub-directories in the specified directory that contain theneedlestatic intGetFileSize(String path) Gets the size of a filestatic booleanIsDirectory(String path) Indicates if the specified path points to a directorystatic booleanCreates a new directory, if it doesn't already existstatic voidMoves the specified file to the specified location and/or renames the filestatic voidMoveFileToDirectory(String pathToFile, String pathToTargetDirectory) Moves the specified file to the specified directory.ReadFileAsLines(String path) Reads an entire file into a list of linesstatic StringReadFileAsString(String path) Reads an entire file into a stringstatic StringReadFileRandLine(String path) Reads a single randomly chosen line from an file into a stringstatic voidCreates an empty file if it doesn't exist; updates the last modified timestamp of the file if it already existsstatic voidWriteLinesToFile(String path, List<String> lines, boolean append) Writes a sequence of lines to a filestatic voidWriteLineToFile(String path, String line, boolean append) Writes a single line to a file
-
Method Details
-
FileExists
Indicates if the file or directory exists- Parameters:
path- The path to check, relative to PhantomBot.jar- Returns:
trueif the path exists and is in an allowed location
-
ReadFileAsLines
Reads an entire file into a list of lines- Parameters:
path- The path to the file to read- Returns:
- A
Listof lines; empty list if not an allowed location - Throws:
IOException- If an I/O error occurs reading from the file or a malformed or unmappable byte sequence is read
-
ReadFileAsString
Reads an entire file into a string- Parameters:
path- The path to the file to read- Returns:
- The file contents;
nullif not an allowed location - Throws:
IOException- If an I/O error occurs reading from the file or a malformed or unmappable byte sequence is read
-
ReadFileRandLine
Reads a single randomly chosen line from an file into a string- Parameters:
path- The path to the file to read- Returns:
- A randomly chosen line from the file;
nullif not an allowed location - Throws:
IOException- If an I/O error occurs reading from the file or a malformed or unmappable byte sequence is read
-
MakeDir
Creates a new directory, if it doesn't already exist- Parameters:
path- The path to the directory to create- Returns:
trueif the directory was created or already exists and is in an allowed location
-
MoveFileToDirectory
public static void MoveFileToDirectory(String pathToFile, String pathToTargetDirectory) throws IOException Moves the specified file to the specified directory. The original file name is kept- Parameters:
pathToFile- The path to the file to movepathToTargetDirectory- The directory to move the file into, not including the file name- Throws:
IOException- If an I/O error occurs
-
MoveFile
Moves the specified file to the specified location and/or renames the file- Parameters:
pathToFile- The path to the file to movenewPathToFile- The path to the new location of the file, including the new file name- Throws:
IOException- If an I/O error occurs
-
WriteLinesToFile
public static void WriteLinesToFile(String path, List<String> lines, boolean append) throws IOException Writes a sequence of lines to a file- Parameters:
path- The path to the file to write tolines- AListof lines to write to the fileappend-trueto add the lines to the end of the file if it already exists;falseto truncate the file before writing- Throws:
IOException- If an I/O error occurs writing to or creating the file, or the text cannot be encoded asUTF-8
-
WriteLineToFile
Writes a single line to a file- Parameters:
path- The path to the file to write toline- The line to write to the fileappend-trueto add the line to the end of the file if it already exists;falseto truncate the file before writing- Throws:
IOException- If an I/O error occurs writing to or creating the file, or the text cannot be encoded asUTF-8
-
TouchFile
Creates an empty file if it doesn't exist; updates the last modified timestamp of the file if it already exists- Parameters:
path- The path to the file to touch- Throws:
IOException- If an I/O error occurs
-
DeleteFile
Deletes the specified file- Parameters:
path- The path to the file to delete- Throws:
IOException- If an I/O error occurs
-
IsDirectory
Indicates if the specified path points to a directory- Parameters:
path- The path to check- Returns:
trueif the path is a directory in an allowed location;falseif the path does not exist, is not a directory, it cannot be determined if it is a directory or not, or is not in an allowed location
-
GetFileSize
Gets the size of a file- Parameters:
path- The path to the file to check- Returns:
- The size of the file, in bytes;
0if the file does not exist, an I/O error occurs, or the path is not in an allowed location
-
FindFilesInDirectory
Returns a list of files and sub-directories in the specified directory that contain theneedle- Parameters:
path- The path to the directory to searchneedle- The substring to match against;""(empty string) to return all files and sub-directories- Returns:
- A
Listof files and sub-directories; empty list ifpathis not a directory or is not in an allowed location - Throws:
IOException
-
CreateStringList
- Returns:
- A new
List
-