def handle_redirect(self, entity, mbid, filename): """ Handle the 307 redirect. """ if not filename: return [statuscode (400), "no filename specified"] filename = filename.replace("-250.jpg", "_thumb250.jpg") filename = filename.replace("-500.jpg", "_thumb500.jpg") return [statuscode (307), "%s/mbid-%s/mbid-%s-%s" % ( self.config.s3.prefix, mbid, mbid, filename)]
def handle_index(self): '''Serve up the one static index page''' try: f = open(os.path.join(self.config.static_path, "index")) except IOError: return [statuscode (500), "Internal Server Error"] txt = f.read() f.close() return [statuscode (200), txt]
def handle_redirect(self, request, mbid, filename): """Handle the 307 redirect.""" if not filename: return [statuscode(400), "no filename specified"] filename = re.sub("-250.(jpg|gif|png|pdf)", "_thumb250.jpg", filename) filename = re.sub("-500.(jpg|gif|png|pdf)", "_thumb500.jpg", filename) url = "%s/mbid-%s/mbid-%s-%s" % (self.config.s3.prefix, mbid, mbid, filename) return request.redirect(code=307, location=url)
def __call__(self, environ, start_response): try: with closing(self.engine.connect()) as conn: (status, txt) = CoverArtRedirect(self.config, conn).handle(environ) if status.startswith("307"): start_response(status, [ ('Location', txt), ('Access-Control-Allow-Origin', '*') ]) return ["See: ", txt, "\n"] elif status.startswith("200"): start_response(statuscode (200), [ ('Content-Type', 'text/html; charset=UTF-8'), ('Content-Length', str(len(txt)))]) return [txt] else: start_response(status, []) return [txt, "\n"] except: cherrypy.log("Caught exception\n" + traceback.format_exc()) start_response(statuscode (500), []) return ["Whoops. Our bad.\n"]
def handle(self, environ): '''Handle a request, parse and validate arguments and dispatch the request''' entity = shift_path_info(environ) if not entity: return self.handle_index() if entity not in ('release', 'release-group'): return [statuscode (400), "Only release and release-group entities are currently supported"] req_mbid = shift_path_info(environ) if not req_mbid: return [statuscode (400), "no MBID specified."] if not re.match('[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$', req_mbid): return [statuscode (400), "invalid MBID specified."] mbid = self.resolve_mbid (entity, req_mbid) if not mbid: return [statuscode (404), "No %s found with identifier %s" % (entity, req_mbid)] filename = shift_path_info(environ) if not filename: if entity == 'release': return self.handle_dir(entity, mbid, environ) else: return [statuscode (400), "Invalid request."] if filename.startswith ('front'): (filename, mbid) = self.resolve_cover (entity, mbid, 'Front', self.thumbnail (filename)) if not filename: return [statuscode (404), "No front cover image found for %s with identifier %s" % (entity, req_mbid)] elif filename.startswith ('back'): if entity == 'release': (filename, mbid) = self.resolve_cover (entity, mbid, 'Back', self.thumbnail (filename)) if not filename: return [statuscode (404), "No back cover image found for %s with identifier %s" % (entity, req_mbid)] else: return [statuscode (400), "Back cover is not defined for %s" % entity] (code, response) = self.handle_redirect(entity, mbid, filename.encode('utf8')) return code, response
def handle_dir(self, entity, mbid, environ): '''When the user requests no file, redirect to the root of the bucket to give the user an index of what is in the bucket''' index_url = "%s/mbid-%s/index.json" % (self.config.s3.prefix, mbid) return [statuscode (307), index_url]