Пример #1
0
 def get(self, request, path, include_body=True):
     session_dir = getsettings(
         'session_dir', os.path.join(getsettings('BASE_DIR'),
                                     'sessions'))  #default session dir
     user = self.request.session.get('gateone_user')
     if user and 'session' in self.request.session.get('gateone_user'):
         session = user['session']
     else:
         return HttpResponse('User session is not valid')
     filepath = os.path.join(session_dir, session, 'downloads', path)
     abspath = os.path.abspath(filepath)
     if not os.path.exists(abspath):
         return HttpResponse(self.get_error_html(404), status=404)
     if not os.path.isfile(abspath):
         return HttpResponse("%s is not a file" % (path), status=403)
     import stat, mimetypes
     stat_result = os.stat(abspath)
     modified = datetime.datetime.fromtimestamp(stat_result[stat.ST_MTIME])
     response = HttpResponse()
     response["Last-Modified"] = modified
     mime_type, encoding = mimetypes.guess_type(abspath)
     if mime_type:
         response["Content-Type"] = mime_type
     # Set the Cache-Control header to private since this file is not meant
     # to be public.
     response["Cache-Control"] = "private"
     # Add some additional headers
     response['Access-Control-Allow-Origin'] = '*'
     # Check the If-Modified-Since, and don't send the result if the
     # content has not been modified
     ims_value = self.request.META.get('If-Modified-Since', None)
     if ims_value is not None:
         import email.utils
         date_tuple = email.utils.parsedate(ims_value)
         if_since = datetime.datetime.fromtimestamp(time.mktime(date_tuple))
         if if_since >= modified:
             response.status = 304
             return response
     # Finally, deliver the file
     with io.open(abspath, "rb") as file:
         data = file.read()
         hasher = hashlib.sha1()
         hasher.update(data)
         response["Etag"] = '"%s"' % hasher.hexdigest()
         if include_body:
             response.content = data
             response.status = 200
             return response
         else:
             assert self.request.method in ("HEAD", "head")
             response["Content-Length"] = len(data)
Пример #2
0
	def create_response(self, vdict=dict(), valid_form=True):
		response = HttpResponse(json.dumps(vdict),
		 content_type='application/json')
		response.status = 200 if valid_form else 500
		return response