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 void
DeleteFile
(String path) Deletes the specified filestatic boolean
FileExists
(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 theneedle
static int
GetFileSize
(String path) Gets the size of a filestatic boolean
IsDirectory
(String path) Indicates if the specified path points to a directorystatic boolean
Creates a new directory, if it doesn't already existstatic void
Moves the specified file to the specified location and/or renames the filestatic void
MoveFileToDirectory
(String pathToFile, String pathToTargetDirectory) Moves the specified file to the specified directory.ReadFileAsLines
(String path) Reads an entire file into a list of linesstatic String
ReadFileAsString
(String path) Reads an entire file into a stringstatic String
ReadFileRandLine
(String path) Reads a single randomly chosen line from an file into a stringstatic void
Creates an empty file if it doesn't exist; updates the last modified timestamp of the file if it already existsstatic void
WriteLinesToFile
(String path, List<String> lines, boolean append) Writes a sequence of lines to a filestatic void
WriteLineToFile
(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:
true
if 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
List
of 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;
null
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
-
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;
null
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
-
MakeDir
Creates a new directory, if it doesn't already exist- Parameters:
path
- The path to the directory to create- Returns:
true
if 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
- AList
of lines to write to the fileappend
-true
to add the lines to the end of the file if it already exists;false
to 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
-true
to add the line to the end of the file if it already exists;false
to 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:
true
if the path is a directory in an allowed location;false
if 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;
0
if 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
List
of files and sub-directories; empty list ifpath
is not a directory or is not in an allowed location - Throws:
IOException
-
CreateStringList
- Returns:
- A new
List
-