Package com.gmt2001

Class JSFileSystem

java.lang.Object
com.gmt2001.JSFileSystem

public final class JSFileSystem extends Object
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 Details

    • FileExists

      public static boolean FileExists(String path)
      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

      public static List<String> ReadFileAsLines(String path) throws IOException
      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

      public static String ReadFileAsString(String path) throws IOException
      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

      public static String ReadFileRandLine(String path) throws IOException
      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

      public static boolean MakeDir(String path)
      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 move
      pathToTargetDirectory - The directory to move the file into, not including the file name
      Throws:
      IOException - If an I/O error occurs
    • MoveFile

      public static void MoveFile(String pathToFile, String newPathToFile) throws IOException
      Moves the specified file to the specified location and/or renames the file
      Parameters:
      pathToFile - The path to the file to move
      newPathToFile - 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 to
      lines - A List of lines to write to the file
      append - 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 as UTF-8
    • WriteLineToFile

      public static void WriteLineToFile(String path, String line, boolean append) throws IOException
      Writes a single line to a file
      Parameters:
      path - The path to the file to write to
      line - The line to write to the file
      append - 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 as UTF-8
    • TouchFile

      public static void TouchFile(String path) throws IOException
      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

      public static void DeleteFile(String path) throws IOException
      Deletes the specified file
      Parameters:
      path - The path to the file to delete
      Throws:
      IOException - If an I/O error occurs
    • IsDirectory

      public static boolean IsDirectory(String path)
      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

      public static int GetFileSize(String path)
      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

      public static List<String> FindFilesInDirectory(String path, String needle) throws IOException
      Returns a list of files and sub-directories in the specified directory that contain the needle
      Parameters:
      path - The path to the directory to search
      needle - The substring to match against; "" (empty string) to return all files and sub-directories
      Returns:
      A List of files and sub-directories; empty list if path is not a directory or is not in an allowed location
      Throws:
      IOException
    • CreateStringList

      public static List<String> CreateStringList()
      Helper method to create a List of String
      Returns:
      A new List