Refactor overriding max size

This commit is contained in:
Douwe Maan 2017-04-21 13:59:34 -05:00
parent a7fd95cd22
commit 0e0c760e48
7 changed files with 33 additions and 27 deletions

View file

@ -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 }

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -9,7 +9,7 @@ module BlobViewer
self.partial_name = 'download'
self.text_based = false
def render_error(*)
def render_error
nil
end
end

View file

@ -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

View file

@ -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