forked from code-corps/code-corps-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase64_image.ex
More file actions
38 lines (31 loc) · 939 Bytes
/
base64_image.ex
File metadata and controls
38 lines (31 loc) · 939 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
defmodule CodeCorps.Base64Image do
def save_to_file("data:" <> data_string) do
[content_type, content_string] =
data_string
|> String.split(";base64,")
path_to_image =
content_string
|> Base.decode64!
|> save_as_image(content_type)
{path_to_image, content_type}
end
defp save_as_image(content, content_type) do
extension = infer_extension(content_type)
filename = random_filename(8, extension)
path_to_image = [ensure_tmp_dir, filename] |> Path.join
path_to_image |> File.write!(content)
path_to_image
end
defp infer_extension("image/" <> extension), do: extension
defp random_filename(length, extension), do: random_string(length) <> "." <> extension
defp random_string(length) do
length
|> :crypto.strong_rand_bytes
|> Base.url_encode64
|> binary_part(0, length)
end
defp ensure_tmp_dir do
"tmp" |> File.mkdir
"tmp"
end
end