def get(self, share_key): if not share_key: return self.redirect("/") sharedfile = Sharedfile.get_by_share_key(share_key) if not sharedfile: raise tornado.web.HTTPError(404) current_user = self.get_current_user_object() if not current_user: self.set_header("Cache-Control", "s-maxage=600, max-age=60") sourcefile = sharedfile.sourcefile() sharedfile_owner = sharedfile.user() owner_twitter_account = Externalservice.by_user(sharedfile_owner, Externalservice.TWITTER) comments = sharedfile.comments() view_count = sharedfile.livish_view_count() save_count = sharedfile.save_count heart_count = sharedfile.like_count can_delete = sharedfile.can_delete(current_user) can_comment = current_user and current_user.email_confirmed == 1 and not options.readonly user_is_owner = current_user and current_user.id == sharedfile_owner.id in_these_shakes = sharedfile.shakes() if current_user: user_shakes = current_user.shakes(include_managed=True) else: user_shakes = [] add_to_shakes = [] for user_shake in user_shakes: found = False for in_shake in in_these_shakes: if in_shake.id == user_shake.id: found = True if found == False: add_to_shakes.append(user_shake) can_add_to_shakes = (can_delete and len(add_to_shakes) > 0) if owner_twitter_account: owner_twitter_account = owner_twitter_account.screen_name else: owner_twitter_account = 'mltshphq' image_url = "/r/%s" % (sharedfile.share_key) if options.debug: file_path = "originals/%s" % (sourcefile.file_key) image_url = s3_authenticated_url(options.aws_key, options.aws_secret, options.aws_bucket, file_path=file_path, seconds=3600) thumb_url = s3_authenticated_url(options.aws_key, options.aws_secret, options.aws_bucket, file_path="thumbnails/%s" % (sourcefile.thumb_key), seconds=3600) jsonp = 'jsonp%s' % int(time.mktime(sharedfile.created_at.timetuple())) return self.render("image/show.html", sharedfile=sharedfile, thumb_url=thumb_url, sharedfile_owner=sharedfile_owner, image_url=image_url, jsonp=jsonp, view_count=view_count, can_delete=can_delete, save_count=save_count, heart_count=heart_count, comments=comments, current_user_obj=current_user, sourcefile=sourcefile, in_these_shakes=in_these_shakes, user_shakes=user_shakes, add_to_shakes=add_to_shakes, can_add_to_shakes=can_add_to_shakes, can_comment=can_comment, owner_twitter_account=owner_twitter_account, user_is_owner=user_is_owner)
def get(self, share_key): if not share_key: raise tornado.web.HTTPError(404) sharedfile = Sharedfile.get_by_share_key(share_key) if not sharedfile: raise tornado.web.HTTPError(404) # check if we have logged in user. user = self.get_current_user() if user: user_id = user['id'] else: user_id = None # count view sharedfile.add_view(user_id) # determine if we are to serve via CDN or direct from S3: if self.request.host == ("s.%s" % options.app_host) and options.use_cdn: # s = static; serve through CDN for "s.mltshp.com" requests # Amazon's ELB will set this header to inform us what scheme is in use # Fallback to checking Tornado's protocol if it is absent using_https = self.request.headers.get( "X-Forwarded-Proto", self.request.protocol) == "https" # construct a URL to the CDN-hosted image # http://cdn.mltshp.com/r/share_key if using_https: cdn_url = "https://%s" % options.cdn_ssl_host else: cdn_url = "http://%s" % options.cdn_host cdn_url += "/r/%s" % share_key self.redirect(cdn_url) else: # piece together headers to be picked up by nginx to proxy file from S3 sourcefile = sharedfile.sourcefile() file_path = "originals/%s" % (sourcefile.file_key) authenticated_url = s3_authenticated_url(options.aws_key, options.aws_secret, options.aws_bucket, file_path=file_path, seconds=3600) (uri, query) = authenticated_url.split('?') self.set_header("Content-Type", sharedfile.content_type) self.set_header("Surrogate-Control", "max-age=86400") self.set_header("X-Accel-Redirect", "/s3/%s?%s" % (sourcefile.file_key, query)) return
def sharedimages(self): """ This is a bit of a hack, but I wanted it in the model so I could fix it up later. This simply pulls all the shared images and adds one new field, which is a signed url to the amazon s3 thumbnail rather than through the server. Does not include deleted images. """ images = self.connection.query("SELECT sf.id, sf.title, sf.name, sf.share_key, src.file_key, \ src.thumb_key FROM sharedfile as sf, sourcefile as src \ WHERE src.id = sf.source_id AND sf.user_id = %s and sf.deleted = 0 ORDER BY sf.created_at DESC limit 3", self.id) for image in images: file_path = "thumbnails/%s" % (image['thumb_key']) authenticated_url = s3_authenticated_url(options.aws_key, options.aws_secret, \ bucket_name=options.aws_bucket, file_path=file_path) image['thumbnail_url'] = authenticated_url return images
def get(self, share_key, format=""): if not share_key: raise tornado.web.HTTPError(404) self._sharedfile = Sharedfile.get_by_share_key(share_key) if not self._sharedfile: raise tornado.web.HTTPError(404) # determine if we are to serve via CDN or direct from S3: if self.request.host == ("s.%s" % options.app_host) and options.use_cdn: # s = static; serve through CDN for "s.mltshp.com" requests # If we're using mltshp-cdn.com, we know that we can use # https; if something else is configured, check the # X-Forwarded-Proto header and fallback to the protocol # of the request using_https = options.cdn_ssl_host == "mltshp-cdn.com" or \ self.request.headers.get("X-Forwarded-Proto", self.request.protocol) == "https" # construct a URL to the CDN-hosted image # https://mltshp-cdn.com/r/share_key if using_https: cdn_url = "https://%s" % options.cdn_ssl_host else: cdn_url = "http://%s" % options.cdn_host cdn_url += "/r/%s" % share_key if format != "": cdn_url += ".%s" % format self.redirect(cdn_url) else: # piece together headers to be picked up by nginx to proxy file from S3 sourcefile = self._sharedfile.sourcefile() content_type = self._sharedfile.content_type # create a task to transcode a sourcefile that has not been processed yet # this will allow us to lazily transcode GIFs that have yet to be # processed if content_type == "image/gif" and options.use_workers: if sourcefile.webm_flag is None or sourcefile.mp4_flag is None: transcode_sharedfile.delay_or_run(self._sharedfile.id) if format == "webm": if sourcefile.webm_flag != 1: raise tornado.web.HTTPError(404) file_path = "webm/%s" % sourcefile.file_key content_type = "video/webm" elif format == "mp4": if sourcefile.mp4_flag != 1: raise tornado.web.HTTPError(404) file_path = "mp4/%s" % sourcefile.file_key content_type = "video/mp4" else: file_path = "originals/%s" % sourcefile.file_key authenticated_url = s3_authenticated_url(options.aws_key, options.aws_secret, options.aws_bucket, file_path=file_path, seconds=3600) (uri, query) = authenticated_url.split('?') self.set_header("Content-Type", content_type) self.set_header("Surrogate-Control", "max-age=86400") self.set_header("X-Accel-Redirect", "/s3/%s?%s" % (file_path, query))
def small_thumbnail_url(self): """ """ return s3_authenticated_url(options.aws_key, options.aws_secret, options.aws_bucket, \ file_path="smalls/%s" % (self.sourcefile().small_key), seconds=3600)
def thumbnail_url(self): return s3_authenticated_url(options.aws_key, options.aws_secret, options.aws_bucket, \ file_path="thumbnails/%s" % (self.sourcefile().thumb_key), seconds=3600)