PhxMediaLibrary.PathGenerator.Default (PhxMediaLibrary v0.6.1)

Copy Markdown View Source

Default path generator for PhxMediaLibrary.

Produces paths in the following format:

  • Original file: {mediable_type}/{mediable_id}/{uuid}/{filename}
  • Conversion: {mediable_type}/{mediable_id}/{uuid}/{base}_{conversion}{ext}

Examples

iex> media = %PhxMediaLibrary.Media{
...>   mediable_type: "posts",
...>   mediable_id: "abc-123",
...>   uuid: "550e8400-e29b-41d4-a716-446655440000",
...>   file_name: "photo.jpg"
...> }
iex> PhxMediaLibrary.PathGenerator.Default.relative_path(media, nil)
"posts/abc-123/550e8400-e29b-41d4-a716-446655440000/photo.jpg"

iex> PhxMediaLibrary.PathGenerator.Default.relative_path(media, :thumb)
"posts/abc-123/550e8400-e29b-41d4-a716-446655440000/photo_thumb.jpg"

Configuration

config :phx_media_library,
  path_generator: PhxMediaLibrary.PathGenerator.Default

This is the default, so explicit configuration is only needed when switching back from a different generator.

Summary

Functions

Generate a storage path for new media (before it has been persisted).

Generate the relative storage path for a media item.

Functions

for_new_media(attrs)

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

Generate a storage path for new media (before it has been persisted).

Produces {mediable_type}/{mediable_id}/{uuid}/{filename}.

relative_path(media, conversion \\ nil)

@spec relative_path(PhxMediaLibrary.Media.t(), atom() | String.t() | nil) ::
  String.t()

Generate the relative storage path for a media item.

Produces {mediable_type}/{mediable_id}/{uuid}/{filename} for the original file and {mediable_type}/{mediable_id}/{uuid}/{base}_{conversion}{ext} for conversions.