upload: Rename upload_message_image to upload_message_file.

Tweaked by tabbott to also fix a Slack import comment.
This commit is contained in:
Puneeth Chaganti
2018-03-28 21:44:17 +05:30
committed by Tim Abbott
parent f839d528d8
commit 4ce8f2aaa2
5 changed files with 30 additions and 30 deletions

View File

@@ -14,7 +14,7 @@ from zerver.lib.actions import decode_email_address, get_email_gateway_message_s
from zerver.lib.notifications import convert_html_to_markdown
from zerver.lib.queue import queue_json_publish
from zerver.lib.redis_utils import get_redis_client
from zerver.lib.upload import upload_message_image
from zerver.lib.upload import upload_message_file
from zerver.lib.utils import generate_random_token
from zerver.lib.str_utils import force_text
from zerver.lib.send_email import FromAddress
@@ -256,10 +256,10 @@ def extract_and_upload_attachments(message: message.Message, realm: Realm) -> Te
if filename:
attachment = part.get_payload(decode=True)
if isinstance(attachment, bytes):
s3_url = upload_message_image(filename, len(attachment), content_type,
attachment,
user_profile,
target_realm=realm)
s3_url = upload_message_file(filename, len(attachment), content_type,
attachment,
user_profile,
target_realm=realm)
formatted_link = "[%s](%s)" % (filename, s3_url)
attachment_links.append(formatted_link)
else:

View File

