Search documentation of phoenix_kit

# PhoenixKit.Pages.FileOperations (phoenix_kit v1.6.19)

View Source

Filesystem operations for Pages module.

Handles reading, writing, listing files and folders.

# Summary

## Functions

absolute_path(relative_path)

Returns the absolute filesystem path for a relative page path.

copy(source_path, dest_path)

Copies a file to a new location.

count_contents(relative_path)

Counts files and folders inside a directory recursively.

create_directory(relative_path)

Creates a new directory.

delete(relative_path)

Deletes a file or directory.

directory_exists?(relative_path)

Checks if a directory exists at the path.

exists?(relative_path)

Checks if a path exists (file or folder).

file_exists?(relative_path)

Checks if a file (not folder) exists at the path.

file_info(relative_path)

Gets file info (size, modified time, etc).

generate_unique_name(relative_path)

Generates a unique filename by adding a number suffix if needed.

list_directory(relative_path \\ "/")

Lists all files and folders in a directory.

move(source_path, dest_path)

Moves a file or directory to a new location.

read_file(relative_path)

Reads file content.

write_file(relative_path, content)

Writes content to a file.

# Functions

# absolute_path(relative_path)

Returns the absolute filesystem path for a relative page path.

# copy(source_path, dest_path)

Copies a file to a new location.

Creates parent directories if they don't exist.

## Examples

    iex> FileOperations.copy("/hello.md", "/blog/hello.md")
    :ok

# count_contents(relative_path)

Counts files and folders inside a directory recursively.

Returns a map with :files and :folders counts.

## Examples

    iex> FileOperations.count_contents("/blog")
    %{files: 5, folders: 2}

# create_directory(relative_path)

Creates a new directory.

## Examples

    iex> FileOperations.create_directory("/blog")
    :ok

# delete(relative_path)

Deletes a file or directory.

Directories are deleted recursively.

## Examples

    iex> FileOperations.delete("/hello.md")
    :ok

    iex> FileOperations.delete("/blog")
    :ok

# directory_exists?(relative_path)

Checks if a directory exists at the path.

## Examples

    iex> FileOperations.directory_exists?("/blog")
    true

    iex> FileOperations.directory_exists?("/hello.md")
    false

# exists?(relative_path)

Checks if a path exists (file or folder).

## Examples

    iex> FileOperations.exists?("/hello.md")
    true

# file_exists?(relative_path)

Checks if a file (not folder) exists at the path.

## Examples

    iex> FileOperations.file_exists?("/hello.md")
    true

    iex> FileOperations.file_exists?("/blog")
    false

# file_info(relative_path)

Gets file info (size, modified time, etc).

## Examples

    iex> FileOperations.file_info("/hello.md")
    {:ok, %{size: 1024, mtime: ~U[2025-01-01 00:00:00Z]}}

# generate_unique_name(relative_path)

Generates a unique filename by adding a number suffix if needed.

## Examples

    iex> FileOperations.generate_unique_name("/hello.md")
    "/hello-2.md"  # if /hello.md exists

    iex> FileOperations.generate_unique_name("/blog")
    "/blog-2"  # if /blog exists

# list_directory(relative_path \\ "/")

Lists all files and folders in a directory.

Returns a list of maps with name, type (:file or :folder), and path.

## Examples

    iex> FileOperations.list_directory("/")
    {:ok, [
      %{name: "blog", type: :folder, path: "/blog"},
      %{name: "hello.md", type: :file, path: "/hello.md"}
    ]}

# move(source_path, dest_path)

Moves a file or directory to a new location.

Creates parent directories if they don't exist.

## Examples

    iex> FileOperations.move("/hello.md", "/blog/hello.md")
    :ok

# read_file(relative_path)

Reads file content.

## Examples

    iex> FileOperations.read_file("/hello.md")
    {:ok, "# Hello World"}

# write_file(relative_path, content)

Writes content to a file.

Creates parent directories if they don't exist.

## Examples

    iex> FileOperations.write_file("/hello.md", "# Hello World")
    :ok