Depot v0.2.1 Depot View Source

Depot is a filesystem abstraction for elixir providing a unified interface over many implementations. It allows you to swap out filesystems on the fly without needing to rewrite all of your application code in the process. It can eliminate vendor-lock in, reduce technical debt, and improve the testability of your code.

This library is based on the ideas of flysystem, which is a PHP library providing similar functionality.

Examples

defmodule LocalFileSystem do
  use Depot.Filesystem,
    adapter: Depot.Adapter.Local,
    prefix: prefix
end

LocalFileSystem.write("test.txt", "Hello World")
{:ok, "Hello World"} = LocalFileSystem.read("test.txt")

Link to this section Summary

Functions

Copy a file from source to destination on a filesystem

Delete a file from a filesystem

Copy a file from source to destination on a filesystem

List the contents of a folder on a filesystem

Move a file from source to destination on a filesystem

Read from a filesystem

Link to this section Types

Specs

adapter() :: module()

Specs

filesystem() :: {module(), Depot.Adapter.config()}

Link to this section Functions

Link to this function

copy(arg, source, destination, opts \\ [])

View Source

Specs

copy(filesystem(), Path.t(), Path.t(), keyword()) :: :ok | {:error, term()}

Copy a file from source to destination on a filesystem

Examples

Direct filesystem

filesystem = Depot.Adapter.Local.configure(prefix: "/home/user/storage")
:ok = Depot.copy(filesystem, "test.txt", "other-test.txt")

Module-based filesystem

defmodule LocalFileSystem do
  use Depot.Filesystem,
    adapter: Depot.Adapter.Local,
    prefix: "/home/user/storage"
end

:ok = LocalFileSystem.copy("test.txt", "other-test.txt")
Link to this function

delete(arg, path, opts \\ [])

View Source

Specs

delete(filesystem(), Path.t(), keyword()) :: :ok | {:error, term()}

Delete a file from a filesystem

Examples

Direct filesystem

filesystem = Depot.Adapter.Local.configure(prefix: "/home/user/storage")
:ok = Depot.delete(filesystem, "test.txt")

Module-based filesystem

defmodule LocalFileSystem do
  use Depot.Filesystem,
    adapter: Depot.Adapter.Local,
    prefix: "/home/user/storage"
end

:ok = LocalFileSystem.delete("test.txt")
Link to this function

file_exists(arg, path, opts \\ [])

View Source

Specs

file_exists(filesystem(), Path.t(), keyword()) ::
  {:ok, :exists | :missing} | {:error, term()}

Copy a file from source to destination on a filesystem

Examples

Direct filesystem

filesystem = Depot.Adapter.Local.configure(prefix: "/home/user/storage")
:ok = Depot.copy(filesystem, "test.txt", "other-test.txt")

Module-based filesystem

defmodule LocalFileSystem do
  use Depot.Filesystem,
    adapter: Depot.Adapter.Local,
    prefix: "/home/user/storage"
end

:ok = LocalFileSystem.copy("test.txt", "other-test.txt")
Link to this function

list_contents(arg, path, opts \\ [])

View Source

Specs

list_contents(filesystem(), Path.t(), keyword()) ::
  {:ok,
   [
     %Depot.Stat.Dir{mtime: term(), name: term(), size: term()}
     | %Depot.Stat.File{mtime: term(), name: term(), size: term()}
   ]}
  | {:error, term()}

List the contents of a folder on a filesystem

Examples

Direct filesystem

filesystem = Depot.Adapter.Local.configure(prefix: "/home/user/storage")
{:ok, contents} = Depot.list_contents(filesystem, ".")

Module-based filesystem

defmodule LocalFileSystem do
  use Depot.Filesystem,
    adapter: Depot.Adapter.Local,
    prefix: "/home/user/storage"
end

{:ok, contents} = LocalFileSystem.list_contents(".")
Link to this function

move(arg, source, destination, opts \\ [])

View Source

Specs

move(filesystem(), Path.t(), Path.t(), keyword()) :: :ok | {:error, term()}

Move a file from source to destination on a filesystem

Examples

Direct filesystem

filesystem = Depot.Adapter.Local.configure(prefix: "/home/user/storage")
:ok = Depot.move(filesystem, "test.txt", "other-test.txt")

Module-based filesystem

defmodule LocalFileSystem do
  use Depot.Filesystem,
    adapter: Depot.Adapter.Local,
    prefix: "/home/user/storage"
end

:ok = LocalFileSystem.move("test.txt", "other-test.txt")
Link to this function

read(arg, path, opts \\ [])

View Source

Specs

read(filesystem(), Path.t(), keyword()) :: {:ok, binary()} | {:error, term()}

Read from a filesystem

Examples

Direct filesystem

filesystem = Depot.Adapter.Local.configure(prefix: "/home/user/storage")
{:ok, "Hello World"} = Depot.read(filesystem, "test.txt")

Module-based filesystem

defmodule LocalFileSystem do
  use Depot.Filesystem,
    adapter: Depot.Adapter.Local,
    prefix: "/home/user/storage"
end

{:ok, "Hello World"} = LocalFileSystem.read("test.txt")
Link to this function

write(arg, path, contents, opts \\ [])

View Source

Specs

write(filesystem(), Path.t(), iodata(), keyword()) :: :ok | {:error, term()}

Write to a filesystem

Examples

Direct filesystem

filesystem = Depot.Adapter.Local.configure(prefix: "/home/user/storage")
:ok = Depot.write(filesystem, "test.txt", "Hello World")

Module-based filesystem

defmodule LocalFileSystem do
  use Depot.Filesystem,
    adapter: Depot.Adapter.Local,
    prefix: "/home/user/storage"
end

LocalFileSystem.write("test.txt", "Hello World")