Module: UploadsActions

Extended by:
ActiveSupport::Concern
Includes:
Gitlab::Utils::StrongMemoize, SendFileUpload
Included in:
Groups::UploadsController, Projects::UploadsController, UploadsController
Defined in:
app/controllers/concerns/uploads_actions.rb

Constant Summary collapse

UPLOAD_MOUNTS =
%w(avatar attachment file logo header_logo favicon).freeze

Instance Method Summary collapse

Methods included from SendFileUpload

#content_type_for, #guess_content_type, #send_upload

Methods included from Gitlab::Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Instance Method Details

#authorizeObject


55
56
57
58
59
60
61
62
63
64
65
# File 'app/controllers/concerns/uploads_actions.rb', line 55

def authorize
  set_workhorse_internal_api_content_type

  authorized = uploader_class.workhorse_authorize(
    has_length: false,
    maximum_size: Gitlab::CurrentSettings.max_attachment_size.megabytes.to_i)

  render json: authorized
rescue SocketError
  render json: _("Error uploading file"), status: :internal_server_error
end

#createObject


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/controllers/concerns/uploads_actions.rb', line 16

def create
  uploader = UploadService.new(model, params[:file], uploader_class).execute

  respond_to do |format|
    if uploader
      format.json do
        render json: { link: uploader.to_h }
      end
    else
      format.json do
        render json: _('Invalid file.'), status: :unprocessable_entity
      end
    end
  end
end

#showObject

This should either

- send the file directly
- or redirect to its URL

36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/controllers/concerns/uploads_actions.rb', line 36

def show
  return render_404 unless uploader&.exists?

  ttl, directives = *cache_settings
  ttl ||= 0
  directives ||= { private: true, must_revalidate: true }

  expires_in ttl, directives

  file_uploader = [uploader, *uploader.versions.values].find do |version|
    version.filename == params[:filename]
  end

  return render_404 unless file_uploader

  workhorse_set_content_type!
  send_upload(file_uploader, attachment: file_uploader.filename, disposition: content_disposition)
end