{"id":18608829,"url":"https://github.com/recruitee/bow","last_synced_at":"2025-04-10T22:30:57.972Z","repository":{"id":26443083,"uuid":"102883886","full_name":"Recruitee/bow","owner":"Recruitee","description":"File uploads for Elixir (with Ecto support)","archived":false,"fork":false,"pushed_at":"2023-08-14T10:16:03.000Z","size":1248,"stargazers_count":26,"open_issues_count":3,"forks_count":4,"subscribers_count":28,"default_branch":"master","last_synced_at":"2024-03-15T06:04:20.683Z","etag":null,"topics":["attachment","ecto","elixir","upload"],"latest_commit_sha":null,"homepage":"","language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Recruitee.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-09-08T16:48:09.000Z","updated_at":"2024-07-11T13:37:44.051Z","dependencies_parsed_at":"2024-07-11T13:52:29.662Z","dependency_job_id":null,"html_url":"https://github.com/Recruitee/bow","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Recruitee%2Fbow","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Recruitee%2Fbow/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Recruitee%2Fbow/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Recruitee%2Fbow/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Recruitee","download_url":"https://codeload.github.com/Recruitee/bow/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248309409,"owners_count":21082206,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["attachment","ecto","elixir","upload"],"created_at":"2024-11-07T03:04:26.057Z","updated_at":"2025-04-10T22:30:57.292Z","avatar_url":"https://github.com/Recruitee.png","language":"Elixir","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Bow\n\nFile uploads for Elixir\n\n## Features\n\n- Generate multiple dependent versions of a file\n- Integration with Ecto\n- Allow downloading remote files (`remote_avatar_url` params etc.)\n- Multiple storage adapters (local disk, Amazon S3)\n\n## Installation\n\n```elixir\ndef deps do\n  [\n    {:bow, \"~\u003e 0.4.3\"},\n\n    # for AWS S3 support\n    {:ex_aws, \"~\u003e 2.0\"},\n    {:ex_aws_s3, \"~\u003e 2.0\"},\n\n    # for Bow.Exec\n    {:erlexec,  \"~\u003e 1.7.0\"}\n  ]\nend\n```\n\n## Usage\n\n### Minimal uploader definition\n\n```elixir\ndefmodule MyUploader do\n  use Bow.Uploader\n\n  # specify storage directory\n  def store_dir(_file) do\n    \"uploads\"\n  end\nend\n```\n\n### Full uploader example\n\n```elixir\ndefmodule AttachmentUploader do\n  use Bow.Uploader\n\n  # define what versions to generate for given file\n  def versions(_file) do\n    [:original, :thumb]\n  end\n\n\n  # keep the origianal file name\n  def filename(file, :original), do: file.name\n\n  # prepend \"thumb_\" for thumbnail\n  def filename(file, :thumb),    do: \"thumb_\\#{file.name}\"\n\n\n  # do nothing with original file\n  def transform(file, :original), do: transform_original(file)\n\n  # generate image thumbnail\n  def transform(source, target, :thumb) do\n    Bow.Exec.exec source, target,\n      \"convert ${input} -strip -gravity Center -resize 250x175^ -extent 250x175 ${output}\"\n  end\n\n\n  # specify storage directory\n  def store_dir(file) do\n    \"attachments/\\#{file.scope.id}\"\n  end\n\n  # specify storage options\n  def store_options(_file) do\n    [acl: :public_read]\n  end\nend\n```\n\n### Usage with Ecto\n\n```elixir\n# Add `use Bow.Ecto` to the uploader\ndefmodule MyApp.UserAvatarUploader do\n  use Bow.Uploader\n  use Bow.Ecto # \u003c---- HERE\n\n  # file.scope will be the user struct\n  def store_dir(file) do\n    \"users/#{file.scope.id}/avatar\"\n  end\nend\n\n# add avatar field to users table\ndefmodule MyApp.Repo.Migrations.AddAvatarToUsers do\n  use Ecto.Migration\n\n  def change do\n    alter table(:users) do\n      add :avatar, :string\n    end\n  end\nend\n\n\n# use `MyApp.UserAvatarUploader.Type` as field type\ndefmodule MyApp.User do\n  schema \"users\" do\n    field :email, :string\n    field :avatar, MyApp.UserAvatarUploader.Type # \u003c-- HERE\n  end\n\n  def changeset(model \\\\ %__MODULE__{}, params) do\n    model\n    |\u003e cast(params, [:email, :avatar])\n    # uncomment to add support for remote_avatar_url params\n    # |\u003e Bow.Ecto.cast_uploads(params, [:avatar])\n    |\u003e Bow.Ecto.validate() # optional validation using uploader rules\n  end\nend\n\n\n# create user and save files\nchangeset = User.changeset(%User{}, params)\nwith {:ok, user}    \u003c- Repo.insert(changeset),\n     {:ok, user, _} \u003c- Bow.Ecto.store(user) do\n  {:ok, user}\nend\n```\n\n### Getting file URL\n\nWith standalone uploaders:\n\n```elixir\nfile = MyUploader.new(\"path/to/file.png\")\n\nBow.url(file)         # =\u003e url of original file\nBow.url(file, :thumb) # =\u003e url of thumb version\n```\n\nWith Ecto integration:\n\n```elixir\nuser = Repo.get(User, 1)\n\nBow.Ecto.url(user, :avatar) # url of avatar original\nBow.Ecto.url(user, :avatar, :thumb) # url of avatar thumb\nBow.Ecto.url(user, :photo, :thumb, signed: true) # you can pass storage-specific options\n```\n\n### Overwriting file name\n\nYou can change the file name using uploader's `cast/1` callback:\n\n```elixir\ndefmodule TimestampUploader do\n  use Bow.Uploader\n  use Bow.Ecto\n\n  def cast(file) do\n    # replace \"myfile.png\" with \"avatar_12343456.png\"\n    ts = DateTime.utc_now |\u003e DateTime.to_unix\n    Bow.set(file, :rootname, \"avatar_#{ts}\")\n  end\n\n  def store_dir(_file), do: \"timestamp\"\nend\n```\n\n### Validation\n\nYou can overwrite `validate/1` to add validations for e.g. allowed extension.\n\n```elixir\ndefmodule AvatarUploader do\n  # ...\n  def validate(%{ext: ext}) when ext in ~w(.jpg .png), do: :ok\n  def validate(_), do: {:error, :extension_not_allowed}\n  # ...\nend\n```\n\n### Using Bow in test environment\n\nIt is best to use local storage adapter when testing.\n\n```elixir\n# config/test.exs\nconfig :bow,\n  storage: Bow.Storage.Local,\n  storage_prefix: \"tmp/bow/\"\n```\n\n## Running Bow tests\n\n```bash\nmix test\n```\n\n#### Testing ecto integration\n\n```bash\n# edit config/config.exs\n\n# create test database\nMIX_ENV=test mix ecto.create\n\n# run tests\nmix test --only ecto\n```\n\n#### Testing S3 adapter\n\n```bash\n# install fake-s3 gem\ngem install fakes3\n\n# start fake-s3 server\nfakes3 -r tmp/s3 -p 4567\n\n# run tests\nmix test --only s3\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frecruitee%2Fbow","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frecruitee%2Fbow","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frecruitee%2Fbow/lists"}