2018-11-20 20:47:30 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module SendsBlob
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
include BlobHelper
|
|
|
|
include SendFileUpload
|
|
|
|
end
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
def send_blob(repository, blob, inline: true, allow_caching: false)
|
2018-11-20 20:47:30 +05:30
|
|
|
if blob
|
|
|
|
headers['X-Content-Type-Options'] = 'nosniff'
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
return if cached_blob?(blob, allow_caching: allow_caching)
|
2018-11-20 20:47:30 +05:30
|
|
|
|
|
|
|
if blob.stored_externally?
|
2020-04-22 19:07:51 +05:30
|
|
|
send_lfs_object(blob, repository.project)
|
2018-11-20 20:47:30 +05:30
|
|
|
else
|
2020-04-22 19:07:51 +05:30
|
|
|
send_git_blob(repository, blob, inline: inline)
|
2018-11-20 20:47:30 +05:30
|
|
|
end
|
|
|
|
else
|
|
|
|
render_404
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
def cached_blob?(blob, allow_caching: false)
|
2023-04-23 21:23:45 +05:30
|
|
|
stale = stale?(strong_etag: blob.id)
|
2018-11-20 20:47:30 +05:30
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
max_age =
|
2018-11-20 20:47:30 +05:30
|
|
|
if @ref && @commit && @ref == @commit.id # rubocop:disable Gitlab/ModuleWithInstanceVariables
|
|
|
|
# This is a link to a commit by its commit SHA. That means that the blob
|
|
|
|
# is immutable. The only reason to invalidate the cache is if the commit
|
|
|
|
# was deleted or if the user lost access to the repository.
|
|
|
|
Blob::CACHE_TIME_IMMUTABLE
|
|
|
|
else
|
|
|
|
# A branch or tag points at this blob. That means that the expected blob
|
|
|
|
# value may change over time.
|
|
|
|
Blob::CACHE_TIME
|
|
|
|
end
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
# Because we are opinionated we set the cache headers ourselves.
|
2023-04-23 21:23:45 +05:30
|
|
|
expires_in(max_age,
|
|
|
|
public: allow_caching, must_revalidate: true, stale_if_error: 5.minutes,
|
|
|
|
stale_while_revalidate: 1.minute, 's-maxage': 1.minute)
|
2022-11-25 23:54:43 +05:30
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
!stale
|
|
|
|
end
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
def send_lfs_object(blob, project)
|
2018-11-20 20:47:30 +05:30
|
|
|
lfs_object = find_lfs_object(blob)
|
|
|
|
|
|
|
|
if lfs_object && lfs_object.project_allowed_access?(project)
|
|
|
|
send_upload(lfs_object.file, attachment: blob.name)
|
|
|
|
else
|
|
|
|
render_404
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_lfs_object(blob)
|
|
|
|
lfs_object = LfsObject.find_by_oid(blob.lfs_oid)
|
|
|
|
if lfs_object && lfs_object.file.exists?
|
|
|
|
lfs_object
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|