Skip to content

Commit

Permalink
Updated to use the GridFS API in PyMongo >= 1.6
Browse files Browse the repository at this point in the history
Updated every file access to use the latest API. The file() view has
been modified so that the urls.py file in exactly the same as before.
  • Loading branch information
bolsote committed May 5, 2011
1 parent e069ab7 commit 56ed1ea
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
36 changes: 20 additions & 16 deletions status/views.py
Expand Up @@ -14,6 +14,8 @@

db = Connection().sms
fs = gridfs.GridFS(db)
thumbs = gridfs.GridFS(db, collection='thumb')

page_size = 10

def _generate_filename(name):
Expand All @@ -28,11 +30,15 @@ def _generate_filename(name):
filename = ".".join(filename)

# Try again if this filename already exists
try:
fs.open(filename, "r")
return _generate_filename(name)
except IOError:
return filename
#try:
# fs.open(filename, "r")
# return _generate_filename(name)
#except IOError:
# return filename
if fs.exists(filename=filename):
return _generate_filename(name)
else:
return filename

def index(request, page=0):
if request.method == 'POST':
Expand All @@ -45,6 +51,7 @@ def index(request, page=0):

if "image" in request.FILES:
filename = _generate_filename(request.FILES['image'].name)
mimetype = request.FILES['image'].content_type

# Only accept appropriate file extensions
if not filename.endswith((".jpg", ".JPG", ".jpeg", ".JPEG", ".png",
Expand All @@ -55,18 +62,14 @@ def index(request, page=0):

# Save fullsize image
image = request.FILES['image'].read()
full = fs.open(filename, "w")
full.write(image)
full.close()
fs.put(image, content_type=mimetype, filename=filename)

# Save thumbnail
thumb = fs.open(filename, "w", "thumb")
image = Image.open(StringIO.StringIO(image))
image.thumbnail((80, 60), Image.ANTIALIAS)
data = StringIO.StringIO()
image.save(data, image.format)
thumb.write(data.getvalue())
thumb.close()
thumbs.put(data.getvalue(), content_type=mimetype, filename=filename)
data.close()

db.messages.insert(message)
Expand All @@ -89,10 +92,11 @@ def index(request, page=0):

def file(request, collection_or_filename, filename=None):
if filename is not None:
f = fs.open(filename, "r", collection_or_filename)
if collection_or_filename == "thumb":
data = thumbs.get_last_version(filename)
else:
data = fs.get_last_version(filename)
else:
f = fs.open(collection_or_filename, "r")
data = f.read()
mimetype = f.content_type or mimetypes.guess_type(filename or collection_or_filename)
f.close()
data = fs.get_last_version(collection_or_filename)
mimetype = data.content_type or mimetypes.guess_type(filename or collection_or_filename)
return HttpResponse(data, mimetype=mimetype)
2 changes: 1 addition & 1 deletion templates/status/index.html
Expand Up @@ -52,7 +52,7 @@
<td width="90px">
{% if message.image %}
<div class="message-thumbnail">
<a href="/image/{{message.image}}" onclick="return popUp(\"/image/{{message.image}}\")">
<a href="/image/{{message.image}}" onclick="return popUp(\"/image/thumb/{{message.image}}\")">
<img src="/image/thumb/{{message.image}}" alt=""/>
</a>
</div>
Expand Down

0 comments on commit 56ed1ea

Please sign in to comment.