diff --git a/app/controllers/projects/blob_controller.rb b/app/controllers/projects/blob_controller.rb index 173c6bcee53..be5822b2cd4 100644 --- a/app/controllers/projects/blob_controller.rb +++ b/app/controllers/projects/blob_controller.rb @@ -35,6 +35,8 @@ class Projects::BlobController < Projects::ApplicationController end def show + @blob.override_max_size! if params[:override_max_size] == 'true' + respond_to do |format| format.html do environment_params = @repository.branch_exists?(@ref) ? { ref: @ref } : { commit: @commit } diff --git a/app/helpers/blob_helper.rb b/app/helpers/blob_helper.rb index 7db9cf0ff01..00cf0ac96c9 100644 --- a/app/helpers/blob_helper.rb +++ b/app/helpers/blob_helper.rb @@ -219,7 +219,13 @@ module BlobHelper def blob_render_error_reason(viewer, error) case error when :too_large - "it is larger than #{number_to_human_size(viewer.relevant_max_size)}" + max_size = + if viewer.absolutely_too_large? + viewer.absolute_max_size + elsif viewer.too_large? + viewer.max_size + end + "it is larger than #{number_to_human_size(max_size)}" when :server_side_but_stored_in_lfs "it is stored in LFS" end @@ -232,7 +238,7 @@ module BlobHelper options << link_to('load it anyway', url_for(params.merge(viewer: viewer.type, override_max_size: true, format: nil))) end - if viewer.rich? && viewer.blob.rendered_as_text?(override_max_size: true) + if viewer.rich? && viewer.blob.rendered_as_text? options << link_to('view the source', '#', class: 'js-blob-viewer-switch-btn', data: { viewer: 'simple' }) end diff --git a/app/models/blob.rb b/app/models/blob.rb index 65356f01cc2..dedf60ca14a 100644 --- a/app/models/blob.rb +++ b/app/models/blob.rb @@ -122,12 +122,17 @@ class Blob < SimpleDelegator @rich_viewer ||= rich_viewer_class&.new(self) end - def rendered_as_text?(override_max_size: false) - simple_viewer.is_a?(BlobViewer::Text) && !simple_viewer.render_error(override_max_size: override_max_size) + def rendered_as_text?(ignore_errors: true) + simple_viewer.is_a?(BlobViewer::Text) && (ignore_errors || simple_viewer.render_error.nil?) end def show_viewer_switcher? - simple_viewer.is_a?(BlobViewer::Text) && rich_viewer + rendered_as_text? && rich_viewer + end + + def override_max_size! + simple_viewer&.override_max_size = true + rich_viewer&.override_max_size = true end private diff --git a/app/models/blob_viewer/base.rb b/app/models/blob_viewer/base.rb index 8e6919f1054..714a063933b 100644 --- a/app/models/blob_viewer/base.rb +++ b/app/models/blob_viewer/base.rb @@ -5,6 +5,7 @@ module BlobViewer delegate :partial_path, :rich?, :simple?, :client_side?, :server_side?, :text_based?, to: :class attr_reader :blob + attr_accessor :override_max_size def initialize(blob) @blob = blob @@ -38,20 +39,20 @@ module BlobViewer !extensions || extensions.include?(blob.extension) end + def too_large? + blob.raw_size > max_size + end + + def absolutely_too_large? + blob.raw_size > absolute_max_size + end + def can_override_max_size? - too_large? && !too_large?(override_max_size: true) + too_large? && !absolutely_too_large? end - def relevant_max_size - if too_large?(override_max_size: true) - absolute_max_size - elsif too_large? - max_size - end - end - - def render_error(override_max_size: false) - if too_large?(override_max_size: override_max_size) + def render_error + if override_max_size ? absolutely_too_large? : too_large? :too_large elsif server_side_but_stored_in_lfs? :server_side_but_stored_in_lfs @@ -66,14 +67,6 @@ module BlobViewer private - def too_large?(override_max_size: false) - if override_max_size - blob.raw_size > absolute_max_size - else - blob.raw_size > max_size - end - end - def server_side_but_stored_in_lfs? server_side? && blob.valid_lfs_pointer? end diff --git a/app/models/blob_viewer/download.rb b/app/models/blob_viewer/download.rb index 8f293ea6008..dbd6f07dbc8 100644 --- a/app/models/blob_viewer/download.rb +++ b/app/models/blob_viewer/download.rb @@ -9,7 +9,7 @@ module BlobViewer self.partial_name = 'download' self.text_based = false - def render_error(*) + def render_error nil end end diff --git a/app/views/projects/blob/_header.html.haml b/app/views/projects/blob/_header.html.haml index 19eca2984db..b89cd460455 100644 --- a/app/views/projects/blob/_header.html.haml +++ b/app/views/projects/blob/_header.html.haml @@ -15,7 +15,7 @@ = render 'projects/blob/viewer_switcher', blob: blob unless blame .btn-group{ role: "group" }< - = copy_blob_source_button(blob) if !blame && blob.rendered_as_text?(override_max_size: params[:override_max_size]) + = copy_blob_source_button(blob) if !blame && blob.rendered_as_text?(ignore_errors: false) = open_raw_file_button(namespace_project_raw_path(@project.namespace, @project, @id)) = view_on_environment_button(@commit.sha, @path, @environment) if @environment diff --git a/app/views/projects/blob/_viewer.html.haml b/app/views/projects/blob/_viewer.html.haml index e0cfe39d1ec..1c45c4a68ce 100644 --- a/app/views/projects/blob/_viewer.html.haml +++ b/app/views/projects/blob/_viewer.html.haml @@ -1,5 +1,5 @@ - hidden = local_assigns.fetch(:hidden, false) -- render_error = viewer.render_error(override_max_size: params[:override_max_size]) +- render_error = viewer.render_error - load_asynchronously = local_assigns.fetch(:load_asynchronously, viewer.server_side?) && render_error.nil? - url = url_for(params.merge(viewer: viewer.type, format: :json)) if load_asynchronously