Commit 0e0c760e authored by Douwe Maan's avatar Douwe Maan

Refactor overriding max size

parent a7fd95cd
...@@ -35,6 +35,8 @@ class Projects::BlobController < Projects::ApplicationController ...@@ -35,6 +35,8 @@ class Projects::BlobController < Projects::ApplicationController
end end
def show def show
@blob.override_max_size! if params[:override_max_size] == 'true'
respond_to do |format| respond_to do |format|
format.html do format.html do
environment_params = @repository.branch_exists?(@ref) ? { ref: @ref } : { commit: @commit } environment_params = @repository.branch_exists?(@ref) ? { ref: @ref } : { commit: @commit }
......
...@@ -219,7 +219,13 @@ module BlobHelper ...@@ -219,7 +219,13 @@ module BlobHelper
def blob_render_error_reason(viewer, error) def blob_render_error_reason(viewer, error)
case error case error
when :too_large 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 when :server_side_but_stored_in_lfs
"it is stored in LFS" "it is stored in LFS"
end end
...@@ -232,7 +238,7 @@ module BlobHelper ...@@ -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))) options << link_to('load it anyway', url_for(params.merge(viewer: viewer.type, override_max_size: true, format: nil)))
end 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' }) options << link_to('view the source', '#', class: 'js-blob-viewer-switch-btn', data: { viewer: 'simple' })
end end
......
...@@ -122,12 +122,17 @@ class Blob < SimpleDelegator ...@@ -122,12 +122,17 @@ class Blob < SimpleDelegator
@rich_viewer ||= rich_viewer_class&.new(self) @rich_viewer ||= rich_viewer_class&.new(self)
end end
def rendered_as_text?(override_max_size: false) def rendered_as_text?(ignore_errors: true)
simple_viewer.is_a?(BlobViewer::Text) && !simple_viewer.render_error(override_max_size: override_max_size) simple_viewer.is_a?(BlobViewer::Text) && (ignore_errors || simple_viewer.render_error.nil?)
end end
def show_viewer_switcher? 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 end
private private
......
...@@ -5,6 +5,7 @@ module BlobViewer ...@@ -5,6 +5,7 @@ module BlobViewer
delegate :partial_path, :rich?, :simple?, :client_side?, :server_side?, :text_based?, to: :class delegate :partial_path, :rich?, :simple?, :client_side?, :server_side?, :text_based?, to: :class
attr_reader :blob attr_reader :blob
attr_accessor :override_max_size
def initialize(blob) def initialize(blob)
@blob = blob @blob = blob
...@@ -38,20 +39,20 @@ module BlobViewer ...@@ -38,20 +39,20 @@ module BlobViewer
!extensions || extensions.include?(blob.extension) !extensions || extensions.include?(blob.extension)
end end
def can_override_max_size? def too_large?
too_large? && !too_large?(override_max_size: true) blob.raw_size > max_size
end end
def relevant_max_size def absolutely_too_large?
if too_large?(override_max_size: true) blob.raw_size > absolute_max_size
absolute_max_size end
elsif too_large?
max_size def can_override_max_size?
end too_large? && !absolutely_too_large?
end end
def render_error(override_max_size: false) def render_error
if too_large?(override_max_size: override_max_size) if override_max_size ? absolutely_too_large? : too_large?
:too_large :too_large
elsif server_side_but_stored_in_lfs? elsif server_side_but_stored_in_lfs?
:server_side_but_stored_in_lfs :server_side_but_stored_in_lfs
...@@ -66,14 +67,6 @@ module BlobViewer ...@@ -66,14 +67,6 @@ module BlobViewer
private 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? def server_side_but_stored_in_lfs?
server_side? && blob.valid_lfs_pointer? server_side? && blob.valid_lfs_pointer?
end end
......
...@@ -9,7 +9,7 @@ module BlobViewer ...@@ -9,7 +9,7 @@ module BlobViewer
self.partial_name = 'download' self.partial_name = 'download'
self.text_based = false self.text_based = false
def render_error(*) def render_error
nil nil
end end
end end
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
= render 'projects/blob/viewer_switcher', blob: blob unless blame = render 'projects/blob/viewer_switcher', blob: blob unless blame
.btn-group{ role: "group" }< .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)) = open_raw_file_button(namespace_project_raw_path(@project.namespace, @project, @id))
= view_on_environment_button(@commit.sha, @path, @environment) if @environment = view_on_environment_button(@commit.sha, @path, @environment) if @environment
......
- hidden = local_assigns.fetch(:hidden, false) - 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? - 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 - url = url_for(params.merge(viewer: viewer.type, format: :json)) if load_asynchronously
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment