PhoenixKit.Modules.Storage (phoenix_kit v1.5.1)

View Source

Storage Module for PhoenixKit.

This module provides a distributed file storage system with redundancy support. Files can be stored across multiple locations (local, S3, remote servers) with automatic failover if a storage location becomes unavailable.

Features

  • Multi-location storage: Store files in 1-5 redundant locations
  • Automatic failover: If one location fails, automatically use another
  • Local storage: Default storage to priv/uploads
  • S3 support: Optional AWS S3 integration (future)
  • Remote servers: Support for distributed storage across multiple servers (future)

Module Status

This module is always enabled and cannot be disabled. It provides core functionality for file management across PhoenixKit.

Configuration

The module uses the following settings stored in the database:

  • storage_default_path - Default local storage path (default: "priv/uploads")

Usage

# Get storage configuration
config = PhoenixKit.Modules.Storage.get_config()
# => %{default_path: "priv/uploads"}

# Get default storage path
path = PhoenixKit.Modules.Storage.get_default_path()
# => "priv/uploads"

Summary

Functions

Creates a directory at the specified path.

Gets the absolute storage path for local file uploads.

Gets the configuration for the Storage module.

Gets the default storage path for local file uploads (relative path).

Checks if the Storage module is enabled.

Updates the default storage path (stores relative path).

Validates and normalizes a storage path.

Functions

create_directory(path)

Creates a directory at the specified path.

Creates all parent directories if they don't exist (mkdir -p behavior).

Examples

iex> PhoenixKit.Modules.Storage.create_directory("/path/to/new/dir")
{:ok, "/path/to/new/dir"}

iex> PhoenixKit.Modules.Storage.create_directory("/invalid/path")
{:error, "Failed to create directory: eacces"}

get_absolute_path()

Gets the absolute storage path for local file uploads.

Expands the relative path from settings to an absolute path.

Examples

iex> PhoenixKit.Modules.Storage.get_absolute_path()
"/Users/don/Projects/pk/phoenix_kit/priv/uploads"

get_config()

Gets the configuration for the Storage module.

Returns a map with:

  • module_enabled - Always true (non-disablable module)
  • default_path - Default local storage path

Examples

iex> PhoenixKit.Modules.Storage.get_config()
%{
  module_enabled: true,
  default_path: "priv/uploads"
}

get_default_path()

Gets the default storage path for local file uploads (relative path).

Returns the configured relative path or the default "priv/uploads" if not set.

Examples

iex> PhoenixKit.Modules.Storage.get_default_path()
"priv/uploads"

module_enabled?()

Checks if the Storage module is enabled.

This module is always enabled and cannot be disabled.

Examples

iex> PhoenixKit.Modules.Storage.module_enabled?()
true

update_default_path(path)

Updates the default storage path (stores relative path).

Examples

iex> PhoenixKit.Modules.Storage.update_default_path("uploads")
{:ok, %PhoenixKit.Settings.Setting{}}

validate_and_normalize_path(path)

Validates and normalizes a storage path.

Takes a path (absolute or relative), validates it exists and is writable, and returns the relative path for storage.

Examples

iex> PhoenixKit.Modules.Storage.validate_and_normalize_path("/full/path/to/storage")
{:ok, "relative/path/to/storage"}

iex> PhoenixKit.Modules.Storage.validate_and_normalize_path("/nonexistent")
{:error, "Directory does not exist: /nonexistent"}