From 18a424be7949a3f40a149a4be312d58a913ee8f5 Mon Sep 17 00:00:00 2001 From: vinitS101 Date: Sat, 4 May 2019 23:54:36 +0530 Subject: [PATCH] uploads: Remove unusable UI elements if file uploading is disabled. If MAX_FILE_UPLOAD_SIZE is set to 0, then UI elements like the upload icon in the compose and message edit UI and "Attachments" menu in "/#settings" are not displayed. A different error message is also displayed if a user tries to drag and drop or paste a file into the compose message box. Fixes #12152. --- frontend_tests/node_tests/upload.js | 6 ++++++ static/js/message_edit.js | 7 +++++++ static/js/upload.js | 18 ++++++++++++------ static/templates/message_edit_form.handlebars | 2 ++ templates/zerver/app/compose.html | 8 ++++++-- templates/zerver/app/settings_overlay.html | 2 ++ zerver/tests/test_templates.py | 1 + zerver/views/home.py | 1 + 8 files changed, 37 insertions(+), 8 deletions(-) diff --git a/frontend_tests/node_tests/upload.js b/frontend_tests/node_tests/upload.js index 8b81e82636..ceca0ac29c 100644 --- a/frontend_tests/node_tests/upload.js +++ b/frontend_tests/node_tests/upload.js @@ -101,6 +101,7 @@ run_test('upload_error', () => { var msg_3 = '"foobar.txt" was too large; the maximum file size is 25MB.'; var msg_4 = 'Sorry, the file was too large.'; var msg_5 = 'An unknown error occurred.'; + var msg_6 = 'File and image uploads have been disabled for this organization.'; test('BrowserNotSupported', msg_prefix + msg_1); test('TooManyFiles', msg_prefix + msg_2); @@ -108,6 +109,11 @@ run_test('upload_error', () => { test(413, msg_prefix + msg_4); test(400, 'ちょっと…', {msg: 'ちょっと…'}); test('Do-not-match-any-case', msg_prefix + msg_5); + + // If uploading files has been disabled, then a different error message is + // displayed when a user tries to paste or drag a file onto the UI. + page_params.max_file_upload_size = 0; + test('FileTooLarge', msg_prefix + msg_6, null); }); run_test('upload_finish', () => { diff --git a/static/js/message_edit.js b/static/js/message_edit.js index 35a9e749c1..f299afd45b 100644 --- a/static/js/message_edit.js +++ b/static/js/message_edit.js @@ -274,6 +274,12 @@ function edit_message(row, raw_content) { var editability = get_editability(message, seconds_left_buffer); var is_editable = editability === message_edit.editability_types.TOPIC_ONLY || editability === message_edit.editability_types.FULL; + var max_file_upload_size = page_params.max_file_upload_size; + var file_upload_enabled = false; + + if (max_file_upload_size > 0) { + file_upload_enabled = true; + } var form = $(templates.render( 'message_edit_form', @@ -284,6 +290,7 @@ function edit_message(row, raw_content) { has_been_editable: editability !== editability_types.NO, topic: util.get_message_topic(message), content: raw_content, + file_upload_enabled: file_upload_enabled, minutes_to_edit: Math.floor(page_params.realm_message_content_edit_limit_seconds / 60)})); var edit_obj = {form: form, raw_content: raw_content}; diff --git a/static/js/upload.js b/static/js/upload.js index d5122d3e1f..29c4e4b515 100644 --- a/static/js/upload.js +++ b/static/js/upload.js @@ -101,12 +101,18 @@ exports.options = function (config) { msg = i18n.t("Unable to upload that many files at once."); break; case 'FileTooLarge': - // sanitization not needed as the file name is not potentially parsed as HTML, etc. - var context = { - file_name: file.name, - file_size: page_params.max_file_upload_size, - }; - msg = i18n.t('"__file_name__" was too large; the maximum file size is __file_size__MB.', context); + if (page_params.max_file_upload_size > 0) { + // sanitization not needed as the file name is not potentially parsed as HTML, etc. + var context = { + file_name: file.name, + file_size: page_params.max_file_upload_size, + }; + msg = i18n.t('"__file_name__" was too large; the maximum file size is __file_size__MB.', + context); + } else { + // If uploading files has been disabled. + msg = i18n.t('File and image uploads have been disabled for this organization.'); + } break; case 413: // HTTP status "Request Entity Too Large" msg = i18n.t("Sorry, the file was too large."); diff --git a/static/templates/message_edit_form.handlebars b/static/templates/message_edit_form.handlebars index d5605ae11c..d5aab0574d 100644 --- a/static/templates/message_edit_form.handlebars +++ b/static/templates/message_edit_form.handlebars @@ -41,7 +41,9 @@ + {{#if file_upload_enabled}} + {{/if}} diff --git a/templates/zerver/app/compose.html b/templates/zerver/app/compose.html index 886d86b022..a5286627df 100644 --- a/templates/zerver/app/compose.html +++ b/templates/zerver/app/compose.html @@ -97,8 +97,12 @@ - {% if jitsi_server_url %} - {% endif %} + {% if max_file_upload_size > 0 %} + + {% endif %} + {% if jitsi_server_url %} + + {% endif %} {{ _('Drafts') }} diff --git a/templates/zerver/app/settings_overlay.html b/templates/zerver/app/settings_overlay.html index bf226c327b..da96bced6e 100644 --- a/templates/zerver/app/settings_overlay.html +++ b/templates/zerver/app/settings_overlay.html @@ -33,10 +33,12 @@
{{ _('Alert words') }}
+ {% if max_file_upload_size > 0 %}
  • {{ _('Uploaded files') }}
  • + {% endif %}
  • {{ _('Muted topics') }}
    diff --git a/zerver/tests/test_templates.py b/zerver/tests/test_templates.py index 807f045a2e..b6ab054a17 100644 --- a/zerver/tests/test_templates.py +++ b/zerver/tests/test_templates.py @@ -205,6 +205,7 @@ class TemplateTestCase(ZulipTestCase): seat_count=8, request=RequestFactory().get("/"), invite_as={"MEMBER": 1}, + max_file_upload_size = 25, ) context.update(kwargs) diff --git a/zerver/views/home.py b/zerver/views/home.py index 3599708ff3..3219568ecc 100644 --- a/zerver/views/home.py +++ b/zerver/views/home.py @@ -304,6 +304,7 @@ def home_real(request: HttpRequest) -> HttpResponse: 'enable_feedback': settings.ENABLE_FEEDBACK, 'embedded': narrow_stream is not None, 'invite_as': PreregistrationUser.INVITE_AS, + 'max_file_upload_size': settings.MAX_FILE_UPLOAD_SIZE, },) patch_cache_control(response, no_cache=True, no_store=True, must_revalidate=True) return response