Commit 60c323fc authored by Micaël Bergeron's avatar Micaël Bergeron

improvements after code review

parent 90e571a9
module UploadsActions module UploadsActions
include Gitlab::Utils::StrongMemoize include Gitlab::Utils::StrongMemoize
UPLOAD_MOUNTS = %w(avatar attachment file logo header_logo).freeze
def create def create
link_to_file = UploadService.new(model, params[:file], uploader_class).execute link_to_file = UploadService.new(model, params[:file], uploader_class).execute
...@@ -18,19 +20,20 @@ module UploadsActions ...@@ -18,19 +20,20 @@ module UploadsActions
end end
# This should either # This should either
# - find the file and redirect to its URL # - send the file directly
# - send the file # - or redirect to its URL
# #
def show def show
return render_404 unless uploader.exists? return render_404 unless uploader.exists?
# send to the remote URL if uploader.file_storage?
redirect_to uploader.url unless uploader.file_storage? disposition = uploader.image_or_video? ? 'inline' : 'attachment'
expires_in 0.seconds, must_revalidate: true, private: true
# or send the file send_file uploader.file.path, disposition: disposition
disposition = uploader.image_or_video? ? 'inline' : 'attachment' else
expires_in 0.seconds, must_revalidate: true, private: true redirect_to uploader.url
send_file uploader.file.path, disposition: disposition end
end end
private private
...@@ -41,8 +44,7 @@ module UploadsActions ...@@ -41,8 +44,7 @@ module UploadsActions
def upload_mount def upload_mount
mounted_as = params[:mounted_as] mounted_as = params[:mounted_as]
upload_mounts = %w(avatar attachment file logo header_logo) mounted_as if UPLOAD_MOUNTS.include?(mounted_as)
mounted_as if upload_mounts.include? mounted_as
end end
def uploader_mounted? def uploader_mounted?
......
...@@ -2,11 +2,20 @@ module Avatarable ...@@ -2,11 +2,20 @@ module Avatarable
extend ActiveSupport::Concern extend ActiveSupport::Concern
included do included do
class_eval do prepend ShadowMethods
validate :avatar_type, if: ->(user) { user.avatar.present? && user.avatar_changed? }
validates :avatar, file_size: { maximum: 200.kilobytes.to_i }
mount_uploader :avatar, AvatarUploader validate :avatar_type, if: ->(user) { user.avatar.present? && user.avatar_changed? }
validates :avatar, file_size: { maximum: 200.kilobytes.to_i }
mount_uploader :avatar, AvatarUploader
end
module ShadowMethods
def avatar_url(**args)
# We use avatar_path instead of overriding avatar_url because of carrierwave.
# See https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/11001/diffs#note_28659864
avatar_path(only_path: args.fetch(:only_path, true)) || super
end end
end end
...@@ -41,4 +50,5 @@ module Avatarable ...@@ -41,4 +50,5 @@ module Avatarable
url_base + avatar.url url_base + avatar.url
end end
end end
...@@ -128,10 +128,6 @@ class Group < Namespace ...@@ -128,10 +128,6 @@ class Group < Namespace
visibility_level_allowed_by_sub_groups?(level) visibility_level_allowed_by_sub_groups?(level)
end end
def avatar_url(**args)
avatar_path(**args)
end
def lfs_enabled? def lfs_enabled?
return false unless Gitlab.config.lfs.enabled return false unless Gitlab.config.lfs.enabled
return Gitlab.config.lfs.enabled if self[:lfs_enabled].nil? return Gitlab.config.lfs.enabled if self[:lfs_enabled].nil?
......
...@@ -207,13 +207,6 @@ class Note < ActiveRecord::Base ...@@ -207,13 +207,6 @@ class Note < ActiveRecord::Base
current_application_settings.max_attachment_size.megabytes.to_i current_application_settings.max_attachment_size.megabytes.to_i
end end
def attachment_upload(uploader)
return unless attachment_identifier
paths = uploader.store_dirs.map { |store, path| File.join(path, attachment_identifier) }
Upload.where(model: self, uploader: uploader.class.to_s, path: paths)&.last
end
def hook_attrs def hook_attrs
attributes attributes
end end
......
...@@ -930,9 +930,7 @@ class Project < ActiveRecord::Base ...@@ -930,9 +930,7 @@ class Project < ActiveRecord::Base
end end
def avatar_url(**args) def avatar_url(**args)
# We use avatar_path instead of overriding avatar_url because of carrierwave. Gitlab::Routing.url_helpers.project_avatar_url(self) if avatar_in_git
# See https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/11001/diffs#note_28659864
avatar_path(**args) || (Gitlab::Routing.url_helpers.project_avatar_url(self) if avatar_in_git)
end end
# For compatibility with old code # For compatibility with old code
......
...@@ -864,9 +864,7 @@ class User < ActiveRecord::Base ...@@ -864,9 +864,7 @@ class User < ActiveRecord::Base
end end
def avatar_url(size: nil, scale: 2, **args) def avatar_url(size: nil, scale: 2, **args)
# We use avatar_path instead of overriding avatar_url because of carrierwave. GravatarService.new.execute(email, size, scale, username: username)
# See https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/11001/diffs#note_28659864
avatar_path(**args) || GravatarService.new.execute(email, size, scale, username: username)
end end
def primary_email_verified? def primary_email_verified?
......
...@@ -4,8 +4,6 @@ class AttachmentUploader < GitlabUploader ...@@ -4,8 +4,6 @@ class AttachmentUploader < GitlabUploader
prepend ObjectStorage::Extension::RecordsUploads prepend ObjectStorage::Extension::RecordsUploads
include UploaderHelper include UploaderHelper
storage_options Gitlab.config.uploads
private private
def dynamic_segment def dynamic_segment
......
...@@ -4,8 +4,6 @@ class AvatarUploader < GitlabUploader ...@@ -4,8 +4,6 @@ class AvatarUploader < GitlabUploader
include ObjectStorage::Concern include ObjectStorage::Concern
prepend ObjectStorage::Extension::RecordsUploads prepend ObjectStorage::Extension::RecordsUploads
storage_options Gitlab.config.uploads
def exists? def exists?
model.avatar.file && model.avatar.file.present? model.avatar.file && model.avatar.file.present?
end end
......
...@@ -17,10 +17,8 @@ class FileUploader < GitlabUploader ...@@ -17,10 +17,8 @@ class FileUploader < GitlabUploader
attr_accessor :model attr_accessor :model
storage_options Gitlab.config.uploads
def self.root def self.root
File.join(storage_options&.storage_path, 'uploads') File.join(options.storage_path, 'uploads')
end end
def self.absolute_path(upload) def self.absolute_path(upload)
......
class GitlabUploader < CarrierWave::Uploader::Base class GitlabUploader < CarrierWave::Uploader::Base
class_attribute :options
class << self class << self
# DSL setter # DSL setter
def storage_options(options = nil) def storage_options(options)
@storage_options = options if options self.options = options
@storage_options
end end
def root def root
storage_options&.storage_path options.storage_path
end end
# represent the directory namespacing at the class level # represent the directory namespacing at the class level
def base_dir def base_dir
storage_options.fetch('base_dir', '') options.fetch('base_dir', '')
end end
def file_storage? def file_storage?
...@@ -24,6 +25,8 @@ class GitlabUploader < CarrierWave::Uploader::Base ...@@ -24,6 +25,8 @@ class GitlabUploader < CarrierWave::Uploader::Base
end end
end end
storage_options Gitlab.config.uploads
delegate :base_dir, :file_storage?, to: :class delegate :base_dir, :file_storage?, to: :class
def file_cache_storage? def file_cache_storage?
......
class NamespaceFileUploader < FileUploader class NamespaceFileUploader < FileUploader
storage_options Gitlab.config.uploads
# Re-Override # Re-Override
def self.root def self.root
storage_options&.storage_path options.storage_path
end end
def self.base_dir(model) def self.base_dir(model)
File.join(storage_options&.base_dir, 'namespace', model_path_segment(model)) File.join(options.base_dir, 'namespace', model_path_segment(model))
end end
def self.model_path_segment(model) def self.model_path_segment(model)
......
class PersonalFileUploader < FileUploader class PersonalFileUploader < FileUploader
storage_options Gitlab.config.uploads
# Re-Override # Re-Override
def self.root def self.root
storage_options&.storage_path options.storage_path
end end
def self.base_dir(model) def self.base_dir(model)
File.join(storage_options&.base_dir, model_path_segment(model)) File.join(options.base_dir, model_path_segment(model))
end end
def self.model_path_segment(model) def self.model_path_segment(model)
......
...@@ -30,7 +30,7 @@ module ObjectStorage ...@@ -30,7 +30,7 @@ module ObjectStorage
paths = store_dirs.map { |store, path| File.join(path, identifier) } paths = store_dirs.map { |store, path| File.join(path, identifier) }
unless current_upload_satisfies?(paths, model) unless current_upload_satisfies?(paths, model)
# we already have the right upload, don't fetch # the upload we already have isn't right, find the correct one
self.upload = uploads.find_by(model: model, path: paths) self.upload = uploads.find_by(model: model, path: paths)
end end
...@@ -56,7 +56,7 @@ module ObjectStorage ...@@ -56,7 +56,7 @@ module ObjectStorage
paths.include?(upload.path) && paths.include?(upload.path) &&
upload.model_id == model.id && upload.model_id == model.id &&
upload.model_type == model.class.to_s upload.model_type == model.base_class.sti_name
end end
end end
end end
...@@ -73,23 +73,23 @@ module ObjectStorage ...@@ -73,23 +73,23 @@ module ObjectStorage
class_methods do class_methods do
def object_store_options def object_store_options
storage_options&.object_store options.object_store
end end
def object_store_enabled? def object_store_enabled?
object_store_options&.enabled object_store_options.enabled
end end
def background_upload_enabled? def background_upload_enabled?
object_store_options&.background_upload object_store_options.background_upload
end end
def object_store_credentials def object_store_credentials
object_store_options&.connection&.to_hash&.deep_symbolize_keys object_store_options.connection.to_hash.deep_symbolize_keys
end end
def remote_store_path def remote_store_path
object_store_options&.remote_directory object_store_options.remote_directory
end end
def licensed? def licensed?
...@@ -252,7 +252,7 @@ module ObjectStorage ...@@ -252,7 +252,7 @@ module ObjectStorage
# this is a hack around CarrierWave. The #migrate method needs to be # this is a hack around CarrierWave. The #migrate method needs to be
# able to force the current file to the migrated file upon success. # able to force the current file to the migrated file upon success.
def file=(file) def file=(file)
@file = file @file = file # rubocop:disable Gitlab/ModuleWithInstanceVariables
end end
def serialization_column def serialization_column
......
...@@ -180,6 +180,7 @@ describe UploadsController do ...@@ -180,6 +180,7 @@ describe UploadsController do
it_behaves_like 'content not cached without revalidation' do it_behaves_like 'content not cached without revalidation' do
subject do subject do
get :show, model: 'user', mounted_as: 'avatar', id: user.id, filename: 'image.png' get :show, model: 'user', mounted_as: 'avatar', id: user.id, filename: 'image.png'
response response
end end
end end
...@@ -196,6 +197,7 @@ describe UploadsController do ...@@ -196,6 +197,7 @@ describe UploadsController do
it_behaves_like 'content not cached without revalidation' do it_behaves_like 'content not cached without revalidation' do
subject do subject do
get :show, model: 'user', mounted_as: 'avatar', id: user.id, filename: 'image.png' get :show, model: 'user', mounted_as: 'avatar', id: user.id, filename: 'image.png'
response response
end end
end end
...@@ -213,12 +215,14 @@ describe UploadsController do ...@@ -213,12 +215,14 @@ describe UploadsController do
context "when not signed in" do context "when not signed in" do
it "responds with status 200" do it "responds with status 200" do
get :show, model: "project", mounted_as: "avatar", id: project.id, filename: "image.png" get :show, model: "project", mounted_as: "avatar", id: project.id, filename: "image.png"
expect(response).to have_gitlab_http_status(200) expect(response).to have_gitlab_http_status(200)
end end
it_behaves_like 'content not cached without revalidation' do it_behaves_like 'content not cached without revalidation' do
subject do subject do
get :show, model: 'project', mounted_as: 'avatar', id: project.id, filename: 'image.png' get :show, model: 'project', mounted_as: 'avatar', id: project.id, filename: 'image.png'
response response
end end
end end
...@@ -238,6 +242,7 @@ describe UploadsController do ...@@ -238,6 +242,7 @@ describe UploadsController do
it_behaves_like 'content not cached without revalidation' do it_behaves_like 'content not cached without revalidation' do
subject do subject do
get :show, model: 'project', mounted_as: 'avatar', id: project.id, filename: 'image.png' get :show, model: 'project', mounted_as: 'avatar', id: project.id, filename: 'image.png'
response response
end end
end end
...@@ -290,6 +295,7 @@ describe UploadsController do ...@@ -290,6 +295,7 @@ describe UploadsController do
it_behaves_like 'content not cached without revalidation' do it_behaves_like 'content not cached without revalidation' do
subject do subject do
get :show, model: 'project', mounted_as: 'avatar', id: project.id, filename: 'image.png' get :show, model: 'project', mounted_as: 'avatar', id: project.id, filename: 'image.png'
response response
end end
end end
...@@ -321,6 +327,7 @@ describe UploadsController do ...@@ -321,6 +327,7 @@ describe UploadsController do
it_behaves_like 'content not cached without revalidation' do it_behaves_like 'content not cached without revalidation' do
subject do subject do
get :show, model: 'group', mounted_as: 'avatar', id: group.id, filename: 'image.png' get :show, model: 'group', mounted_as: 'avatar', id: group.id, filename: 'image.png'
response response
end end
end end
...@@ -340,6 +347,7 @@ describe UploadsController do ...@@ -340,6 +347,7 @@ describe UploadsController do
it_behaves_like 'content not cached without revalidation' do it_behaves_like 'content not cached without revalidation' do
subject do subject do
get :show, model: 'group', mounted_as: 'avatar', id: group.id, filename: 'image.png' get :show, model: 'group', mounted_as: 'avatar', id: group.id, filename: 'image.png'
response response
end end
end end
...@@ -383,6 +391,7 @@ describe UploadsController do ...@@ -383,6 +391,7 @@ describe UploadsController do
it_behaves_like 'content not cached without revalidation' do it_behaves_like 'content not cached without revalidation' do
subject do subject do
get :show, model: 'group', mounted_as: 'avatar', id: group.id, filename: 'image.png' get :show, model: 'group', mounted_as: 'avatar', id: group.id, filename: 'image.png'
response response
end end
end end
...@@ -419,6 +428,7 @@ describe UploadsController do ...@@ -419,6 +428,7 @@ describe UploadsController do
it_behaves_like 'content not cached without revalidation' do it_behaves_like 'content not cached without revalidation' do
subject do subject do
get :show, model: 'note', mounted_as: 'attachment', id: note.id, filename: 'image.png' get :show, model: 'note', mounted_as: 'attachment', id: note.id, filename: 'image.png'
response response
end end
end end
...@@ -438,6 +448,7 @@ describe UploadsController do ...@@ -438,6 +448,7 @@ describe UploadsController do
it_behaves_like 'content not cached without revalidation' do it_behaves_like 'content not cached without revalidation' do
subject do subject do
get :show, model: 'note', mounted_as: 'attachment', id: note.id, filename: 'image.png' get :show, model: 'note', mounted_as: 'attachment', id: note.id, filename: 'image.png'
response response
end end
end end
...@@ -490,6 +501,7 @@ describe UploadsController do ...@@ -490,6 +501,7 @@ describe UploadsController do
it_behaves_like 'content not cached without revalidation' do it_behaves_like 'content not cached without revalidation' do
subject do subject do
get :show, model: 'note', mounted_as: 'attachment', id: note.id, filename: 'image.png' get :show, model: 'note', mounted_as: 'attachment', id: note.id, filename: 'image.png'
response response
end end
end end
...@@ -521,6 +533,7 @@ describe UploadsController do ...@@ -521,6 +533,7 @@ describe UploadsController do
it_behaves_like 'content not cached without revalidation' do it_behaves_like 'content not cached without revalidation' do
subject do subject do
get :show, model: 'appearance', mounted_as: 'header_logo', id: appearance.id, filename: 'dk.png' get :show, model: 'appearance', mounted_as: 'header_logo', id: appearance.id, filename: 'dk.png'
response response
end end
end end
...@@ -540,6 +553,7 @@ describe UploadsController do ...@@ -540,6 +553,7 @@ describe UploadsController do
it_behaves_like 'content not cached without revalidation' do it_behaves_like 'content not cached without revalidation' do
subject do subject do
get :show, model: 'appearance', mounted_as: 'logo', id: appearance.id, filename: 'dk.png' get :show, model: 'appearance', mounted_as: 'logo', id: appearance.id, filename: 'dk.png'
response response
end end
end end
......
...@@ -4,7 +4,7 @@ shared_context 'with storage' do |store, **stub_params| ...@@ -4,7 +4,7 @@ shared_context 'with storage' do |store, **stub_params|
end end
end end
shared_examples "migrates" do | to_store: , from_store: nil | shared_examples "migrates" do |to_store: , from_store: nil|
let(:to) { to_store } let(:to) { to_store }
let(:from) { from_store || subject.object_store } let(:from) { from_store || subject.object_store }
......
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