support client-side caching through 'Last-Modified' response headers
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import logging
|
||||
import time
|
||||
|
||||
from django.http import HttpResponse, Http404
|
||||
from django.http import HttpResponse, Http404, HttpResponseNotModified
|
||||
|
||||
from xmodule.contentstore.django import contentstore
|
||||
from xmodule.contentstore import StaticContent
|
||||
@@ -30,7 +31,22 @@ class StaticContentServer(object):
|
||||
else:
|
||||
logging.debug('cache hit on {0}'.format(content.filename))
|
||||
|
||||
# see if the last-modified at hasn't changed, if not return a 302 (Not Modified)
|
||||
|
||||
logging.debug(request.META)
|
||||
|
||||
# convert over the DB persistent last modified timestamp to a HTTP compatible
|
||||
# timestamp
|
||||
last_modified_at_str = content.last_modified_at.strftime("%a, %d-%b-%Y %H:%M:%S GMT")
|
||||
|
||||
# see if the client has cached this content, if so then compare the
|
||||
# timestamps, if they are the same then just return a 304 (Not Modified)
|
||||
if 'HTTP_IF_MODIFIED_SINCE' in request.META:
|
||||
if_modified_since = request.META['HTTP_IF_MODIFIED_SINCE']
|
||||
if if_modified_since == last_modified_at_str:
|
||||
return HttpResponseNotModified()
|
||||
|
||||
response = HttpResponse(content.data, content_type=content.content_type)
|
||||
response['Content-Disposition'] = 'attachment; filename={0}'.format(content.name)
|
||||
|
||||
response['Last-Modified'] = last_modified_at_str
|
||||
|
||||
return response
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
class StaticContent(object):
|
||||
def __init__(self, filename, name, content_type, data):
|
||||
def __init__(self, filename, name, content_type, data, last_modified_at=None):
|
||||
self.filename = filename
|
||||
self.name = name
|
||||
self.content_type = content_type
|
||||
self.data = data
|
||||
self.last_modified_at = last_modified_at
|
||||
|
||||
@staticmethod
|
||||
def get_location_tag():
|
||||
|
||||
@@ -23,8 +23,7 @@ class MongoContentStore(object):
|
||||
def find(self, filename):
|
||||
try:
|
||||
with self.fs.get_last_version(filename) as fp:
|
||||
logging.debug('fetched {0}'.format(fp.name))
|
||||
return StaticContent(fp.filename, fp.displayname, fp.content_type, fp.read())
|
||||
return StaticContent(fp.filename, fp.displayname, fp.content_type, fp.read(), fp.uploadDate)
|
||||
except NoFile:
|
||||
raise NotFoundError()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user