diff core/download.py @ 973:6f55c086db1e

Guess file extension based on content-type. When downloading a file, and no path is supplied to store it, guess the file extension using mimetypes and the content-type header. Also supply a unit test for the HotLinkImageForm.
author Brian Neal <bgneal@gmail.com>
date Thu, 01 Oct 2015 19:44:45 -0500
parents 7138883966b3
children f5aa74dcdd7a
line wrap: on
line diff
--- a/core/download.py	Wed Sep 23 21:26:09 2015 -0500
+++ b/core/download.py	Thu Oct 01 19:44:45 2015 -0500
@@ -1,6 +1,7 @@
 """This module contains routines for downloading files."""
 
 import logging
+import mimetypes
 import os
 import shutil
 import tempfile
@@ -35,7 +36,11 @@
     # Save file data
 
     if not path:
-        fd, path = tempfile.mkstemp()
+        content_type = r.headers.get('content-type')
+        suffix = mimetypes.guess_extension(content_type) if content_type else ''
+        if suffix == '.jpe':
+            suffix = '.jpg'
+        fd, path = tempfile.mkstemp(suffix=suffix)
         os.close(fd)
 
     try: