-
Notifications
You must be signed in to change notification settings - Fork 341
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2876dfe
commit 8dff421
Showing
6 changed files
with
117 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
defmodule Bamboo.Attachment do | ||
@moduledoc """ | ||
""" | ||
|
||
defstruct filename: nil, content_type: nil, path: nil | ||
|
||
@doc ~S""" | ||
Creates a new Attachment | ||
Examples: | ||
Attachment.new("/path/to/attachment.png") | ||
Attachment.new("/path/to/attachment.png", filename: "image.png") | ||
Attachment.new("/path/to/attachment.png", filename: "image.png", content_type: "image/png") | ||
Attachment.new(params["file"]) # Where params["file"] is a %Plug.Upload | ||
""" | ||
def new(path, opts \\ []) | ||
if Code.ensure_loaded?(Plug) do | ||
def new(%Plug.Upload{filename: filename, content_type: content_type, path: path}, opts), do: | ||
new(path, Dict.merge([filename: filename, content_type: content_type], opts)) | ||
end | ||
def new(path, opts) do | ||
filename = opts[:filename] || Path.basename(path) | ||
content_type = opts[:content_type] || determine_content_type(path) | ||
%__MODULE__{path: path, filename: filename, content_type: content_type} | ||
end | ||
|
||
defp determine_content_type(path) do | ||
if Code.ensure_loaded?(Plug) do | ||
Plug.MIME.path(path) | ||
else | ||
"application/octet-stream" | ||
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
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,53 @@ | ||
defmodule Bamboo.AttachmentTest do | ||
use ExUnit.Case | ||
|
||
alias Bamboo.Attachment | ||
|
||
test "create an attachment" do | ||
path = Path.join(__DIR__, "../../support/attachment.docx") | ||
attachment = Attachment.new(path) | ||
assert attachment.content_type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document" | ||
assert attachment.filename == "attachment.docx" | ||
assert attachment.path == path | ||
end | ||
|
||
test "create an attachment with an unknown content type" do | ||
path = Path.join(__DIR__, "../../support/attachment.unknown") | ||
attachment = Attachment.new(path) | ||
assert attachment.content_type == "application/octet-stream" | ||
end | ||
|
||
test "create an attachment with a specified file name" do | ||
path = Path.join(__DIR__, "../../support/attachment.docx") | ||
attachment = Attachment.new(path, filename: "my-test-name.doc") | ||
assert attachment.filename == "my-test-name.doc" | ||
end | ||
|
||
test "create an attachment with a specified content type" do | ||
path = Path.join(__DIR__, "../../support/attachment.docx") | ||
attachment = Attachment.new(path, content_type: "application/msword") | ||
assert attachment.content_type == "application/msword" | ||
end | ||
|
||
test "create an attachment from a Plug Upload struct" do | ||
path = Path.join(__DIR__, "../../support/attachment.docx") | ||
upload = %Plug.Upload{filename: "test.docx", | ||
content_type: "application/msword", | ||
path: path} | ||
attachment = Attachment.new(upload) | ||
assert attachment.content_type == "application/msword" | ||
assert attachment.filename == "test.docx" | ||
assert attachment.path == path | ||
end | ||
|
||
test "create an attachment from a Plug Upload struct with overrides" do | ||
path = Path.join(__DIR__, "../../support/attachment.docx") | ||
upload = %Plug.Upload{filename: "test.docx", | ||
content_type: "application/msword", | ||
path: path} | ||
attachment = Attachment.new(upload, filename: "my-attachment.doc", content_type: "application/other") | ||
assert attachment.content_type == "application/other" | ||
assert attachment.filename == "my-attachment.doc" | ||
assert attachment.path == path | ||
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
Binary file not shown.
Empty file.