diff --git a/app/views/avatar/_new_avatar_form.html.erb b/app/views/avatar/_new_avatar_form.html.erb index 793252d3a..64abfbc30 100644 --- a/app/views/avatar/_new_avatar_form.html.erb +++ b/app/views/avatar/_new_avatar_form.html.erb @@ -9,8 +9,8 @@ :size => "1", :multiple => true, :data => { - :max_file_size => Setting.attachment_max_size.to_i.kilobytes, - :max_file_size_message => l(:error_attachment_too_big, :max_size => number_to_human_size(Setting.attachment_max_size.to_i.kilobytes)), + :max_file_size => Setting.upload_avatar_max_size, + :max_file_size_message => l(:error_upload_avatar_to_large, :max_size => number_to_human_size(Setting.upload_avatar_max_size.to_i)), :max_concurrent_uploads => Redmine::Configuration['max_concurrent_ajax_uploads'].to_i, :file_type => Redmine::Configuration['pic_types'].to_s, :type_support_message => l(:error_pic_type), diff --git a/config/locales/en.yml b/config/locales/en.yml index 9cf572830..8989ae36a 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1520,3 +1520,5 @@ en: label_no_courses: You do not participate in any course, please search the curriculum, course, or create a course! label_commit_failed: commit failed #api end + error_upload_avatar_to_large: "too big (%{max_size})" + diff --git a/config/locales/zh.yml b/config/locales/zh.yml index de3ae4d53..5fcb1fb5c 100644 --- a/config/locales/zh.yml +++ b/config/locales/zh.yml @@ -1983,3 +1983,4 @@ zh: label_media: 媒体 label_code: 代码 + error_upload_avatar_to_large: "超过大小限制 (%{max_size})" diff --git a/config/settings.yml b/config/settings.yml index 1dc3c7151..cf98025e0 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -60,7 +60,9 @@ per_page_options: default: '25,50,100' mail_from: default: trustieforge@gmail.com - +upload_avatar_max_size: + format: int + default: 5242880 ### delayjob for send email. delayjob_enabled: default: 1 diff --git a/public/javascripts/jq-upload/upload.js b/public/javascripts/jq-upload/upload.js index 59ed96882..985450648 100644 --- a/public/javascripts/jq-upload/upload.js +++ b/public/javascripts/jq-upload/upload.js @@ -1,28 +1,44 @@ $(function() { var $upload_file = $('.upload_file'); - var validateImage = function(file){ - if(!/^image\//.test(file.type)){ - alert($upload_file.attr("data-type-support-message") + $upload_file.attr("data-file-type")); - return false; - } - return true; + + function endsWith(str, suffix) { + return str.indexOf(suffix, str.length - suffix.length) !== -1; + } + var validateImage = function(file) { + var types = $upload_file.attr('data-file-type').split(","); + var check = false; + $.each(types, function(n, e) { + if (endsWith(file.name, e)) { + check = true; + } + }); + if (!check) { + alert($upload_file.attr("data-type-support-message") + $upload_file.attr("data-file-type")); + return false; + } + if (file.size && file.size > parseInt($upload_file.attr('data-max-file-size'))) { + alert($upload_file.attr("data-max-file-size-message")); + return false; + } + return true; }; $('.upload_file').fileupload({ url: '/upload_avatar.json?source_type=' + $('.upload_file').attr('data-source-type') + '&source_id=' + $('.upload_file').attr('data-source-id'), - add: function(e,data){ - if(!validateImage(data.files[0])){ - return false; - } - data.submit(); + add: function(e, data) { + if (!validateImage(data.files[0])) { + return false; + } + data.submit(); }, formData: function(form) { var data = form.serializeArray(); var auth = null; - for(var key in data){ - if(data[key].name == "authenticity_token"){ - auth = data[key];break; - } + for (var key in data) { + if (data[key].name == "authenticity_token") { + auth = data[key]; + break; + } } return [auth]; },