def get(self,site_key): # Get the file by the provided key name user_data = UserData.get_by_key_name(site_key) if not user_data: return self.error(404) blob_info = user_data.blob_info # Figure out hostname so we can give 'full links' host_name = '/'.join([k for k in self.request.url.split('/')[:3]]) # Build the response resp = { 'name':blob_info.filename, 'creation':blob_info.creation.strftime("%Y-%m-%d %H:%M:%S"), 'download_url':'%s/%s' % (host_name,site_key), 'url':'%s/%s' % (host_name,site_key), 'type':blob_info.content_type, 'size':blob_info.size, 'size_in_kb':(blob_info.size)/1024, 'info_url':'%s/i/%s' % (host_name,site_key), 'thumbnail_url': get_serving_url(blob_info) if blob_info.content_type in IMAGE_MIMETYPES else '' } if self.request.get('format') == 'json': # If the requested format is json render to json return self.response.out.write(json.dumps(resp)) else: # Otherwise render to html self.response.out.write(template.render('templates/viewer.html', resp))
def basic_auth_required(self, include_viewers=False): # scheme = os.environ.get('wsgi.url_scheme') # if scheme != 'https': # # Basic auth should be done via https only # return False auth = os.environ.get("HTTP_AUTHORIZATION", None) auth_msg = 'Basic realm="%s on AppEngine"' % self.reponame if not auth: # lacking auth info self.response.headers["WWW-Authenticate"] = auth_msg return False (basic, _, auth) = auth.partition(" ") if basic != "Basic": # Mechanism is not Basic. self.response.headers["WWW-Authenticate"] = auth_msg return False (username, _, password) = base64.b64decode(auth).partition(":") if username.find("@") < 0: username += "@gmail.com" userdata = UserData.get_by_key_name("user_" + username) if not userdata or str(userdata.passphrase) != str(password): # passphrase mismatch. self.response.headers["WWW-Authenticate"] = auth_msg return False if not self.repo.check_user_perm(userdata.key(), include_viewers): # Userdata mismatch. self.response.headers["WWW-Authenticate"] = auth_msg return False # Otherwise, valid user. logging.info("%s pushed into %s." % (username, self.reponame)) return True
def get(self,site_key): # Get the file by the provided key name user_data = UserData.get_by_key_name(site_key) if not user_data: return self.error(404) if user_data.blob_info.content_type in SEND_AS_RAW_MIMETYPES: # If this is a picture or otherwise something the browser can just use, send it self.send_blob(user_data.blob_info) else: # Otherwise prompt the browser to Save As self.send_blob(user_data.blob_info,save_as=True)
def get_current_user(): u = users.get_current_user() if not u: return None return UserData.get_by_key_name("user_" + u.email())