Module: SendFileUpload

Instance Method Summary collapse

Instance Method Details

#cdn_fronted_url(file, redirect_params) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'app/controllers/concerns/send_file_upload.rb', line 37

def cdn_fronted_url(file, redirect_params)
  if file.respond_to?(:cdn_enabled_url)
    result = file.cdn_enabled_url(request.remote_ip, redirect_params[:query])
    Gitlab::ApplicationContext.push(artifact_used_cdn: result.used_cdn)
    result.url
  else
    file.url(**redirect_params)
  end
end

#content_type_for(attachment) ⇒ Object



47
48
49
50
51
# File 'app/controllers/concerns/send_file_upload.rb', line 47

def content_type_for(attachment)
  return '' unless attachment

  guess_content_type(attachment)
end

#guess_content_type(filename) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'app/controllers/concerns/send_file_upload.rb', line 53

def guess_content_type(filename)
  types = MIME::Types.type_for(filename)

  if types.present?
    types.first.content_type
  else
    "application/octet-stream"
  end
end

#send_upload(file_upload, send_params: {}, redirect_params: {}, attachment: nil, proxy: false, disposition: 'attachment') ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/controllers/concerns/send_file_upload.rb', line 4

def send_upload(file_upload, send_params: {}, redirect_params: {}, attachment: nil, proxy: false, disposition: 'attachment')
  content_type = content_type_for(attachment)

  if attachment
    response_disposition = ActionDispatch::Http::ContentDisposition.format(disposition: disposition, filename: attachment)

    # Response-Content-Type will not override an existing Content-Type in
    # Google Cloud Storage, so the metadata needs to be cleared on GCS for
    # this to work. However, this override works with AWS.
    redirect_params[:query] = { "response-content-disposition" => response_disposition,
                                "response-content-type" => content_type }
    # By default, Rails will send uploads with an extension of .js with a
    # content-type of text/javascript, which will trigger Rails'
    # cross-origin JavaScript protection.
    send_params[:content_type] = 'text/plain' if File.extname(attachment) == '.js'

    send_params.merge!(filename: attachment, disposition: disposition)
  end

  if image_scaling_request?(file_upload)
    location = file_upload.file_storage? ? file_upload.path : file_upload.url
    headers.store(*Gitlab::Workhorse.send_scaled_image(location, params[:width].to_i, content_type))
    head :ok
  elsif file_upload.file_storage?
    send_file file_upload.path, send_params
  elsif file_upload.class.proxy_download_enabled? || proxy
    headers.store(*Gitlab::Workhorse.send_url(file_upload.url(**redirect_params)))
    head :ok
  else
    redirect_to cdn_fronted_url(file_upload, redirect_params)
  end
end