python: Reformat with Black, except quotes.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg
2021-02-11 23:19:30 -08:00
committed by Tim Abbott
parent 5028c081cb
commit 11741543da
817 changed files with 44952 additions and 24860 deletions

View File

@@ -74,9 +74,11 @@ INLINE_MIME_TYPES = [
# "file name" is the original filename provided by the user run
# through a sanitization function.
class RealmUploadQuotaError(JsonableError):
code = ErrorCode.REALM_UPLOAD_QUOTA
def sanitize_name(value: str) -> str:
"""
Sanitizes a value to be safe to store in a Linux filesystem, in
@@ -94,9 +96,11 @@ def sanitize_name(value: str) -> str:
assert value not in {'', '.', '..'}
return mark_safe(value)
class BadImageError(JsonableError):
code = ErrorCode.BAD_IMAGE
name_to_tag_num = {name: num for num, name in ExifTags.TAGS.items()}
# https://stackoverflow.com/a/6218425
@@ -119,7 +123,8 @@ def exif_rotate(image: Image) -> Image:
return image
def resize_avatar(image_data: bytes, size: int=DEFAULT_AVATAR_SIZE) -> bytes:
def resize_avatar(image_data: bytes, size: int = DEFAULT_AVATAR_SIZE) -> bytes:
try:
im = Image.open(io.BytesIO(image_data))
im = exif_rotate(im)
@@ -134,11 +139,12 @@ def resize_avatar(image_data: bytes, size: int=DEFAULT_AVATAR_SIZE) -> bytes:
im.save(out, format='png')
return out.getvalue()
def resize_logo(image_data: bytes) -> bytes:
try:
im = Image.open(io.BytesIO(image_data))
im = exif_rotate(im)
im.thumbnail((8*DEFAULT_AVATAR_SIZE, DEFAULT_AVATAR_SIZE), Image.ANTIALIAS)
im.thumbnail((8 * DEFAULT_AVATAR_SIZE, DEFAULT_AVATAR_SIZE), Image.ANTIALIAS)
except OSError:
raise BadImageError(_("Could not decode image; did you upload an image file?"))
except DecompressionBombError:
@@ -150,7 +156,7 @@ def resize_logo(image_data: bytes) -> bytes:
return out.getvalue()
def resize_gif(im: GifImageFile, size: int=DEFAULT_EMOJI_SIZE) -> bytes:
def resize_gif(im: GifImageFile, size: int = DEFAULT_EMOJI_SIZE) -> bytes:
frames = []
duration_info = []
disposals = []
@@ -165,15 +171,20 @@ def resize_gif(im: GifImageFile, size: int=DEFAULT_EMOJI_SIZE) -> bytes:
duration_info.append(im.info['duration'])
disposals.append(im.disposal_method)
out = io.BytesIO()
frames[0].save(out, save_all=True, optimize=False,
format="GIF", append_images=frames[1:],
duration=duration_info,
disposal=disposals,
loop=loop)
frames[0].save(
out,
save_all=True,
optimize=False,
format="GIF",
append_images=frames[1:],
duration=duration_info,
disposal=disposals,
loop=loop,
)
return out.getvalue()
def resize_emoji(image_data: bytes, size: int=DEFAULT_EMOJI_SIZE) -> bytes:
def resize_emoji(image_data: bytes, size: int = DEFAULT_EMOJI_SIZE) -> bytes:
try:
im = Image.open(io.BytesIO(image_data))
image_format = im.format
@@ -183,9 +194,9 @@ def resize_emoji(image_data: bytes, size: int=DEFAULT_EMOJI_SIZE) -> bytes:
# only resize under certain conditions to minimize the chance of
# creating ugly gifs.
should_resize = (
im.size[0] != im.size[1] # not square
or im.size[0] > MAX_EMOJI_GIF_SIZE # dimensions too large
or len(image_data) > MAX_EMOJI_GIF_FILE_SIZE_BYTES # filesize too large
im.size[0] != im.size[1] # not square
or im.size[0] > MAX_EMOJI_GIF_SIZE # dimensions too large
or len(image_data) > MAX_EMOJI_GIF_FILE_SIZE_BYTES # filesize too large
)
return resize_gif(im, size) if should_resize else image_data
else:
@@ -202,17 +213,26 @@ def resize_emoji(image_data: bytes, size: int=DEFAULT_EMOJI_SIZE) -> bytes:
### Common
class ZulipUploadBackend:
def upload_message_file(self, uploaded_file_name: str, uploaded_file_size: int,
content_type: Optional[str], file_data: bytes,
user_profile: UserProfile,
target_realm: Optional[Realm]=None) -> str:
def upload_message_file(
self,
uploaded_file_name: str,
uploaded_file_size: int,
content_type: Optional[str],
file_data: bytes,
user_profile: UserProfile,
target_realm: Optional[Realm] = None,
) -> str:
raise NotImplementedError()
def upload_avatar_image(self, user_file: File,
acting_user_profile: UserProfile,
target_user_profile: UserProfile,
content_type: Optional[str]=None) -> None:
def upload_avatar_image(
self,
user_file: File,
acting_user_profile: UserProfile,
target_user_profile: UserProfile,
content_type: Optional[str] = None,
) -> None:
raise NotImplementedError()
def delete_avatar_image(self, user: UserProfile) -> None:
@@ -221,7 +241,7 @@ class ZulipUploadBackend:
def delete_message_image(self, path_id: str) -> bool:
raise NotImplementedError()
def get_avatar_url(self, hash_key: str, medium: bool=False) -> str:
def get_avatar_url(self, hash_key: str, medium: bool = False) -> str:
raise NotImplementedError()
def copy_avatar(self, source_profile: UserProfile, target_profile: UserProfile) -> None:
@@ -239,21 +259,28 @@ class ZulipUploadBackend:
def get_realm_icon_url(self, realm_id: int, version: int) -> str:
raise NotImplementedError()
def upload_realm_logo_image(self, logo_file: File, user_profile: UserProfile,
night: bool) -> None:
def upload_realm_logo_image(
self, logo_file: File, user_profile: UserProfile, night: bool
) -> None:
raise NotImplementedError()
def get_realm_logo_url(self, realm_id: int, version: int, night: bool) -> str:
raise NotImplementedError()
def upload_emoji_image(self, emoji_file: File, emoji_file_name: str, user_profile: UserProfile) -> None:
def upload_emoji_image(
self, emoji_file: File, emoji_file_name: str, user_profile: UserProfile
) -> None:
raise NotImplementedError()
def get_emoji_url(self, emoji_file_name: str, realm_id: int) -> str:
raise NotImplementedError()
def upload_export_tarball(self, realm: Realm, tarball_path: str,
percent_callback: Optional[Callable[[Any], None]]=None) -> str:
def upload_export_tarball(
self,
realm: Realm,
tarball_path: str,
percent_callback: Optional[Callable[[Any], None]] = None,
) -> str:
raise NotImplementedError()
def delete_export_tarball(self, export_path: str) -> Optional[str]:
@@ -265,24 +292,29 @@ class ZulipUploadBackend:
def realm_avatar_and_logo_path(self, realm: Realm) -> str:
raise NotImplementedError()
### S3
def get_bucket(bucket_name: str, session: Optional[Session]=None) -> ServiceResource:
def get_bucket(bucket_name: str, session: Optional[Session] = None) -> ServiceResource:
# See https://github.com/python/typeshed/issues/2706
# for why this return type is a `ServiceResource`.
if session is None:
session = boto3.Session(settings.S3_KEY, settings.S3_SECRET_KEY)
bucket = session.resource('s3', region_name=settings.S3_REGION,
endpoint_url=settings.S3_ENDPOINT_URL).Bucket(bucket_name)
bucket = session.resource(
's3', region_name=settings.S3_REGION, endpoint_url=settings.S3_ENDPOINT_URL
).Bucket(bucket_name)
return bucket
def upload_image_to_s3(
# See https://github.com/python/typeshed/issues/2706
bucket: ServiceResource,
file_name: str,
content_type: Optional[str],
user_profile: UserProfile,
contents: bytes) -> None:
# See https://github.com/python/typeshed/issues/2706
bucket: ServiceResource,
file_name: str,
content_type: Optional[str],
user_profile: UserProfile,
contents: bytes,
) -> None:
key = bucket.Object(file_name)
metadata = {
"user_profile_id": str(user_profile.id),
@@ -295,8 +327,13 @@ def upload_image_to_s3(
if content_type not in INLINE_MIME_TYPES:
content_disposition = "attachment"
key.put(Body=contents, Metadata=metadata, ContentType=content_type,
ContentDisposition=content_disposition)
key.put(
Body=contents,
Metadata=metadata,
ContentType=content_type,
ContentDisposition=content_disposition,
)
def check_upload_within_quota(realm: Realm, uploaded_file_size: int) -> None:
upload_quota = realm.upload_quota_bytes()
@@ -306,6 +343,7 @@ def check_upload_within_quota(realm: Realm, uploaded_file_size: int) -> None:
if (used_space + uploaded_file_size) > upload_quota:
raise RealmUploadQuotaError(_("Upload would exceed your organization's upload quota."))
def get_file_info(request: HttpRequest, user_file: File) -> Tuple[str, int, Optional[str]]:
uploaded_file_name = user_file.name
@@ -326,16 +364,20 @@ def get_file_info(request: HttpRequest, user_file: File) -> Tuple[str, int, Opti
def get_signed_upload_url(path: str) -> str:
client = boto3.client('s3', aws_access_key_id=settings.S3_KEY,
aws_secret_access_key=settings.S3_SECRET_KEY,
region_name=settings.S3_REGION,
endpoint_url=settings.S3_ENDPOINT_URL)
return client.generate_presigned_url(ClientMethod='get_object',
Params={
'Bucket': settings.S3_AUTH_UPLOADS_BUCKET,
'Key': path},
ExpiresIn=SIGNED_UPLOAD_URL_DURATION,
HttpMethod='GET')
client = boto3.client(
's3',
aws_access_key_id=settings.S3_KEY,
aws_secret_access_key=settings.S3_SECRET_KEY,
region_name=settings.S3_REGION,
endpoint_url=settings.S3_ENDPOINT_URL,
)
return client.generate_presigned_url(
ClientMethod='get_object',
Params={'Bucket': settings.S3_AUTH_UPLOADS_BUCKET, 'Key': path},
ExpiresIn=SIGNED_UPLOAD_URL_DURATION,
HttpMethod='GET',
)
class S3UploadBackend(ZulipUploadBackend):
def __init__(self) -> None:
@@ -343,7 +385,8 @@ class S3UploadBackend(ZulipUploadBackend):
self.avatar_bucket = get_bucket(settings.S3_AVATAR_BUCKET, self.session)
network_location = urllib.parse.urlparse(
self.avatar_bucket.meta.client.meta.endpoint_url).netloc
self.avatar_bucket.meta.client.meta.endpoint_url
).netloc
self.avatar_bucket_url = f"https://{self.avatar_bucket.name}.{network_location}"
self.uploads_bucket = get_bucket(settings.S3_AUTH_UPLOADS_BUCKET, self.session)
@@ -355,21 +398,31 @@ class S3UploadBackend(ZulipUploadBackend):
key.load()
except botocore.exceptions.ClientError:
file_name = path_id.split("/")[-1]
logging.warning("%s does not exist. Its entry in the database will be removed.", file_name)
logging.warning(
"%s does not exist. Its entry in the database will be removed.", file_name
)
return False
key.delete()
return True
def upload_message_file(self, uploaded_file_name: str, uploaded_file_size: int,
content_type: Optional[str], file_data: bytes,
user_profile: UserProfile, target_realm: Optional[Realm]=None) -> str:
def upload_message_file(
self,
uploaded_file_name: str,
uploaded_file_size: int,
content_type: Optional[str],
file_data: bytes,
user_profile: UserProfile,
target_realm: Optional[Realm] = None,
) -> str:
if target_realm is None:
target_realm = user_profile.realm
s3_file_name = "/".join([
str(target_realm.id),
secrets.token_urlsafe(18),
sanitize_name(uploaded_file_name),
])
s3_file_name = "/".join(
[
str(target_realm.id),
secrets.token_urlsafe(18),
sanitize_name(uploaded_file_name),
]
)
url = f"/user_uploads/{s3_file_name}"
upload_image_to_s3(
@@ -386,8 +439,13 @@ class S3UploadBackend(ZulipUploadBackend):
def delete_message_image(self, path_id: str) -> bool:
return self.delete_file_from_s3(path_id, self.uploads_bucket)
def write_avatar_images(self, s3_file_name: str, target_user_profile: UserProfile,
image_data: bytes, content_type: Optional[str]) -> None:
def write_avatar_images(
self,
s3_file_name: str,
target_user_profile: UserProfile,
image_data: bytes,
content_type: Optional[str],
) -> None:
upload_image_to_s3(
self.avatar_bucket,
s3_file_name + ".original",
@@ -417,17 +475,19 @@ class S3UploadBackend(ZulipUploadBackend):
# See avatar_url in avatar.py for URL. (That code also handles the case
# that users use gravatar.)
def upload_avatar_image(self, user_file: File,
acting_user_profile: UserProfile,
target_user_profile: UserProfile,
content_type: Optional[str] = None) -> None:
def upload_avatar_image(
self,
user_file: File,
acting_user_profile: UserProfile,
target_user_profile: UserProfile,
content_type: Optional[str] = None,
) -> None:
if content_type is None:
content_type = guess_type(user_file.name)[0]
s3_file_name = user_avatar_path(target_user_profile)
image_data = user_file.read()
self.write_avatar_images(s3_file_name, target_user_profile,
image_data, content_type)
self.write_avatar_images(s3_file_name, target_user_profile, image_data, content_type)
def delete_avatar_image(self, user: UserProfile) -> None:
path_id = user_avatar_path(user)
@@ -452,7 +512,7 @@ class S3UploadBackend(ZulipUploadBackend):
self.write_avatar_images(s3_target_file_name, target_profile, image_data, content_type)
def get_avatar_url(self, hash_key: str, medium: bool=False) -> str:
def get_avatar_url(self, hash_key: str, medium: bool = False) -> str:
medium_suffix = "-medium.png" if medium else ""
# ?x=x allows templates to append additional parameters with &s
return f"{self.avatar_bucket_url}/{hash_key}{medium_suffix}?x=x"
@@ -492,8 +552,9 @@ class S3UploadBackend(ZulipUploadBackend):
# ?x=x allows templates to append additional parameters with &s
return f"{self.avatar_bucket_url}/{realm_id}/realm/icon.png?version={version}"
def upload_realm_logo_image(self, logo_file: File, user_profile: UserProfile,
night: bool) -> None:
def upload_realm_logo_image(
self, logo_file: File, user_profile: UserProfile, night: bool
) -> None:
content_type = guess_type(logo_file.name)[0]
if night:
basename = 'night_logo'
@@ -563,8 +624,9 @@ class S3UploadBackend(ZulipUploadBackend):
resized_avatar,
)
def upload_emoji_image(self, emoji_file: File, emoji_file_name: str,
user_profile: UserProfile) -> None:
def upload_emoji_image(
self, emoji_file: File, emoji_file_name: str, user_profile: UserProfile
) -> None:
content_type = guess_type(emoji_file.name)[0]
emoji_path = RealmEmoji.PATH_ID_TEMPLATE.format(
realm_id=user_profile.realm_id,
@@ -589,24 +651,33 @@ class S3UploadBackend(ZulipUploadBackend):
)
def get_emoji_url(self, emoji_file_name: str, realm_id: int) -> str:
emoji_path = RealmEmoji.PATH_ID_TEMPLATE.format(realm_id=realm_id,
emoji_file_name=emoji_file_name)
emoji_path = RealmEmoji.PATH_ID_TEMPLATE.format(
realm_id=realm_id, emoji_file_name=emoji_file_name
)
return f"{self.avatar_bucket_url}/{emoji_path}"
def upload_export_tarball(self, realm: Optional[Realm], tarball_path: str,
percent_callback: Optional[Callable[[Any], None]]=None) -> str:
def upload_export_tarball(
self,
realm: Optional[Realm],
tarball_path: str,
percent_callback: Optional[Callable[[Any], None]] = None,
) -> str:
# We use the avatar bucket, because it's world-readable.
key = self.avatar_bucket.Object(os.path.join("exports", secrets.token_hex(16),
os.path.basename(tarball_path)))
key = self.avatar_bucket.Object(
os.path.join("exports", secrets.token_hex(16), os.path.basename(tarball_path))
)
key.upload_file(tarball_path, Callback=percent_callback)
session = botocore.session.get_session()
config = Config(signature_version=botocore.UNSIGNED)
public_url = session.create_client('s3', region_name=settings.S3_REGION,
endpoint_url=settings.S3_ENDPOINT_URL,
config=config).generate_presigned_url(
public_url = session.create_client(
's3',
region_name=settings.S3_REGION,
endpoint_url=settings.S3_ENDPOINT_URL,
config=config,
).generate_presigned_url(
'get_object',
Params={
'Bucket': self.avatar_bucket.name,
@@ -623,8 +694,10 @@ class S3UploadBackend(ZulipUploadBackend):
return export_path
return None
### Local
def write_local_file(type: str, path: str, file_data: bytes) -> None:
file_path = os.path.join(settings.LOCAL_UPLOADS_DIR, type, path)
@@ -632,11 +705,13 @@ def write_local_file(type: str, path: str, file_data: bytes) -> None:
with open(file_path, 'wb') as f:
f.write(file_data)
def read_local_file(type: str, path: str) -> bytes:
file_path = os.path.join(settings.LOCAL_UPLOADS_DIR, type, path)
with open(file_path, 'rb') as f:
return f.read()
def delete_local_file(type: str, path: str) -> bool:
file_path = os.path.join(settings.LOCAL_UPLOADS_DIR, type, path)
if os.path.isfile(file_path):
@@ -647,6 +722,7 @@ def delete_local_file(type: str, path: str) -> bool:
logging.warning("%s does not exist. Its entry in the database will be removed.", file_name)
return False
def get_local_file_path(path_id: str) -> Optional[str]:
local_path = os.path.join(settings.LOCAL_UPLOADS_DIR, 'files', path_id)
if os.path.isfile(local_path):
@@ -654,8 +730,10 @@ def get_local_file_path(path_id: str) -> Optional[str]:
else:
return None
LOCAL_FILE_ACCESS_TOKEN_SALT = "local_file_"
def generate_unauthed_file_access_url(path_id: str) -> str:
signed_data = TimestampSigner(salt=LOCAL_FILE_ACCESS_TOKEN_SALT).sign(path_id)
token = base64.b16encode(signed_data.encode('utf-8')).decode('utf-8')
@@ -663,6 +741,7 @@ def generate_unauthed_file_access_url(path_id: str) -> str:
filename = path_id.split('/')[-1]
return reverse('local_file_unauthed', args=[token, filename])
def get_local_file_path_id_from_token(token: str) -> Optional[str]:
signer = TimestampSigner(salt=LOCAL_FILE_ACCESS_TOKEN_SALT)
try:
@@ -673,17 +752,26 @@ def get_local_file_path_id_from_token(token: str) -> Optional[str]:
return path_id
class LocalUploadBackend(ZulipUploadBackend):
def upload_message_file(self, uploaded_file_name: str, uploaded_file_size: int,
content_type: Optional[str], file_data: bytes,
user_profile: UserProfile, target_realm: Optional[Realm]=None) -> str:
def upload_message_file(
self,
uploaded_file_name: str,
uploaded_file_size: int,
content_type: Optional[str],
file_data: bytes,
user_profile: UserProfile,
target_realm: Optional[Realm] = None,
) -> str:
# Split into 256 subdirectories to prevent directories from getting too big
path = "/".join([
str(user_profile.realm_id),
format(random.randint(0, 255), 'x'),
secrets.token_urlsafe(18),
sanitize_name(uploaded_file_name),
])
path = "/".join(
[
str(user_profile.realm_id),
format(random.randint(0, 255), 'x'),
secrets.token_urlsafe(18),
sanitize_name(uploaded_file_name),
]
)
write_local_file('files', path, file_data)
create_attachment(uploaded_file_name, path, user_profile, uploaded_file_size)
@@ -701,10 +789,13 @@ class LocalUploadBackend(ZulipUploadBackend):
resized_medium = resize_avatar(image_data, MEDIUM_AVATAR_SIZE)
write_local_file('avatars', file_path + '-medium.png', resized_medium)
def upload_avatar_image(self, user_file: File,
acting_user_profile: UserProfile,
target_user_profile: UserProfile,
content_type: Optional[str] = None) -> None:
def upload_avatar_image(
self,
user_file: File,
acting_user_profile: UserProfile,
target_user_profile: UserProfile,
content_type: Optional[str] = None,
) -> None:
file_path = user_avatar_path(target_user_profile)
image_data = user_file.read()
@@ -717,7 +808,7 @@ class LocalUploadBackend(ZulipUploadBackend):
delete_local_file("avatars", path_id + ".png")
delete_local_file("avatars", path_id + "-medium.png")
def get_avatar_url(self, hash_key: str, medium: bool=False) -> str:
def get_avatar_url(self, hash_key: str, medium: bool = False) -> str:
# ?x=x allows templates to append additional parameters with &s
medium_suffix = "-medium" if medium else ""
return f"/user_avatars/{hash_key}{medium_suffix}.png?x=x"
@@ -735,10 +826,7 @@ class LocalUploadBackend(ZulipUploadBackend):
def upload_realm_icon_image(self, icon_file: File, user_profile: UserProfile) -> None:
upload_path = self.realm_avatar_and_logo_path(user_profile.realm)
image_data = icon_file.read()
write_local_file(
upload_path,
'icon.original',
image_data)
write_local_file(upload_path, 'icon.original', image_data)
resized_data = resize_avatar(image_data)
write_local_file(upload_path, 'icon.png', resized_data)
@@ -747,8 +835,9 @@ class LocalUploadBackend(ZulipUploadBackend):
# ?x=x allows templates to append additional parameters with &s
return f"/user_avatars/{realm_id}/realm/icon.png?version={version}"
def upload_realm_logo_image(self, logo_file: File, user_profile: UserProfile,
night: bool) -> None:
def upload_realm_logo_image(
self, logo_file: File, user_profile: UserProfile, night: bool
) -> None:
upload_path = self.realm_avatar_and_logo_path(user_profile.realm)
if night:
original_file = 'night_logo.original'
@@ -757,10 +846,7 @@ class LocalUploadBackend(ZulipUploadBackend):
original_file = 'logo.original'
resized_file = 'logo.png'
image_data = logo_file.read()
write_local_file(
upload_path,
original_file,
image_data)
write_local_file(upload_path, original_file, image_data)
resized_data = resize_logo(image_data)
write_local_file(upload_path, resized_file, resized_data)
@@ -800,31 +886,31 @@ class LocalUploadBackend(ZulipUploadBackend):
resized_avatar = resize_avatar(image_data)
write_local_file('avatars', file_path + '.png', resized_avatar)
def upload_emoji_image(self, emoji_file: File, emoji_file_name: str,
user_profile: UserProfile) -> None:
def upload_emoji_image(
self, emoji_file: File, emoji_file_name: str, user_profile: UserProfile
) -> None:
emoji_path = RealmEmoji.PATH_ID_TEMPLATE.format(
realm_id= user_profile.realm_id,
realm_id=user_profile.realm_id,
emoji_file_name=emoji_file_name,
)
image_data = emoji_file.read()
resized_image_data = resize_emoji(image_data)
write_local_file(
'avatars',
".".join((emoji_path, "original")),
image_data)
write_local_file(
'avatars',
emoji_path,
resized_image_data)
write_local_file('avatars', ".".join((emoji_path, "original")), image_data)
write_local_file('avatars', emoji_path, resized_image_data)
def get_emoji_url(self, emoji_file_name: str, realm_id: int) -> str:
return os.path.join(
"/user_avatars",
RealmEmoji.PATH_ID_TEMPLATE.format(realm_id=realm_id, emoji_file_name=emoji_file_name))
RealmEmoji.PATH_ID_TEMPLATE.format(realm_id=realm_id, emoji_file_name=emoji_file_name),
)
def upload_export_tarball(self, realm: Realm, tarball_path: str,
percent_callback: Optional[Callable[[Any], None]]=None) -> str:
def upload_export_tarball(
self,
realm: Realm,
tarball_path: str,
percent_callback: Optional[Callable[[Any], None]] = None,
) -> str:
path = os.path.join(
'exports',
str(realm.id),
@@ -849,48 +935,74 @@ class LocalUploadBackend(ZulipUploadBackend):
# export_path has a leading `/`
return realm.uri + export_path
# Common and wrappers
if settings.LOCAL_UPLOADS_DIR is not None:
upload_backend: ZulipUploadBackend = LocalUploadBackend()
else:
upload_backend = S3UploadBackend() # nocoverage
def delete_message_image(path_id: str) -> bool:
return upload_backend.delete_message_image(path_id)
def upload_avatar_image(user_file: File, acting_user_profile: UserProfile,
target_user_profile: UserProfile,
content_type: Optional[str]=None) -> None:
upload_backend.upload_avatar_image(user_file, acting_user_profile,
target_user_profile, content_type=content_type)
def upload_avatar_image(
user_file: File,
acting_user_profile: UserProfile,
target_user_profile: UserProfile,
content_type: Optional[str] = None,
) -> None:
upload_backend.upload_avatar_image(
user_file, acting_user_profile, target_user_profile, content_type=content_type
)
def delete_avatar_image(user_profile: UserProfile) -> None:
upload_backend.delete_avatar_image(user_profile)
def copy_avatar(source_profile: UserProfile, target_profile: UserProfile) -> None:
upload_backend.copy_avatar(source_profile, target_profile)
def upload_icon_image(user_file: File, user_profile: UserProfile) -> None:
upload_backend.upload_realm_icon_image(user_file, user_profile)
def upload_logo_image(user_file: File, user_profile: UserProfile, night: bool) -> None:
upload_backend.upload_realm_logo_image(user_file, user_profile, night)
def upload_emoji_image(emoji_file: File, emoji_file_name: str, user_profile: UserProfile) -> None:
upload_backend.upload_emoji_image(emoji_file, emoji_file_name, user_profile)
def upload_message_file(uploaded_file_name: str, uploaded_file_size: int,
content_type: Optional[str], file_data: bytes,
user_profile: UserProfile, target_realm: Optional[Realm]=None) -> str:
return upload_backend.upload_message_file(uploaded_file_name, uploaded_file_size,
content_type, file_data, user_profile,
target_realm=target_realm)
def claim_attachment(user_profile: UserProfile,
path_id: str,
message: Message,
is_message_realm_public: bool,
is_message_web_public: bool=False) -> Attachment:
def upload_message_file(
uploaded_file_name: str,
uploaded_file_size: int,
content_type: Optional[str],
file_data: bytes,
user_profile: UserProfile,
target_realm: Optional[Realm] = None,
) -> str:
return upload_backend.upload_message_file(
uploaded_file_name,
uploaded_file_size,
content_type,
file_data,
user_profile,
target_realm=target_realm,
)
def claim_attachment(
user_profile: UserProfile,
path_id: str,
message: Message,
is_message_realm_public: bool,
is_message_web_public: bool = False,
) -> Attachment:
attachment = Attachment.objects.get(path_id=path_id)
attachment.messages.add(message)
attachment.is_web_public = attachment.is_web_public or is_message_web_public
@@ -898,24 +1010,39 @@ def claim_attachment(user_profile: UserProfile,
attachment.save()
return attachment
def create_attachment(file_name: str, path_id: str, user_profile: UserProfile,
file_size: int) -> bool:
attachment = Attachment.objects.create(file_name=file_name, path_id=path_id, owner=user_profile,
realm=user_profile.realm, size=file_size)
def create_attachment(
file_name: str, path_id: str, user_profile: UserProfile, file_size: int
) -> bool:
attachment = Attachment.objects.create(
file_name=file_name,
path_id=path_id,
owner=user_profile,
realm=user_profile.realm,
size=file_size,
)
from zerver.lib.actions import notify_attachment_update
notify_attachment_update(user_profile, 'add', attachment.to_dict())
return True
def upload_message_image_from_request(request: HttpRequest, user_file: File,
user_profile: UserProfile) -> str:
uploaded_file_name, uploaded_file_size, content_type = get_file_info(request, user_file)
return upload_message_file(uploaded_file_name, uploaded_file_size,
content_type, user_file.read(), user_profile)
def upload_export_tarball(realm: Realm, tarball_path: str,
percent_callback: Optional[Callable[[Any], None]]=None) -> str:
return upload_backend.upload_export_tarball(realm, tarball_path,
percent_callback=percent_callback)
def upload_message_image_from_request(
request: HttpRequest, user_file: File, user_profile: UserProfile
) -> str:
uploaded_file_name, uploaded_file_size, content_type = get_file_info(request, user_file)
return upload_message_file(
uploaded_file_name, uploaded_file_size, content_type, user_file.read(), user_profile
)
def upload_export_tarball(
realm: Realm, tarball_path: str, percent_callback: Optional[Callable[[Any], None]] = None
) -> str:
return upload_backend.upload_export_tarball(
realm, tarball_path, percent_callback=percent_callback
)
def delete_export_tarball(export_path: str) -> Optional[str]:
return upload_backend.delete_export_tarball(export_path)