halo - v1.0.0
    Preparing search index...

    Class IOF

    IOF (Input/Output File) class provides methods for file and directory operations, including creating directories, removing files, watching directories for changes, reading and writing JSON files, calculating file hashes and sizes, and saving files.

    Index

    Constructors

    Methods

    • Converts a file to a generative path format.

      Parameters

      • fileName: string

        The name of the file to convert.

      Returns Promise<{ inlineData: { data: string; mimeType: string } }>

      An object containing the inline data with base64 encoded content and MIME type.

      An error if the file cannot be read or converted.

    • Saves a file buffer to the specified file path on disk.

      Calculates the file's hash, size, and MIME type, creates the necessary directories, and writes the file data to disk. Returns an object containing metadata about the saved file.

      Parameters

      • data: FileInterface

        An object implementing the FileInterface, containing the file data, filename, and target filepath.

      Returns Promise<null | FileStorageInterface>

      A promise that resolves to a FileStorageInterface object with file metadata, or null if saving fails.

      If the file cannot be saved to the specified location.

    • Calculates the SHA-256 hash of a given buffer.

      Parameters

      • buffer: Buffer

        The buffer to hash.

      Returns string

      The SHA-256 hash as a hexadecimal string.

    • Calculates the size of a file based on its buffer.

      Parameters

      • buffer: Buffer

        The buffer representing the file.

      Returns number

      The size of the file in bytes.

    • Asynchronously checks if a file exists at the specified path.

      Parameters

      • filePath: string

        The path to the file.

      Returns Promise<boolean>

      A promise that resolves to a boolean indicating whether the file exists.

      An error if the existence check fails.

    • Checks if a file exists at the specified path.

      Parameters

      • filePath: string

        The path to the file.

      Returns boolean

      A boolean indicating whether the file exists.

      An error if the existence check fails.

    • Creates a directory if it does not exist.

      Parameters

      • dirPath: string

        The path of the directory to create.

      Returns void

    • Reads a JSON file and returns its content as an array.

      Type Parameters

      • T

      Parameters

      • filePath: string

        The path to the JSON file.

      Returns Promise<T[]>

      An array of objects parsed from the JSON file.

      An error if the file does not exist or if the content is not an array.

    • Retrieves the string content of a text file.

      Parameters

      • filePath: string

        The path to the text file.

      Returns Promise<string>

      A promise that resolves to the content of the file as a string.

      An error if the file cannot be read.

    • Removes a directory or file at the specified path. If the path is a directory, it will be removed recursively.

      Parameters

      • dirPath: string

        The path of the directory to remove.

      Returns void

    • Watches a directory for file system events and executes a callback with the event details.

      Parameters

      • options: {
            dirPath: string;
            event:
                | (keyof FSWatcherKnownEventMap)
                | "add"
                | "change"
                | "addDir"
                | "unlink"
                | "unlinkDir";
            onEvent?: (
                args: {
                    event:
                        | (keyof FSWatcherKnownEventMap)
                        | "add"
                        | "change"
                        | "addDir"
                        | "unlink"
                        | "unlinkDir";
                    filePath: string;
                },
            ) => Promise<void>;
        }

        The options for the watcher.

        • dirPath: string

          The path of the directory to watch.

        • event:
              | (keyof FSWatcherKnownEventMap)
              | "add"
              | "change"
              | "addDir"
              | "unlink"
              | "unlinkDir"

          The type of event to listen for.

        • OptionalonEvent?: (
              args: {
                  event:
                      | (keyof FSWatcherKnownEventMap)
                      | "add"
                      | "change"
                      | "addDir"
                      | "unlink"
                      | "unlinkDir";
                  filePath: string;
              },
          ) => Promise<void>

          Optional callback function to handle file system events.

      Returns void

    • Writes an object to a JSON file, appending it to an existing array if the file already exists.

      Type Parameters

      • T

      Parameters

      • params: { data: T; filePath: string }

      Returns Promise<void>

      An error if the file cannot be written or if the content is not an array.

    • Overwrites a JSON file with a new array of objects.

      Type Parameters

      • T

      Parameters

      • params: { data: T[]; filePath: string }

      Returns Promise<void>

      An error if the file cannot be written or if the content is not an array.