Skip to content

Commit

Permalink
Merge pull request #9 from 4Catalyzer/file-type
Browse files Browse the repository at this point in the history
Improve handling of file types
  • Loading branch information
taion committed Sep 11, 2015
2 parents d38208a + cf66725 commit 2a61a53
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion flask_annex/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ def save_file(self, key, in_file):
raise NotImplementedError()

@abstractmethod
def send_file(self, key, **options):
def send_file(self, key):
raise NotImplementedError()
8 changes: 6 additions & 2 deletions flask_annex/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,9 @@ def save_file(self, key, in_file):
with open(out_filename, 'wb') as out_fp:
shutil.copyfileobj(in_file, out_fp)

def send_file(self, key, **options):
return flask.send_from_directory(self._root_path, key, **options)
def send_file(self, key):
return flask.send_from_directory(
self._root_path, key,
as_attachment=True,
attachment_filename=os.path.basename(key)
)
10 changes: 9 additions & 1 deletion flask_annex/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from boto.s3.key import Key
import flask
from six import string_types
from werkzeug.datastructures import FileStorage

from .base import AnnexBase

Expand Down Expand Up @@ -51,8 +52,15 @@ def save_file(self, key, in_file):
if isinstance(in_file, string_types):
s3_key.set_contents_from_filename(in_file)
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)

def send_file(self, key, **options):
def send_file(self, key):
s3_key = self._get_s3_key(key)
return flask.redirect(s3_key.generate_url(self._url_expires_in))

0 comments on commit 2a61a53

Please sign in to comment.