Skip to content

Commit

Permalink
Merge pull request #10 from 4Catalyzer/s3-content-type
Browse files Browse the repository at this point in the history
Fix content type handling on S3
  • Loading branch information
taion committed Sep 11, 2015
2 parents e024e01 + 148d43e commit 7d60b51
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions flask_annex/s3.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from boto.s3.connection import S3Connection
from boto.s3.key import Key
import flask
import mimetypes
from six import string_types
from werkzeug.datastructures import FileStorage

from .base import AnnexBase

Expand Down Expand Up @@ -49,17 +49,18 @@ def list_keys(self, prefix):
def save_file(self, key, in_file):
s3_key = self._get_s3_key(key)

# Get the content type from the key, rather than letting Boto try to
# figure it out from the file's name, which may be uninformative.
content_type = mimetypes.guess_type(key)[0]
if content_type:
headers = {'Content-Type': content_type}
else:
headers = None

if isinstance(in_file, string_types):
s3_key.set_contents_from_filename(in_file)
s3_key.set_contents_from_filename(in_file, headers=headers)
else:
if isinstance(in_file, FileStorage):
# Use filename for type inference instead of form name.
s3_key.path = in_file.filename
in_file = in_file.stream
else:
# Use key as fallback for guessing file type.
s3_key.path = key
s3_key.set_contents_from_file(in_file)
s3_key.set_contents_from_file(in_file, headers=headers)

def send_file(self, key):
s3_key = self._get_s3_key(key)
Expand Down

0 comments on commit 7d60b51

Please sign in to comment.