diff options
author | Robert Speicher <robert@gitlab.com> | 2017-05-16 23:26:04 +0300 |
---|---|---|
committer | Robert Speicher <robert@gitlab.com> | 2017-05-16 23:26:04 +0300 |
commit | 67312fceaa69abb2bc88aa62601625e9f6a62270 (patch) | |
tree | f67e39bab7386f870e4908dba0746914a4db7d29 /app/models/blob_viewer | |
parent | e66b811d81d399fdfeed13563db108242f9021e6 (diff) | |
parent | b0a163208c1b428fc9f5adad6489f8a3ab884a2b (diff) |
Merge branch 'dm-blob-viewer-concerns' into 'master'
Move some blob viewer stuff around without changing behavior
See merge request !11358
Diffstat (limited to 'app/models/blob_viewer')
-rw-r--r-- | app/models/blob_viewer/auxiliary.rb | 2 | ||||
-rw-r--r-- | app/models/blob_viewer/base.rb | 58 | ||||
-rw-r--r-- | app/models/blob_viewer/client_side.rb | 6 | ||||
-rw-r--r-- | app/models/blob_viewer/download.rb | 10 | ||||
-rw-r--r-- | app/models/blob_viewer/gitlab_ci_yml.rb | 2 | ||||
-rw-r--r-- | app/models/blob_viewer/license.rb | 7 | ||||
-rw-r--r-- | app/models/blob_viewer/route_map.rb | 2 | ||||
-rw-r--r-- | app/models/blob_viewer/server_side.rb | 19 | ||||
-rw-r--r-- | app/models/blob_viewer/static.rb | 14 | ||||
-rw-r--r-- | app/models/blob_viewer/text.rb | 4 |
10 files changed, 66 insertions, 58 deletions
diff --git a/app/models/blob_viewer/auxiliary.rb b/app/models/blob_viewer/auxiliary.rb index db124397b27..cd6e596ed60 100644 --- a/app/models/blob_viewer/auxiliary.rb +++ b/app/models/blob_viewer/auxiliary.rb @@ -5,8 +5,8 @@ module BlobViewer included do self.loading_partial_name = 'loading_auxiliary' self.type = :auxiliary + self.overridable_max_size = 100.kilobytes self.max_size = 100.kilobytes - self.absolute_max_size = 100.kilobytes end end end diff --git a/app/models/blob_viewer/base.rb b/app/models/blob_viewer/base.rb index 4f38c31714b..c7b8fbfc56a 100644 --- a/app/models/blob_viewer/base.rb +++ b/app/models/blob_viewer/base.rb @@ -2,11 +2,11 @@ module BlobViewer class Base PARTIAL_PATH_PREFIX = 'projects/blob/viewers'.freeze - class_attribute :partial_name, :loading_partial_name, :type, :extensions, :file_type, :client_side, :binary, :switcher_icon, :switcher_title, :max_size, :absolute_max_size + class_attribute :partial_name, :loading_partial_name, :type, :extensions, :file_types, :load_async, :binary, :switcher_icon, :switcher_title, :overridable_max_size, :max_size self.loading_partial_name = 'loading' - delegate :partial_path, :loading_partial_path, :rich?, :simple?, :client_side?, :server_side?, :text?, :binary?, to: :class + delegate :partial_path, :loading_partial_path, :rich?, :simple?, :text?, :binary?, to: :class attr_reader :blob attr_accessor :override_max_size @@ -35,12 +35,8 @@ module BlobViewer type == :auxiliary end - def self.client_side? - client_side - end - - def self.server_side? - !client_side? + def self.load_async? + load_async end def self.binary? @@ -54,21 +50,33 @@ module BlobViewer def self.can_render?(blob, verify_binary: true) return false if verify_binary && binary? != blob.binary? return true if extensions&.include?(blob.extension) - return true if file_type && Gitlab::FileDetector.type_of(blob.path) == file_type + return true if file_types&.include?(Gitlab::FileDetector.type_of(blob.path)) false end - def too_large? - blob.raw_size > max_size + def load_async? + self.class.load_async? && render_error.nil? end - def absolutely_too_large? - blob.raw_size > absolute_max_size + def exceeds_overridable_max_size? + overridable_max_size && blob.raw_size > overridable_max_size + end + + def exceeds_max_size? + max_size && blob.raw_size > max_size end def can_override_max_size? - too_large? && !absolutely_too_large? + exceeds_overridable_max_size? && !exceeds_max_size? + end + + def too_large? + if override_max_size + exceeds_max_size? + else + exceeds_overridable_max_size? + end end # This method is used on the server side to check whether we can attempt to @@ -83,29 +91,13 @@ module BlobViewer # binary from `blob_raw_url` and does its own format validation and error # rendering, especially for potentially large binary formats. def render_error - return @render_error if defined?(@render_error) - - @render_error = - if server_side_but_stored_externally? - # Files that are not stored in the repository, like LFS files and - # build artifacts, can only be rendered using a client-side viewer, - # since we do not want to read large amounts of data into memory on the - # server side. Client-side viewers use JS and can fetch the file from - # `blob_raw_url` using AJAX. - :server_side_but_stored_externally - elsif override_max_size ? absolutely_too_large? : too_large? - :too_large - end + if too_large? + :too_large + end end def prepare! # To be overridden by subclasses end - - private - - def server_side_but_stored_externally? - server_side? && blob.stored_externally? - end end end diff --git a/app/models/blob_viewer/client_side.rb b/app/models/blob_viewer/client_side.rb index 42ec68f864b..cc68236f92b 100644 --- a/app/models/blob_viewer/client_side.rb +++ b/app/models/blob_viewer/client_side.rb @@ -3,9 +3,9 @@ module BlobViewer extend ActiveSupport::Concern included do - self.client_side = true - self.max_size = 10.megabytes - self.absolute_max_size = 50.megabytes + self.load_async = false + self.overridable_max_size = 10.megabytes + self.max_size = 50.megabytes end end end diff --git a/app/models/blob_viewer/download.rb b/app/models/blob_viewer/download.rb index adc06587f69..074e7204814 100644 --- a/app/models/blob_viewer/download.rb +++ b/app/models/blob_viewer/download.rb @@ -1,17 +1,9 @@ module BlobViewer class Download < Base include Simple - # We treat the Download viewer as if it renders the content client-side, - # so that it doesn't attempt to load the entire blob contents and is - # rendered synchronously instead of loaded asynchronously. - include ClientSide + include Static self.partial_name = 'download' self.binary = true - - # We can always render the Download viewer, even if the blob is in LFS or too large. - def render_error - nil - end end end diff --git a/app/models/blob_viewer/gitlab_ci_yml.rb b/app/models/blob_viewer/gitlab_ci_yml.rb index 81afab2f49b..7267c3965d3 100644 --- a/app/models/blob_viewer/gitlab_ci_yml.rb +++ b/app/models/blob_viewer/gitlab_ci_yml.rb @@ -5,7 +5,7 @@ module BlobViewer self.partial_name = 'gitlab_ci_yml' self.loading_partial_name = 'gitlab_ci_yml_loading' - self.file_type = :gitlab_ci + self.file_types = %i(gitlab_ci) self.binary = false def validation_message diff --git a/app/models/blob_viewer/license.rb b/app/models/blob_viewer/license.rb index 3ad49570c88..6751ea15a5d 100644 --- a/app/models/blob_viewer/license.rb +++ b/app/models/blob_viewer/license.rb @@ -1,13 +1,10 @@ module BlobViewer class License < Base - # We treat the License viewer as if it renders the content client-side, - # so that it doesn't attempt to load the entire blob contents and is - # rendered synchronously instead of loaded asynchronously. - include ClientSide include Auxiliary + include Static self.partial_name = 'license' - self.file_type = :license + self.file_types = %i(license) self.binary = false def license diff --git a/app/models/blob_viewer/route_map.rb b/app/models/blob_viewer/route_map.rb index 1ca730c1ea0..153b4eeb2c9 100644 --- a/app/models/blob_viewer/route_map.rb +++ b/app/models/blob_viewer/route_map.rb @@ -5,7 +5,7 @@ module BlobViewer self.partial_name = 'route_map' self.loading_partial_name = 'route_map_loading' - self.file_type = :route_map + self.file_types = %i(route_map) self.binary = false def validation_message diff --git a/app/models/blob_viewer/server_side.rb b/app/models/blob_viewer/server_side.rb index e8c5c17b824..87884dcd6bf 100644 --- a/app/models/blob_viewer/server_side.rb +++ b/app/models/blob_viewer/server_side.rb @@ -3,9 +3,9 @@ module BlobViewer extend ActiveSupport::Concern included do - self.client_side = false - self.max_size = 2.megabytes - self.absolute_max_size = 5.megabytes + self.load_async = true + self.overridable_max_size = 2.megabytes + self.max_size = 5.megabytes end def prepare! @@ -13,5 +13,18 @@ module BlobViewer blob.load_all_data!(blob.project.repository) end end + + def render_error + if blob.stored_externally? + # Files that are not stored in the repository, like LFS files and + # build artifacts, can only be rendered using a client-side viewer, + # since we do not want to read large amounts of data into memory on the + # server side. Client-side viewers use JS and can fetch the file from + # `blob_raw_url` using AJAX. + return :server_side_but_stored_externally + end + + super + end end end diff --git a/app/models/blob_viewer/static.rb b/app/models/blob_viewer/static.rb new file mode 100644 index 00000000000..c9e257e5388 --- /dev/null +++ b/app/models/blob_viewer/static.rb @@ -0,0 +1,14 @@ +module BlobViewer + module Static + extend ActiveSupport::Concern + + included do + self.load_async = false + end + + # We can always render a static viewer, even if the blob is too large. + def render_error + nil + end + end +end diff --git a/app/models/blob_viewer/text.rb b/app/models/blob_viewer/text.rb index e27b2c2b493..eddca50b4d4 100644 --- a/app/models/blob_viewer/text.rb +++ b/app/models/blob_viewer/text.rb @@ -5,7 +5,7 @@ module BlobViewer self.partial_name = 'text' self.binary = false - self.max_size = 1.megabyte - self.absolute_max_size = 10.megabytes + self.overridable_max_size = 1.megabyte + self.max_size = 10.megabytes end end |