@@ -671,7 +671,7 @@ def channel_message_to_zerver_message(realm_id: int, users: List[ZerverFieldsT],
def get_attachment_path_and_content(fileinfo: ZerverFieldsT, realm_id: int) -> Tuple[str,
str]:
# Should be kept in sync with its equivalent in zerver/lib/uploads in the function
# 'upload_message_image'
# 'upload_message_file'
s3_path = "/".join([
str(realm_id),
format(random.randint(0, 255), 'x'),

View File

@@ -118,10 +118,10 @@ def resize_emoji(image_data: bytes, size: int=DEFAULT_EMOJI_SIZE) -> bytes:
### Common
class ZulipUploadBackend:
def upload_message_image(self, uploaded_file_name: Text, uploaded_file_size: int,
content_type: Optional[Text], file_data: bytes,
user_profile: UserProfile,
target_realm: Optional[Realm]=None) -> Text:
def upload_message_file(self, uploaded_file_name: Text, uploaded_file_size: int,
content_type: Optional[Text], file_data: bytes,
user_profile: UserProfile,
target_realm: Optional[Realm]=None) -> Text:
raise NotImplementedError()
def upload_avatar_image(self, user_file: File,
@@ -236,9 +236,9 @@ def get_realm_for_filename(path: Text) -> Optional[int]:
class S3UploadBackend(ZulipUploadBackend):
def upload_message_image(self, uploaded_file_name: Text, uploaded_file_size: int,
content_type: Optional[Text], file_data: bytes,
user_profile: UserProfile, target_realm: Optional[Realm]=None) -> Text:
def upload_message_file(self, uploaded_file_name: Text, uploaded_file_size: int,
content_type: Optional[Text], file_data: bytes,
user_profile: UserProfile, target_realm: Optional[Realm]=None) -> Text:
bucket_name = settings.S3_AUTH_UPLOADS_BUCKET
if target_realm is None:
target_realm = user_profile.realm
@@ -415,9 +415,9 @@ def get_local_file_path(path_id: Text) -> Optional[Text]:
return None
class LocalUploadBackend(ZulipUploadBackend):
def upload_message_image(self, uploaded_file_name: Text, uploaded_file_size: int,
content_type: Optional[Text], file_data: bytes,
user_profile: UserProfile, target_realm: Optional[Realm]=None) -> Text:
def upload_message_file(self, uploaded_file_name: Text, uploaded_file_size: int,
content_type: Optional[Text], file_data: bytes,
user_profile: UserProfile, target_realm: Optional[Realm]=None) -> Text:
# Split into 256 subdirectories to prevent directories from getting too big
path = "/".join([
str(user_profile.realm_id),
@@ -530,12 +530,12 @@ def upload_icon_image(user_file: File, user_profile: UserProfile) -> None:
def upload_emoji_image(emoji_file: File, emoji_file_name: Text, user_profile: UserProfile) -> None:
upload_backend.upload_emoji_image(emoji_file, emoji_file_name, user_profile)
def upload_message_image(uploaded_file_name: Text, uploaded_file_size: int,
content_type: Optional[Text], file_data: bytes,
user_profile: UserProfile, target_realm: Optional[Realm]=None) -> Text:
return upload_backend.upload_message_image(uploaded_file_name, uploaded_file_size,
content_type, file_data, user_profile,
target_realm=target_realm)
def upload_message_file(uploaded_file_name: Text, uploaded_file_size: int,
content_type: Optional[Text], file_data: bytes,
user_profile: UserProfile, target_realm: Optional[Realm]=None) -> Text:
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: Text,
@@ -555,5 +555,5 @@ def create_attachment(file_name: Text, path_id: Text, user_profile: UserProfile,
def upload_message_image_from_request(request: HttpRequest, user_file: File,
user_profile: UserProfile) -> Text:
uploaded_file_name, uploaded_file_size, content_type = get_file_info(request, user_file)
return upload_message_image(uploaded_file_name, uploaded_file_size,
content_type, user_file.read(), user_profile)
return upload_message_file(uploaded_file_name, uploaded_file_size,
content_type, user_file.read(), user_profile)

View File

@@ -20,7 +20,7 @@ from zerver.lib.export import (
)
from zerver.lib.upload import (
claim_attachment,
upload_message_image,
upload_message_file,
)
from zerver.lib.utils import (
query_chunker,
@@ -212,7 +212,7 @@ class ExportTest(ZulipTestCase):
def test_attachment(self) -> None:
message = Message.objects.all()[0]
user_profile = message.sender
url = upload_message_image(u'dummy.txt', len(b'zulip!'), u'text/plain', b'zulip!', user_profile)
url = upload_message_file(u'dummy.txt', len(b'zulip!'), u'text/plain', b'zulip!', user_profile)
path_id = url.replace('/user_uploads/', '')
claim_attachment(
user_profile=user_profile,

View File

@@ -19,7 +19,7 @@ from zerver.lib.test_helpers import (
)
from zerver.lib.test_runner import slow
from zerver.lib.upload import sanitize_name, S3UploadBackend, \
upload_message_image, delete_message_image, LocalUploadBackend, \
upload_message_file, delete_message_image, LocalUploadBackend, \
ZulipUploadBackend, MEDIUM_AVATAR_SIZE, resize_avatar
import zerver.lib.upload
from zerver.models import Attachment, get_user, \
@@ -946,7 +946,7 @@ class LocalStorageTest(UploadSerializeMixin, ZulipTestCase):
def test_file_upload_local(self) -> None:
user_profile = self.example_user('hamlet')
uri = upload_message_image(u'dummy.txt', len(b'zulip!'), u'text/plain', b'zulip!', user_profile)
uri = upload_message_file(u'dummy.txt', len(b'zulip!'), u'text/plain', b'zulip!', user_profile)
base = '/user_uploads/'
self.assertEqual(base, uri[:len(base)])
@@ -977,7 +977,7 @@ class S3Test(ZulipTestCase):
bucket = conn.create_bucket(settings.S3_AUTH_UPLOADS_BUCKET)
user_profile = self.example_user('hamlet')
uri = upload_message_image(u'dummy.txt', len(b'zulip!'), u'text/plain', b'zulip!', user_profile)
uri = upload_message_file(u'dummy.txt', len(b'zulip!'), u'text/plain', b'zulip!', user_profile)
base = '/user_uploads/'
self.assertEqual(base, uri[:len(base)])
@@ -999,7 +999,7 @@ class S3Test(ZulipTestCase):
conn.create_bucket(settings.S3_AUTH_UPLOADS_BUCKET)
user_profile = self.example_user('hamlet')
uri = upload_message_image(u'dummy.txt', len(b'zulip!'), u'text/plain', b'zulip!', user_profile)
uri = upload_message_file(u'dummy.txt', len(b'zulip!'), u'text/plain', b'zulip!', user_profile)
path_id = re.sub('/user_uploads/', '', uri)
self.assertTrue(delete_message_image(path_id))