This repository has been archived by the owner on Mar 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #79 from infinitered/feature/new-image-uploader-op…
…tion adds feature to store files in the database as blob
- Loading branch information
Showing
23 changed files
with
296 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
apps/example/priv/repo/migrations/20161123212846_create_thesis_files_table.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
defmodule Example.Repo.Migrations.CreateThesisFilesTable do | ||
@moduledoc false | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table(:thesis_files) do | ||
add :slug, :string | ||
add :content_type, :string | ||
add :filename, :string | ||
add :data, :binary | ||
|
||
timestamps | ||
end | ||
create unique_index(:thesis_files, [:slug]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
defmodule Thesis.File do | ||
@moduledoc """ | ||
Represents a file. | ||
""" | ||
use Ecto.Schema | ||
import Ecto.Changeset, only: [cast: 3, validate_required: 2] | ||
import Thesis.Utilities | ||
|
||
@type t :: %Thesis.File{ | ||
id: any, | ||
slug: String.t, | ||
content_type: String.t, | ||
filename: String.t, | ||
data: any, | ||
inserted_at: any, | ||
updated_at: any | ||
} | ||
|
||
schema "thesis_files" do | ||
field :slug, :string | ||
field :content_type, :string | ||
field :filename, :string | ||
field :data, :binary | ||
|
||
timestamps | ||
end | ||
|
||
@valid_attributes [:slug, :content_type, :filename, :data] | ||
@required_attributes [:slug, :content_type, :filename, :data] | ||
|
||
@doc """ | ||
Changeset for File structs. | ||
""" | ||
def changeset(file, %Plug.Upload{} = f) do | ||
file | ||
|> cast(%{content_type: f.content_type}, [:content_type]) | ||
|> cast(%{filename: f.filename}, [:filename]) | ||
|> cast(%{data: File.read!(f.path)}, [:data]) | ||
|> cast(%{slug: generate_slug(f.filename)}, [:slug]) | ||
|> validate_required(@required_attributes) | ||
end | ||
|
||
defp generate_slug(name) do | ||
random_string(10) <> "-" <> parameterize(name) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
defmodule Thesis.RepoUploader do | ||
@moduledoc """ | ||
Handles file uploads | ||
""" | ||
|
||
@behaviour Thesis.Uploader | ||
|
||
import Thesis.Config | ||
import Ecto.Query, only: [from: 2] | ||
alias Thesis.File | ||
|
||
def upload(file) do | ||
changeset = File.changeset(%File{}, file) | ||
do_upload(changeset) | ||
end | ||
|
||
defp do_upload(changeset) do | ||
case repo.insert_or_update(changeset) do | ||
{:ok, file} -> {:ok, "/thesis/files/" <> file.slug} | ||
{:error, changeset} -> {:error, changeset} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
defmodule Thesis.Utilities do | ||
@moduledoc """ | ||
Module that provides helper functions. | ||
""" | ||
|
||
def parameterize(str) do | ||
str = Regex.replace(~r/[^a-z0-9\-\s\.]/i, str, "") | ||
Regex.split(~r/\%20|\s/, str) | ||
|> Enum.join("-") | ||
|> String.downcase | ||
end | ||
|
||
def random_string(length) do | ||
length | ||
|> :crypto.strong_rand_bytes | ||
|> Base.url_encode64 | ||
|> String.replace("_", "") | ||
|> String.downcase | ||
|> binary_part(0, length) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.