def main(): keys = sys.argv[2:] options.use_workers = False if len(keys) == 0: print "Selecting untranscoded sharedfiles..." select = """SELECT share_key FROM sharedfile JOIN sourcefile ON sourcefile.id = sharedfile.source_id WHERE sharedfile.deleted = 0 AND sharedfile.content_type = 'image/gif' AND sharedfile.parent_id = 0 AND (sourcefile.webm_flag IS NULL OR sourcefile.mp4_flag IS NULL) ORDER BY sharedfile.created_at DESC""" results = Sharedfile.query(select) for result in results: keys.append(result["share_key"]) print "Found %d sharedfiles to transcode" % len(keys) for key in keys: sf = Sharedfile.get( "share_key=%s AND content_type='image/gif' AND deleted=0", key) if sf is not None: print "Transcoding %s..." % sf.share_key transcode_sharedfile.delay_or_run(sf.id) else: print "Could not find sharedfile with key: %s" % key
def create_from_file(file_path, file_name, sha1_value, content_type, user_id, title=None, shake_id=None, skip_s3=None): """ TODO: Must only accept acceptable content-types after consulting a list. """ if len(sha1_value) <> 40: return None if user_id == None: return None if content_type not in [ 'image/gif', 'image/jpeg', 'image/jpg', 'image/png' ]: return None # If we have no shake_id, drop in user's main shake. Otherwise, validate that the specififed # shake is a group shake that the user has permissions for. if not shake_id: destination_shake = shake.Shake.get( 'user_id = %s and type=%s and deleted=0', user_id, 'user') else: destination_shake = shake.Shake.get('id=%s and deleted=0', shake_id) if not destination_shake: return None if not destination_shake.can_update(user_id): return None sf = sourcefile.Sourcefile.get_from_file(file_path, sha1_value, skip_s3=skip_s3) if sf: shared_file = Sharedfile(user_id=user_id, name=file_name, content_type=content_type, source_id=sf.id, title=title, size=path.getsize(file_path)) shared_file.save() if shared_file.saved(): shared_file.share_key = base36encode(shared_file.id) shared_file.save() shared_file.add_to_shake(destination_shake) if options.use_workers and content_type == "image/gif": transcode_sharedfile.delay_or_run(shared_file.id) return shared_file else: return None else: return None
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))