diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-03-07 09:15:33 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-03-07 09:15:33 +0300 |
commit | 0ead22f9db870eb1d6914ef60d5c1c998ed538d9 (patch) | |
tree | 42cc4f297aa116bc517c86c943428ef0840b70ef /app/validators | |
parent | 9d20ce8c998506a1e27c55bbf99cd4c0595185af (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/validators')
-rw-r--r-- | app/validators/import/gitlab_projects/remote_file_validator.rb | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/app/validators/import/gitlab_projects/remote_file_validator.rb b/app/validators/import/gitlab_projects/remote_file_validator.rb new file mode 100644 index 00000000000..67bf102e928 --- /dev/null +++ b/app/validators/import/gitlab_projects/remote_file_validator.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +module Import + module GitlabProjects + # Validates the given object's #content_type and #content_length accordingly + # with the Project Import requirements + class RemoteFileValidator < ActiveModel::Validator + FILE_SIZE_LIMIT = 10.gigabytes + ALLOWED_CONTENT_TYPES = [ + 'application/gzip', + # S3 uses different file types + 'application/x-tar', + 'application/x-gzip' + ].freeze + + def validate(record) + validate_content_length(record) + validate_content_type(record) + end + + private + + def validate_content_length(record) + if record.content_length.to_i <= 0 + record.errors.add(:content_length, :size_too_small, file_size: humanize(1.byte)) + elsif record.content_length > FILE_SIZE_LIMIT + record.errors.add(:content_length, :size_too_big, file_size: humanize(FILE_SIZE_LIMIT)) + end + end + + def humanize(number) + ActiveSupport::NumberHelper.number_to_human_size(number) + end + + def validate_content_type(record) + return if ALLOWED_CONTENT_TYPES.include?(record.content_type) + + record.errors.add(:content_type, "'%{content_type}' not allowed. (Allowed: %{allowed})" % { + content_type: record.content_type, + allowed: ALLOWED_CONTENT_TYPES.join(', ') + }) + end + end + end +end |