Commit f711b2df authored by Douwe Maan's avatar Douwe Maan

Clean up some backend

parent 2197ee02
module RendersBlob module RendersBlob
extend ActiveSupport::Concern extend ActiveSupport::Concern
def render_blob_json(blob) def blob_json(blob)
viewer = viewer =
case params[:viewer] case params[:viewer]
when 'rich' when 'rich'
...@@ -12,26 +12,18 @@ module RendersBlob ...@@ -12,26 +12,18 @@ module RendersBlob
blob.simple_viewer blob.simple_viewer
end end
return render_404 unless viewer return unless viewer
if blob.binary? {
render json: { html: view_to_html_string("projects/blob/_viewer", viewer: viewer, load_async: false)
binary: true,
mime_type: blob.mime_type,
name: blob.name,
extension: blob.extension,
size: blob.raw_size
}
else
render json: {
html: view_to_html_string("projects/blob/_viewer", viewer: viewer, load_async: false),
plain: blob.data,
name: blob.name,
extension: blob.extension,
size: blob.raw_size,
mime_type: blob.mime_type
} }
end end
def render_blob_json(blob)
json = blob_json(blob)
return render_404 unless json
render json: json
end end
def conditionally_expand_blob(blob) def conditionally_expand_blob(blob)
......
...@@ -4,7 +4,6 @@ class Projects::BlobController < Projects::ApplicationController ...@@ -4,7 +4,6 @@ class Projects::BlobController < Projects::ApplicationController
include CreatesCommit include CreatesCommit
include RendersBlob include RendersBlob
include ActionView::Helpers::SanitizeHelper include ActionView::Helpers::SanitizeHelper
include ApplicationHelper
# Raised when given an invalid file path # Raised when given an invalid file path
InvalidPathError = Class.new(StandardError) InvalidPathError = Class.new(StandardError)
...@@ -38,17 +37,32 @@ class Projects::BlobController < Projects::ApplicationController ...@@ -38,17 +37,32 @@ class Projects::BlobController < Projects::ApplicationController
respond_to do |format| respond_to do |format|
format.html do format.html do
assign_ref_vars
environment_params = @repository.branch_exists?(@ref) ? { ref: @ref } : { commit: @commit } environment_params = @repository.branch_exists?(@ref) ? { ref: @ref } : { commit: @commit }
@environment = EnvironmentsFinder.new(@project, current_user, environment_params).execute.last @environment = EnvironmentsFinder.new(@project, current_user, environment_params).execute.last
@last_commit = @repository.last_commit_for_path(@commit.id, tree.path) || @commit @last_commit = @repository.last_commit_for_path(@commit.id, @blob.path)
show_new_repo? ? render('projects/tree/show') : render('show') render 'show'
end end
format.json do format.json do
render_blob_json(@blob) json = blob_json(@blob)
return render_404 unless json
render json: json.merge(
path: blob.path,
name: blob.name,
extension: blob.extension,
size: blob.raw_size,
mime_type: blob.mime_type,
binary: blob.raw_binary?,
simple_viewer: blob.simple_viewer&.class&.partial_name,
rich_viewer: blob.rich_viewer&.class&.partial_name,
show_viewer_switcher: !!blob.show_viewer_switcher?,
raw_path: project_raw_path(project, @id),
blame_path: project_blame_path(project, @id),
commits_path: project_commits_path(project, @id),
permalink: project_blob_path(project, File.join(@commit.id, @path)),
)
end end
end end
end end
......
...@@ -118,7 +118,7 @@ module BlobHelper ...@@ -118,7 +118,7 @@ module BlobHelper
icon("#{file_type_icon_class('file', mode, name)} fw") icon("#{file_type_icon_class('file', mode, name)} fw")
end end
def blob_raw_url def blob_raw_path
if @build && @entry if @build && @entry
raw_project_job_artifacts_path(@project, @build, path: @entry.path) raw_project_job_artifacts_path(@project, @build, path: @entry.path)
elsif @snippet elsif @snippet
...@@ -235,7 +235,7 @@ module BlobHelper ...@@ -235,7 +235,7 @@ module BlobHelper
title = 'Open raw' title = 'Open raw'
end end
link_to icon, blob_raw_url, class: 'btn btn-sm has-tooltip', target: '_blank', rel: 'noopener noreferrer', title: title, data: { container: 'body' } link_to icon, blob_raw_path, class: 'btn btn-sm has-tooltip', target: '_blank', rel: 'noopener noreferrer', title: title, data: { container: 'body' }
end end
def blob_render_error_reason(viewer) def blob_render_error_reason(viewer)
...@@ -270,7 +270,7 @@ module BlobHelper ...@@ -270,7 +270,7 @@ module BlobHelper
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
options << link_to('download it', blob_raw_url, target: '_blank', rel: 'noopener noreferrer') options << link_to('download it', blob_raw_path, target: '_blank', rel: 'noopener noreferrer')
options options
end end
......
...@@ -82,7 +82,7 @@ module BlobViewer ...@@ -82,7 +82,7 @@ module BlobViewer
# format of the blob. # format of the blob.
# #
# Prefer to implement a client-side viewer, where the JS component loads the # Prefer to implement a client-side viewer, where the JS component loads the
# binary from `blob_raw_url` and does its own format validation and error # binary from `blob_raw_path` and does its own format validation and error
# rendering, especially for potentially large binary formats. # rendering, especially for potentially large binary formats.
def render_error def render_error
if too_large? if too_large?
......
...@@ -17,7 +17,7 @@ module BlobViewer ...@@ -17,7 +17,7 @@ module BlobViewer
# build artifacts, can only be rendered using a client-side viewer, # 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 # 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 # server side. Client-side viewers use JS and can fetch the file from
# `blob_raw_url` using AJAX. # `blob_raw_path` using AJAX.
return :server_side_but_stored_externally if blob.stored_externally? return :server_side_but_stored_externally if blob.stored_externally?
super super
......
- commit = local_assigns.fetch(:commit) { @repository.commit } - commit = local_assigns.fetch(:commit) { @repository.commit }
- ref = local_assigns.fetch(:ref) { current_ref } - ref = local_assigns.fetch(:ref) { current_ref }
- project = local_assigns.fetch(:project) { @project } - project = local_assigns.fetch(:project) { @project }
- content_url = local_assigns.fetch(:content_url) { @tree.readme ? project_blob_path(@project, tree_join(@ref, @tree.readme.path)) : project_tree_path(@project, @ref) }
#tree-holder.tree-holder.clearfix #tree-holder.tree-holder.clearfix
.nav-block .nav-block
...@@ -9,4 +10,4 @@ ...@@ -9,4 +10,4 @@
- if !show_new_repo? && commit - if !show_new_repo? && commit
= render 'shared/commit_well', commit: commit, ref: ref, project: project = render 'shared/commit_well', commit: commit, ref: ref, project: project
= render 'projects/tree/tree_content', tree: @tree = render 'projects/tree/tree_content', tree: @tree, content_url: content_url
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
= render "projects/blob/auxiliary_viewer", blob: blob = render "projects/blob/auxiliary_viewer", blob: blob
#blob-content-holder.blob-content-holder #blob-content-holder.blob-content-holder
- if !show_new_repo? - unless show_new_repo?
%article.file-holder %article.file-holder
= render 'projects/blob/header', blob: blob = render 'projects/blob/header', blob: blob
= render 'projects/blob/content', blob: blob = render 'projects/blob/content', blob: blob
...@@ -6,11 +6,17 @@ ...@@ -6,11 +6,17 @@
- content_for :page_specific_javascripts do - content_for :page_specific_javascripts do
= webpack_bundle_tag 'blob' = webpack_bundle_tag 'blob'
- if show_new_repo?
= webpack_bundle_tag 'common_vue' = webpack_bundle_tag 'common_vue'
= webpack_bundle_tag 'repo'
= render 'projects/last_push' = render 'projects/last_push'
%div{ class: container_class } %div{ class: container_class }
- if show_new_repo?
= render 'projects/files', commit: @last_commit, project: @project, ref: @ref, content_url: project_blob_path(@project, @id)
- else
#tree-holder.tree-holder #tree-holder.tree-holder
= render 'blob', blob: @blob = render 'blob', blob: @blob
......
- content_for :page_specific_javascripts do - content_for :page_specific_javascripts do
= page_specific_javascript_bundle_tag('balsamiq_viewer') = page_specific_javascript_bundle_tag('balsamiq_viewer')
.file-content.balsamiq-viewer#js-balsamiq-viewer{ data: { endpoint: blob_raw_url } } .file-content.balsamiq-viewer#js-balsamiq-viewer{ data: { endpoint: blob_raw_path } }
.file-content.blob_file.blob-no-preview .file-content.blob_file.blob-no-preview
.center .center
= link_to blob_raw_url do = link_to blob_raw_path do
%h1.light %h1.light
= icon('download') = icon('download')
%h4 %h4
......
.file-content.image_file .file-content.image_file
= image_tag(blob_raw_url, alt: viewer.blob.name) = image_tag(blob_raw_path, alt: viewer.blob.name)
...@@ -2,4 +2,4 @@ ...@@ -2,4 +2,4 @@
= page_specific_javascript_bundle_tag('common_vue') = page_specific_javascript_bundle_tag('common_vue')
= page_specific_javascript_bundle_tag('notebook_viewer') = page_specific_javascript_bundle_tag('notebook_viewer')
.file-content#js-notebook-viewer{ data: { endpoint: blob_raw_url } } .file-content#js-notebook-viewer{ data: { endpoint: blob_raw_path } }
...@@ -2,4 +2,4 @@ ...@@ -2,4 +2,4 @@
= page_specific_javascript_bundle_tag('common_vue') = page_specific_javascript_bundle_tag('common_vue')
= page_specific_javascript_bundle_tag('pdf_viewer') = page_specific_javascript_bundle_tag('pdf_viewer')
.file-content#js-pdf-viewer{ data: { endpoint: blob_raw_url } } .file-content#js-pdf-viewer{ data: { endpoint: blob_raw_path } }
...@@ -2,6 +2,6 @@ ...@@ -2,6 +2,6 @@
= page_specific_javascript_bundle_tag('common_vue') = page_specific_javascript_bundle_tag('common_vue')
= page_specific_javascript_bundle_tag('sketch_viewer') = page_specific_javascript_bundle_tag('sketch_viewer')
.file-content#js-sketch-viewer{ data: { endpoint: blob_raw_url } } .file-content#js-sketch-viewer{ data: { endpoint: blob_raw_path } }
.js-loading-icon.text-center.prepend-top-default.append-bottom-default.js-loading-icon{ 'aria-label' => 'Loading Sketch preview' } .js-loading-icon.text-center.prepend-top-default.append-bottom-default.js-loading-icon{ 'aria-label' => 'Loading Sketch preview' }
= icon('spinner spin 2x', 'aria-hidden' => 'true'); = icon('spinner spin 2x', 'aria-hidden' => 'true');
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
= page_specific_javascript_bundle_tag('stl_viewer') = page_specific_javascript_bundle_tag('stl_viewer')
.file-content.is-stl-loading .file-content.is-stl-loading
.text-center#js-stl-viewer{ data: { endpoint: blob_raw_url } } .text-center#js-stl-viewer{ data: { endpoint: blob_raw_path } }
= icon('spinner spin 2x', class: 'prepend-top-default append-bottom-default', 'aria-hidden' => 'true', 'aria-label' => 'Loading') = icon('spinner spin 2x', class: 'prepend-top-default append-bottom-default', 'aria-hidden' => 'true', 'aria-label' => 'Loading')
.text-center.prepend-top-default.append-bottom-default.stl-controls .text-center.prepend-top-default.append-bottom-default.stl-controls
.btn-group .btn-group
......
.file-content.video .file-content.video
%video{ src: blob_raw_url, controls: true, data: { setup: '{}' } } %video{ src: blob_raw_path, controls: true, data: { setup: '{}' } }
- content_url = local_assigns.fetch(:content_url, nil)
- if show_new_repo? - if show_new_repo?
= render 'shared/repo/repo', project: @project = render 'shared/repo/repo', project: @project, content_url: content_url
- else - else
= render 'projects/tree/old_tree_content', tree: tree = render 'projects/tree/old_tree_content', tree: tree
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
= icon('long-arrow-right', title: 'to target branch') = icon('long-arrow-right', title: 'to target branch')
= render 'shared/target_switcher', destination: 'tree', path: @path = render 'shared/target_switcher', destination: 'tree', path: @path
- if !show_new_repo? - unless show_new_repo?
= render 'projects/tree/old_tree_header' = render 'projects/tree/old_tree_header'
.tree-controls .tree-controls
......
...@@ -15,4 +15,4 @@ ...@@ -15,4 +15,4 @@
%div{ class: [container_class, ("limit-container-width" unless fluid_layout)] } %div{ class: [container_class, ("limit-container-width" unless fluid_layout)] }
= render 'projects/last_push' = render 'projects/last_push'
= render 'projects/files', commit: @last_commit, project: @project, ref: @ref = render 'projects/files', commit: @last_commit, project: @project, ref: @ref, content_url: project_tree_path(@project, @id)
#repo{ data: { url: repo_url(project), project_name: project.name, refs_url: refs_project_path(project, format: :json), project_url: project_path(project), project_id: project.id } } #repo{ data: { url: content_url, project_name: project.name, refs_url: refs_project_path(project, format: :json), project_url: project_path(project), project_id: project.id } }
...@@ -2616,7 +2616,7 @@ got@^7.0.0: ...@@ -2616,7 +2616,7 @@ got@^7.0.0:
url-parse-lax "^1.0.0" url-parse-lax "^1.0.0"
url-to-options "^1.0.1" url-to-options "^1.0.1"
graceful-fs@^4.1.11, graceful-fs@^4.1.2: graceful-fs@^4.1.11, graceful-fs@^4.1.2, graceful-fs@^4.1.6, graceful-fs@^4.1.9:
version "4.1.11" version "4.1.11"
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.1.11.tgz#0e8bdfe4d1ddb8854d64e04ea7c00e2a026e5658" resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.1.11.tgz#0e8bdfe4d1ddb8854d64e04ea7c00e2a026e5658"
......
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