def move_avatars_to_be_uid_based(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None: user_profile_model = apps.get_model('zerver', 'UserProfile') if settings.LOCAL_UPLOADS_DIR is not None: for user_profile in user_profile_model.objects.filter( avatar_source="U"): src_file_name = user_avatar_hash(user_profile.email) dst_file_name = user_avatar_path(user_profile) try: move_local_file('avatars', src_file_name + '.original', dst_file_name + '.original') move_local_file('avatars', src_file_name + '-medium.png', dst_file_name + '-medium.png') move_local_file('avatars', src_file_name + '.png', dst_file_name + '.png') except MissingAvatarException: # If the user's avatar is missing, it's probably # because they previously changed their email address. # So set them to have a gravatar instead. user_profile.avatar_source = "G" user_profile.save(update_fields=["avatar_source"]) else: conn = S3Connection(settings.S3_KEY, settings.S3_SECRET_KEY) bucket_name = settings.S3_AVATAR_BUCKET bucket = conn.get_bucket(bucket_name, validate=False) for user_profile in user_profile_model.objects.filter( avatar_source="U"): uid_hash_path = user_avatar_path(user_profile) email_hash_path = user_avatar_hash(user_profile.email) if bucket.get_key(uid_hash_path): continue if not bucket.get_key(email_hash_path): # This is likely caused by a user having previously changed their email # If the user's avatar is missing, it's probably # because they previously changed their email address. # So set them to have a gravatar instead. user_profile.avatar_source = "G" user_profile.save(update_fields=["avatar_source"]) continue bucket.copy_key(uid_hash_path + ".original", bucket_name, email_hash_path + ".original") bucket.copy_key(uid_hash_path + "-medium.png", bucket_name, email_hash_path + "-medium.png") bucket.copy_key(uid_hash_path, bucket_name, email_hash_path) # From an error handling sanity perspective, it's best to # start deleting after everything is copied, so that recovery # from failures is easy (just rerun one loop or the other). for user_profile in user_profile_model.objects.filter( avatar_source="U"): bucket.delete_key( user_avatar_hash(user_profile.email) + ".original") bucket.delete_key( user_avatar_hash(user_profile.email) + "-medium.png") bucket.delete_key(user_avatar_hash(user_profile.email))
def move_avatars_to_be_uid_based(apps, schema_editor): # type: (StateApps, DatabaseSchemaEditor) -> None user_profile_model = apps.get_model('zerver', 'UserProfile') if settings.LOCAL_UPLOADS_DIR is not None: for user_profile in user_profile_model.objects.filter(avatar_source=u"U"): src_file_name = user_avatar_hash(user_profile.email) dst_file_name = user_avatar_path(user_profile) try: move_local_file('avatars', src_file_name + '.original', dst_file_name + '.original') move_local_file('avatars', src_file_name + '-medium.png', dst_file_name + '-medium.png') move_local_file('avatars', src_file_name + '.png', dst_file_name + '.png') except MissingAvatarException: # If the user's avatar is missing, it's probably # because they previously changed their email address. # So set them to have a gravatar instead. user_profile.avatar_source = u"G" user_profile.save(update_fields=["avatar_source"]) else: conn = S3Connection(settings.S3_KEY, settings.S3_SECRET_KEY) bucket_name = settings.S3_AVATAR_BUCKET bucket = conn.get_bucket(bucket_name, validate=False) for user_profile in user_profile_model.objects.filter(avatar_source=u"U"): uid_hash_path = user_avatar_path(user_profile) email_hash_path = user_avatar_hash(user_profile.email) if bucket.get_key(uid_hash_path): continue if not bucket.get_key(email_hash_path): # This is likely caused by a user having previously changed their email # If the user's avatar is missing, it's probably # because they previously changed their email address. # So set them to have a gravatar instead. user_profile.avatar_source = u"G" user_profile.save(update_fields=["avatar_source"]) continue bucket.copy_key(uid_hash_path + ".original", bucket_name, email_hash_path + ".original") bucket.copy_key(uid_hash_path + "-medium.png", bucket_name, email_hash_path + "-medium.png") bucket.copy_key(uid_hash_path, bucket_name, email_hash_path) # From an error handling sanity perspective, it's best to # start deleting after everything is copied, so that recovery # from failures is easy (just rerun one loop or the other). for user_profile in user_profile_model.objects.filter(avatar_source=u"U"): bucket.delete_key(user_avatar_hash(user_profile.email) + ".original") bucket.delete_key(user_avatar_hash(user_profile.email) + "-medium.png") bucket.delete_key(user_avatar_hash(user_profile.email))
def upload_avatar_image(self, user_file, user_profile, email): # type: (File, UserProfile, text_type) -> None content_type = guess_type(user_file.name)[0] bucket_name = settings.S3_AVATAR_BUCKET s3_file_name = user_avatar_hash(email) image_data = user_file.read() upload_image_to_s3( bucket_name, s3_file_name + ".original", content_type, user_profile, image_data, ) # custom 500px wide version resized_medium = resize_avatar(image_data, MEDIUM_AVATAR_SIZE) upload_image_to_s3(bucket_name, s3_file_name + "-medium.png", "image/png", user_profile, resized_medium) resized_data = resize_avatar(image_data) upload_image_to_s3( bucket_name, s3_file_name, 'image/png', user_profile, resized_data, )
def upload_avatar_image(self, user_file, user_profile, email): # type: (File, UserProfile, text_type) -> None content_type = guess_type(user_file.name)[0] bucket_name = settings.S3_AVATAR_BUCKET s3_file_name = user_avatar_hash(email) image_data = user_file.read() upload_image_to_s3( bucket_name, s3_file_name + ".original", content_type, user_profile, image_data, ) # custom 500px wide version resized_medium = resize_avatar(image_data, MEDIUM_AVATAR_SIZE) upload_image_to_s3( bucket_name, s3_file_name + "-medium.png", "image/png", user_profile, resized_medium ) resized_data = resize_avatar(image_data) upload_image_to_s3( bucket_name, s3_file_name, 'image/png', user_profile, resized_data, )
def upload_avatar_image(self, user_file, user_profile, email): # type: (File, UserProfile, text_type) -> None email_hash = user_avatar_hash(email) image_data = user_file.read() write_local_file('avatars', email_hash+'.original', image_data) resized_data = resize_avatar(image_data) write_local_file('avatars', email_hash+'.png', resized_data)
def upload_avatar_image(self, user_file, user_profile, email): # type: (File, UserProfile, text_type) -> None email_hash = user_avatar_hash(email) image_data = user_file.read() write_local_file('avatars', email_hash + '.original', image_data) resized_data = resize_avatar(image_data) write_local_file('avatars', email_hash + '.png', resized_data)
def get_avatar_url(avatar_source, email): # type: (text_type, text_type) -> text_type if avatar_source == u'U': hash_key = user_avatar_hash(email) return upload_backend.get_avatar_url(hash_key) elif settings.ENABLE_GRAVATAR: hash_key = gravatar_hash(email) return u"https://secure.gravatar.com/avatar/%s?d=identicon" % (hash_key,) else: return settings.DEFAULT_AVATAR_URI+'?x=x'
def get_avatar_url(avatar_source, email): # type: (text_type, text_type) -> text_type if avatar_source == u'U': hash_key = user_avatar_hash(email) return upload_backend.get_avatar_url(hash_key) elif settings.ENABLE_GRAVATAR: hash_key = gravatar_hash(email) return u"https://secure.gravatar.com/avatar/%s?d=identicon" % ( hash_key, ) else: return settings.DEFAULT_AVATAR_URI + '?x=x'
def get_avatar_url(avatar_source, email, medium=False): # type: (text_type, text_type, bool) -> text_type if avatar_source == u"U": hash_key = user_avatar_hash(email) return upload_backend.get_avatar_url(hash_key, medium=medium) elif settings.ENABLE_GRAVATAR: gravitar_query_suffix = "&s=%s" % (MEDIUM_AVATAR_SIZE,) if medium else "" hash_key = gravatar_hash(email) return u"https://secure.gravatar.com/avatar/%s?d=identicon%s" % (hash_key, gravitar_query_suffix) else: return settings.DEFAULT_AVATAR_URI + "?x=x"
def _get_unversioned_avatar_url(avatar_source, email, medium=False): # type: (Text, Text, bool) -> Text if avatar_source == u'U': hash_key = user_avatar_hash(email) return upload_backend.get_avatar_url(hash_key, medium=medium) elif settings.ENABLE_GRAVATAR: gravitar_query_suffix = "&s=%s" % (MEDIUM_AVATAR_SIZE,) if medium else "" hash_key = gravatar_hash(email) return u"https://secure.gravatar.com/avatar/%s?d=identicon%s" % (hash_key, gravitar_query_suffix) else: return settings.DEFAULT_AVATAR_URI+'?x=x'
def ensure_medium_avatar_image(self, email): # type: (Text) -> None email_hash = user_avatar_hash(email) output_path = os.path.join(settings.LOCAL_UPLOADS_DIR, "avatars", email_hash + "-medium.png") if os.path.isfile(output_path): return image_path = os.path.join(settings.LOCAL_UPLOADS_DIR, "avatars", email_hash + ".original") image_data = open(image_path, "rb").read() resized_medium = resize_avatar(image_data, MEDIUM_AVATAR_SIZE) write_local_file('avatars', email_hash + '-medium.png', resized_medium)
def ensure_medium_avatar_image(self, email): # type: (text_type) -> None email_hash = user_avatar_hash(email) output_path = os.path.join(settings.LOCAL_UPLOADS_DIR, "avatars", email_hash + "-medium.png") if os.path.isfile(output_path): return image_path = os.path.join(settings.LOCAL_UPLOADS_DIR, "avatars", email_hash + ".original") image_data = open(image_path, "rb").read() resized_medium = resize_avatar(image_data, MEDIUM_AVATAR_SIZE) write_local_file('avatars', email_hash + '-medium.png', resized_medium)
def upload_avatar_image(self, user_file, user_profile, email): # type: (File, UserProfile, Text) -> None email_hash = user_avatar_hash(email) image_data = user_file.read() write_local_file('avatars', email_hash + '.original', image_data) resized_data = resize_avatar(image_data) write_local_file('avatars', email_hash + '.png', resized_data) resized_medium = resize_avatar(image_data, MEDIUM_AVATAR_SIZE) write_local_file('avatars', email_hash + '-medium.png', resized_medium)
def upload_avatar_image(self, user_file, user_profile, email): # type: (File, UserProfile, Text) -> None email_hash = user_avatar_hash(email) image_data = user_file.read() write_local_file('avatars', email_hash+'.original', image_data) resized_data = resize_avatar(image_data) write_local_file('avatars', email_hash+'.png', resized_data) resized_medium = resize_avatar(image_data, MEDIUM_AVATAR_SIZE) write_local_file('avatars', email_hash+'-medium.png', resized_medium)
def ensure_medium_avatar_image(self, email): # type: (text_type) -> None user_profile = get_user_profile_by_email(email) email_hash = user_avatar_hash(email) s3_file_name = email_hash bucket_name = settings.S3_AVATAR_BUCKET conn = S3Connection(settings.S3_KEY, settings.S3_SECRET_KEY) bucket = get_bucket(conn, force_str(bucket_name)) key = bucket.get_key(email_hash) image_data = key.get_contents_as_string() resized_medium = resize_avatar(image_data, MEDIUM_AVATAR_SIZE) upload_image_to_s3(bucket_name, s3_file_name + "-medium.png", "image/png", user_profile, resized_medium)
def ensure_medium_avatar_image(self, email): # type: (text_type) -> None user_profile = get_user_profile_by_email(email) email_hash = user_avatar_hash(email) s3_file_name = email_hash bucket_name = settings.S3_AVATAR_BUCKET conn = S3Connection(settings.S3_KEY, settings.S3_SECRET_KEY) bucket = get_bucket(conn, force_str(bucket_name)) key = bucket.get_key(email_hash) image_data = key.get_contents_as_string() resized_medium = resize_avatar(image_data, MEDIUM_AVATAR_SIZE) upload_image_to_s3( bucket_name, s3_file_name + "-medium.png", "image/png", user_profile, resized_medium )
def upload_avatar_image(self, user_file, user_profile, email): # type: (File, UserProfile, text_type) -> None content_type = guess_type(user_file.name)[0] bucket_name = settings.S3_AVATAR_BUCKET s3_file_name = user_avatar_hash(email) image_data = user_file.read() upload_image_to_s3( bucket_name, s3_file_name + ".original", content_type, user_profile, image_data, ) resized_data = resize_avatar(image_data) upload_image_to_s3( bucket_name, s3_file_name, 'image/png', user_profile, resized_data, )