Commit f8bedb75 authored by Douwe Maan's avatar Douwe Maan

Merge branch '23705-add-single-file-download-in-repo' into 'master'

Resolve "Ability to download single file"

Closes #23705

See merge request gitlab-org/gitlab-ce!20480
parents 470e526a 3239860f
...@@ -10,7 +10,6 @@ class Projects::RawController < Projects::ApplicationController ...@@ -10,7 +10,6 @@ class Projects::RawController < Projects::ApplicationController
def show def show
@blob = @repository.blob_at(@commit.id, @path) @blob = @repository.blob_at(@commit.id, @path)
if @blob if @blob
headers['X-Content-Type-Options'] = 'nosniff' headers['X-Content-Type-Options'] = 'nosniff'
...@@ -19,7 +18,7 @@ class Projects::RawController < Projects::ApplicationController ...@@ -19,7 +18,7 @@ class Projects::RawController < Projects::ApplicationController
if @blob.stored_externally? if @blob.stored_externally?
send_lfs_object send_lfs_object
else else
send_git_blob @repository, @blob send_git_blob @repository, @blob, inline: (params[:inline] != 'false')
end end
else else
render_404 render_404
......
...@@ -114,22 +114,22 @@ module BlobHelper ...@@ -114,22 +114,22 @@ 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(only_path: false) def blob_raw_url(**kwargs)
if @build && @entry if @build && @entry
raw_project_job_artifacts_url(@project, @build, path: @entry.path, only_path: only_path) raw_project_job_artifacts_url(@project, @build, path: @entry.path, **kwargs)
elsif @snippet elsif @snippet
if @snippet.project_id if @snippet.project_id
raw_project_snippet_url(@project, @snippet, only_path: only_path) raw_project_snippet_url(@project, @snippet, **kwargs)
else else
raw_snippet_url(@snippet, only_path: only_path) raw_snippet_url(@snippet, **kwargs)
end end
elsif @blob elsif @blob
project_raw_url(@project, @id, only_path: only_path) project_raw_url(@project, @id, **kwargs)
end end
end end
def blob_raw_path def blob_raw_path(**kwargs)
blob_raw_url(only_path: true) blob_raw_url(**kwargs, only_path: true)
end end
# SVGs can contain malicious JavaScript; only include whitelisted # SVGs can contain malicious JavaScript; only include whitelisted
...@@ -226,16 +226,17 @@ module BlobHelper ...@@ -226,16 +226,17 @@ module BlobHelper
def open_raw_blob_button(blob) def open_raw_blob_button(blob)
return if blob.empty? return if blob.empty?
return if blob.raw_binary? || blob.stored_externally?
if blob.raw_binary? || blob.stored_externally?
icon = sprite_icon('download')
title = 'Download'
else
icon = icon('file-code-o')
title = 'Open raw' title = 'Open raw'
link_to icon('file-code-o'), blob_raw_path, class: 'btn btn-sm has-tooltip', target: '_blank', rel: 'noopener noreferrer', title: title, data: { container: 'body' }
end end
link_to icon, blob_raw_path, class: 'btn btn-sm has-tooltip', target: '_blank', rel: 'noopener noreferrer', title: title, data: { container: 'body' } def download_blob_button(blob)
return if blob.empty?
title = 'Download'
link_to sprite_icon('download'), blob_raw_path(inline: false), download: @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)
......
...@@ -2,9 +2,9 @@ ...@@ -2,9 +2,9 @@
# Workhorse will also serve files when using `send_file`. # Workhorse will also serve files when using `send_file`.
module WorkhorseHelper module WorkhorseHelper
# Send a Git blob through Workhorse # Send a Git blob through Workhorse
def send_git_blob(repository, blob) def send_git_blob(repository, blob, inline: true)
headers.store(*Gitlab::Workhorse.send_git_blob(repository, blob)) headers.store(*Gitlab::Workhorse.send_git_blob(repository, blob))
headers['Content-Disposition'] = 'inline' headers['Content-Disposition'] = inline ? 'inline' : 'attachment'
headers['Content-Type'] = safe_content_type(blob) headers['Content-Type'] = safe_content_type(blob)
render plain: "" render plain: ""
end end
......
...@@ -27,6 +27,6 @@ ...@@ -27,6 +27,6 @@
.btn-group{ role: "group" }< .btn-group{ role: "group" }<
= copy_blob_source_button(blob) = copy_blob_source_button(blob)
= open_raw_blob_button(blob) = download_blob_button(blob)
= render 'projects/blob/content', blob: blob = render 'projects/blob/content', blob: blob
...@@ -8,8 +8,8 @@ ...@@ -8,8 +8,8 @@
.btn-group{ role: "group" }< .btn-group{ role: "group" }<
= copy_blob_source_button(blob) unless blame = copy_blob_source_button(blob) unless blame
= open_raw_blob_button(blob) = open_raw_blob_button(blob)
= download_blob_button(blob)
= view_on_environment_button(@commit.sha, @path, @environment) if @environment = view_on_environment_button(@commit.sha, @path, @environment) if @environment
.btn-group{ role: "group" }< .btn-group{ role: "group" }<
= render_if_exists 'projects/blob/header_file_locks_link' = render_if_exists 'projects/blob/header_file_locks_link'
= edit_blob_button = edit_blob_button
......
---
title: Add download button for single file (including raw files) in repository
merge_request: 20480
author: Kia Mei Somabes
type: added
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