diff --git a/docker/web/nginx.conf b/docker/web/nginx.conf index 012d6fa6..969cb6e1 100644 --- a/docker/web/nginx.conf +++ b/docker/web/nginx.conf @@ -6,6 +6,16 @@ upstream s3 { server files:80 fail_timeout=0; } +map $uri $custom_content_type { + default "text/html"; + ~(.*\.png)$ "image/png"; + ~(.*\.jpe?g)$ "image/jpeg"; + ~(.*\.gif)$ "image/gif"; + ~(.*\.svg)$ "image/svg+xml"; + ~(.*\.mp4)$ "video/mp4"; + ~(.*\.webm)$ "video/webm"; +} + server { listen 80 default; listen [::]:80; @@ -19,6 +29,8 @@ server { expires max; add_header Cache-Control public; proxy_pass http://s3/$BUCKET_NAME/images/$1/$2/full.$3; + proxy_hide_header Content-Type; + add_header Content-Type $custom_content_type; } location ~ ^/img/download/(.+)/([0-9]+).*\.([A-Za-z0-9]+)$ { @@ -26,36 +38,48 @@ server { expires max; add_header Cache-Control public; proxy_pass http://s3/$BUCKET_NAME/images/$1/$2/full.$3; + proxy_hide_header Content-Type; + add_header Content-Type $custom_content_type; } location ~ ^/img/(.+) { expires max; add_header Cache-Control public; proxy_pass http://s3/$BUCKET_NAME/images/$1; + proxy_hide_header Content-Type; + add_header Content-Type $custom_content_type; } location ~ ^/spns/(.+) { expires max; add_header Cache-Control public; proxy_pass http://s3/$BUCKET_NAME/adverts/$1; + proxy_hide_header Content-Type; + add_header Content-Type $custom_content_type; } location ~ ^/avatars/(.+) { expires max; add_header Cache-Control public; proxy_pass http://s3/$BUCKET_NAME/avatars/$1; + proxy_hide_header Content-Type; + add_header Content-Type $custom_content_type; } location ~ ^/badges/(.+) { expires max; add_header Cache-Control public; proxy_pass http://s3/$BUCKET_NAME/badges/$1; + proxy_hide_header Content-Type; + add_header Content-Type $custom_content_type; } location ~ ^/tags/(.+) { expires max; add_header Cache-Control public; proxy_pass http://s3/$BUCKET_NAME/tags/$1; + proxy_hide_header Content-Type; + add_header Content-Type $custom_content_type; } location / { diff --git a/lib/philomena/images/thumbnailer.ex b/lib/philomena/images/thumbnailer.ex index 2f4236de..9d80a070 100644 --- a/lib/philomena/images/thumbnailer.ex +++ b/lib/philomena/images/thumbnailer.ex @@ -8,6 +8,7 @@ defmodule Philomena.Images.Thumbnailer do alias Philomena.Images.Image alias Philomena.Processors alias Philomena.Analyzers + alias Philomena.Uploader alias Philomena.Sha512 alias Philomena.Repo alias ExAws.S3 @@ -131,10 +132,7 @@ defmodule Philomena.Images.Thumbnailer do def upload_file(image, file, version_name) do path = Path.join(image_thumb_prefix(image), version_name) - file - |> S3.Upload.stream_file() - |> S3.upload(bucket(), path, @acl) - |> ExAws.request!() + Uploader.persist_file(path, file) end defp bulk_rename(file_names, source_prefix, target_prefix) do diff --git a/lib/philomena/uploader.ex b/lib/philomena/uploader.ex index effb31a3..1b02a4b6 100644 --- a/lib/philomena/uploader.ex +++ b/lib/philomena/uploader.ex @@ -6,6 +6,7 @@ defmodule Philomena.Uploader do alias Philomena.Filename alias Philomena.Analyzers alias Philomena.Sha512 + alias Philomena.Mime alias ExAws.S3 import Ecto.Changeset @@ -64,9 +65,19 @@ defmodule Philomena.Uploader do dest = Map.get(model, field(field_name)) target = Path.join(file_root, dest) - source + persist_file(target, source) + end + + @doc """ + Persist an arbitrary file to storage at the given path with the correct + content type and permissions. + """ + def persist_file(path, file) do + {_, mime} = Mime.file(path) + + file |> S3.Upload.stream_file() - |> S3.upload(bucket(), target, acl: :public_read) + |> S3.upload(bucket(), path, acl: :public_read, content_type: mime) |> ExAws.request!() end