Deal with content type screwery

This commit is contained in:
byte[] 2022-02-10 19:13:14 -05:00
parent 7094f14072
commit fd758b7f0d
3 changed files with 39 additions and 6 deletions

View file

@ -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 / {

View file

@ -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

View file

@ -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