Commits

Matthew Schinckel  committed 803ffa6

Handle iterable files in static uploads.

  • Participants
  • Parent commits bc531a2

Comments (0)

Files changed (1)

File rest_api/middleware.py

 class ContentHashMiddleware(object):
     def process_response(self, request, response):
         response['Content-Hash'] = hashlib.md5(response.content).hexdigest()
-        response['Content-Length'] = len(response.content)
+        try:
+            response.content.seek(0)
+        except:
+            pass
         return response
 
 class HttpLoggingMiddleware(object):
                     raise Exception(response.content)
                 except:
                     exc_info = sys.exc_info()
+                    try:
+                        response.content.seek(0)
+                    except:
+                        pass
             
             logger = logging.getLogger('django.request').warning
             if status in (401, 403, 409):
                 logger = logging.getLogger('django.request').debug
             if status >= 500:
                 logger = logging.getLogger('django.request').error
-                
+            
             logger("%s %s [%s]" % (
                 request.method, request.get_full_path(), response.status_code
             ), exc_info=exc_info, extra={
                     'response': response.content,
                 }
             })
-        
+            try:
+                response.content.seek(0)
+            except:
+                pass
+            
         if isinstance(time_spent, float) and not request.path.startswith('/admin/'):
             # Log as a warning if we took too long.
             if time_spent > getattr(settings, 'WARN_IF_TIME_EXCEEDS', 2) and not (request.get_full_path() in getattr(settings, 'IGNORE_LONG_REQUEST_TIME_URLS', [])):
                         'status':response.status_code,
                     }
                 })
+                try:
+                    response.content.seek(0)
+                except:
+                    pass
+                
         if not request.path.startswith('/sentry/'):
             logging.getLogger('django.request').info(
                 "%s %s %s [%s] \"%s %s %s\" %s %s %s" % (
                     len(response.content) or '-',
                     time_spent,
             ))
+            try:
+                response.content.seek(0)
+            except:
+                pass
+        
+        try:
+            response.content.seek(0)
+        except:
+            pass
+            
         return response
         
     def process_request(self, request):