Matthew Schinckel avatar Matthew Schinckel committed 803ffa6

Handle iterable files in static uploads.

Comments (0)

Files changed (1)

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):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.