Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable send_file function to send file via file_id(the hex str) #145

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions flask_pymongo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

from flask_pymongo.helpers import BSONObjectIdConverter, JSONEncoder
from flask_pymongo.wrappers import MongoClient

from bson import ObjectId

PY2 = sys.version_info[0] == 2

Expand Down Expand Up @@ -130,7 +130,7 @@ def init_app(self, app, uri=None, *args, **kwargs):
app.json_encoder = self._json_encoder

# view helpers
def send_file(self, filename, base="fs", version=-1, cache_for=31536000):
def send_file(self, filename=None, file_id=None, base="fs", version=-1, cache_for=31536000):
"""Respond with a file from GridFS.

Returns an instance of the :attr:`~flask.Flask.response_class`
Expand All @@ -144,6 +144,8 @@ def get_upload(filename):
return mongo.send_file(filename)

:param str filename: the filename of the file to return
:param str file_id: the file_id of the file to return. (only the hex
str eg: '507f191e810c19729de860ea')
:param str base: the base name of the GridFS collections to use
:param bool version: if positive, return the Nth revision of the file
identified by filename; if negative, return the Nth most recent
Expand All @@ -161,10 +163,16 @@ def get_upload(filename):
storage = GridFS(self.db, base)

try:
fileobj = storage.get_version(filename=filename, version=version)
if filename != None and file_id == None:
fileobj = storage.get_version(filename=filename, version=version)
elif file_id != None and filename == None:
fileobj = storage.get(ObjectId(file_id))
else:
abort(404)
except NoFile:
abort(404)


# mostly copied from flask/helpers.py, with
# modifications for GridFS
data = wrap_file(request.environ, fileobj, buffer_size=1024 * 255)
Expand Down