Commit f711b2df authored by Douwe Maan's avatar Douwe Maan
Browse files

Clean up some backend

parent 95e83935ada1
module RendersBlob
extend ActiveSupport::Concern
def render_blob_json(blob)
def blob_json(blob)
viewer =
case params[:viewer]
when 'rich'
......@@ -11,27 +11,19 @@ def render_blob_json(blob)
else
blob.simple_viewer
end
return render_404 unless viewer
if blob.binary?
render json: {
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
return unless viewer
{
html: view_to_html_string("projects/blob/_viewer", viewer: viewer, load_async: false)
}
end
def render_blob_json(blob)
json = blob_json(blob)
return render_404 unless json
render json: json
end
def conditionally_expand_blob(blob)
......
......@@ -4,7 +4,6 @@ class Projects::BlobController < Projects::ApplicationController
include CreatesCommit
include RendersBlob
include ActionView::Helpers::SanitizeHelper
include ApplicationHelper
# Raised when given an invalid file path
InvalidPathError = Class.new(StandardError)
......@@ -38,17 +37,32 @@ def show
respond_to do |format|
format.html do
assign_ref_vars
environment_params = @repository.branch_exists?(@ref) ? { ref: @ref } : { commit: @commit }
@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
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
......
......@@ -118,7 +118,7 @@ def blob_icon(mode, name)
icon("#{file_type_icon_class('file', mode, name)} fw")
end
def blob_raw_url
def blob_raw_path
if @build && @entry
raw_project_job_artifacts_path(@project, @build, path: @entry.path)
elsif @snippet
......@@ -235,7 +235,7 @@ def open_raw_blob_button(blob)
title = 'Open raw'
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
def blob_render_error_reason(viewer)
......@@ -270,7 +270,7 @@ def blob_render_error_options(viewer)
options << link_to('view the source', '#', class: 'js-blob-viewer-switch-btn', data: { viewer: 'simple' })
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
end
......
......@@ -82,7 +82,7 @@ def binary_detected_after_load?
# format of the blob.
#
# 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.
def render_error
if too_large?
......
......@@ -17,7 +17,7 @@ def render_error
# 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.
# `blob_raw_path` using AJAX.
return :server_side_but_stored_externally if blob.stored_externally?
super
......
- commit = local_assigns.fetch(:commit) { @repository.commit }
- ref = local_assigns.fetch(:ref) { current_ref }
- 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
.nav-block
......@@ -9,4 +10,4 @@
- if !show_new_repo? && commit
= 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 @@
= render "projects/blob/auxiliary_viewer", blob: blob
#blob-content-holder.blob-content-holder
- if !show_new_repo?
- unless show_new_repo?
%article.file-holder
= render 'projects/blob/header', blob: blob
= render 'projects/blob/content', blob: blob
......@@ -6,16 +6,22 @@
- content_for :page_specific_javascripts do
= webpack_bundle_tag 'blob'
= webpack_bundle_tag 'common_vue'
- if show_new_repo?
= webpack_bundle_tag 'common_vue'
= webpack_bundle_tag 'repo'
= render 'projects/last_push'
%div{ class: container_class }
#tree-holder.tree-holder
= render 'blob', blob: @blob
- 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
= render 'blob', blob: @blob
- if can_modify_blob?(@blob)
= render 'projects/blob/remove'
- if can_modify_blob?(@blob)
= render 'projects/blob/remove'
- title = "Replace #{@blob.name}"
= render 'projects/blob/upload', title: title, placeholder: title, button_title: 'Replace file', form_path: project_update_blob_path(@project, @id), method: :put
- title = "Replace #{@blob.name}"
= render 'projects/blob/upload', title: title, placeholder: title, button_title: 'Replace file', form_path: project_update_blob_path(@project, @id), method: :put
- content_for :page_specific_javascripts do
= 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
.center
= link_to blob_raw_url do
= link_to blob_raw_path do
%h1.light
= icon('download')
%h4
......
.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 @@
= page_specific_javascript_bundle_tag('common_vue')
= 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 @@
= page_specific_javascript_bundle_tag('common_vue')
= 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 @@
= page_specific_javascript_bundle_tag('common_vue')
= 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' }
= icon('spinner spin 2x', 'aria-hidden' => 'true');
......@@ -2,7 +2,7 @@
= page_specific_javascript_bundle_tag('stl_viewer')
.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')
.text-center.prepend-top-default.append-bottom-default.stl-controls
.btn-group
......
.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?
= render 'shared/repo/repo', project: @project
= render 'shared/repo/repo', project: @project, content_url: content_url
- else
= render 'projects/tree/old_tree_content', tree: tree
......@@ -5,7 +5,7 @@
= icon('long-arrow-right', title: 'to target branch')
= render 'shared/target_switcher', destination: 'tree', path: @path
- if !show_new_repo?
- unless show_new_repo?
= render 'projects/tree/old_tree_header'
.tree-controls
......
......@@ -15,4 +15,4 @@
%div{ class: [container_class, ("limit-container-width" unless fluid_layout)] }
= 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 } }
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