PhoenixKitProjects.Attachments (PhoenixKitProjects v0.21.0)

Copy Markdown View Source

Folder-scoped file attachments for a project, backed by core PhoenixKit.Modules.Storage — the workspace's per-resource-folder convention (phoenix_kit_staff.Attachments is the reference this mirrors): no module-owned table, no migration.

Each project owns a deterministic root folder project-<uuid>, resolved by name on every read (never cached on the project row, so renames or deletions in /admin/media can't strand a dangling uuid) and created lazily on first use. Core's [:name, :parent_uuid] unique index makes find-or-create race-safe. Files live in core phoenix_kit_files — a file is IN the folder either as its home (file.folder_uuid) or via a FolderLink. Removal follows core's non-destructive convention: soft-trash a sole-home file, promote a link on a shared one, drop the link on a linked-only one — never hard-delete a possibly-shared asset.

Summary

Functions

Links picked/uploaded files into the project folder: a homeless file gets this folder as home; a file homed elsewhere gains a FolderLink (idempotent per file).

Public download URL, nil-safe.

Find-or-create the project folder. Race-safe: a lost create re-resolves the winner via the unique index.

Heroicon name for a file's type (template helper).

Deterministic root folder name for a project's files.

Resolves the project folder uuid WITHOUT creating it (render-safe).

Active files in the project folder (home or linked), newest first, capped.

Removes a file from the project folder. Home here + not linked elsewhere → soft-trash (recoverable in the media trash); home here + linked elsewhere → promote a link to home; linked-only here → drop the link.

Functions

attach_files(project_uuid, file_uuids)

@spec attach_files(binary(), [binary()]) :: :ok

Links picked/uploaded files into the project folder: a homeless file gets this folder as home; a file homed elsewhere gains a FolderLink (idempotent per file).

download_url(file)

@spec download_url(PhoenixKit.Modules.Storage.File.t()) :: String.t() | nil

Public download URL, nil-safe.

ensure_folder(project_uuid)

@spec ensure_folder(binary()) :: {:ok, binary()} | {:error, term()}

Find-or-create the project folder. Race-safe: a lost create re-resolves the winner via the unique index.

file_icon(arg1)

@spec file_icon(map()) :: String.t()

Heroicon name for a file's type (template helper).

folder_name(project_uuid)

@spec folder_name(binary()) :: binary()

Deterministic root folder name for a project's files.

folder_uuid(project_uuid)

@spec folder_uuid(binary()) :: binary() | nil

Resolves the project folder uuid WITHOUT creating it (render-safe).

list_files(project_uuid)

@spec list_files(binary()) :: [PhoenixKit.Modules.Storage.File.t()]

Active files in the project folder (home or linked), newest first, capped.

remove_file(project_uuid, file_uuid)

@spec remove_file(binary(), binary()) :: :ok | {:error, term()}

Removes a file from the project folder. Home here + not linked elsewhere → soft-trash (recoverable in the media trash); home here + linked elsewhere → promote a link to home; linked-only here → drop the link.