Module: ExternalUploadHelpers

Extended by:
ActiveSupport::Concern
Included in:
Admin::BackupsController, UploadsController
Defined in:
lib/external_upload_helpers.rb

Overview

Extends controllers with the methods required to do direct external uploads.

Defined Under Namespace

Classes: ExternalUploadValidationError

Instance Method Summary collapse

Instance Method Details

#abort_multipartObject



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/external_upload_helpers.rb', line 176

def abort_multipart
  external_upload_identifier = params.require(:external_upload_identifier)
  external_upload_stub =
    ExternalUploadStub.find_by(external_upload_identifier: external_upload_identifier)

  # The stub could have already been deleted by an earlier error via
  # ExternalUploadManager, so we consider this a great success if the
  # stub is already gone.
  return render json: success_json if external_upload_stub.blank?

  return render_404 if external_upload_stub.created_by_id != current_user.id
  store = multipart_store(external_upload_stub.upload_type)

  begin
    store.abort_multipart(
      upload_id: external_upload_stub.external_upload_identifier,
      key: external_upload_stub.key,
    )
  rescue Aws::S3::Errors::ServiceError => err
    return(
      render_json_error(
        debug_upload_error(
          err,
          I18n.t(
            "upload.abort_multipart_failure",
            additional_detail: "external upload stub id: #{external_upload_stub.id}",
          ),
        ),
        status: 422,
      )
    )
  end

  external_upload_stub.destroy!

  render json: success_json
end

#batch_presign_multipart_partsObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/external_upload_helpers.rb', line 116

def batch_presign_multipart_parts
  part_numbers = params.require(:part_numbers)
  unique_identifier = params.require(:unique_identifier)

  ##
  # NOTE: This is configurable by hidden site setting because this really is heavily
  # dependent on upload speed. We request 5-10 URLs at a time with this endpoint; for
  # a 1.5GB upload with 5mb parts this could mean 60 requests to the server to get all
  # the part URLs. If the user's upload speed is super fast they may request all 60
  # batches in a minute, if it is slow they may request 5 batches in a minute.
  RateLimiter.new(
    current_user,
    "batch-presign",
    SiteSetting.max_batch_presign_multipart_per_minute,
    1.minute,
  ).performed!

  part_numbers = part_numbers.map { |part_number| validate_part_number(part_number) }

  external_upload_stub =
    ExternalUploadStub.find_by(unique_identifier: unique_identifier, created_by: current_user)
  return render_404 if external_upload_stub.blank?

  return render_404 if !multipart_upload_exists?(external_upload_stub)

  store = multipart_store(external_upload_stub.upload_type)

  presigned_urls = {}
  part_numbers.each do |part_number|
    presigned_urls[part_number] = store.presign_multipart_part(
      upload_id: external_upload_stub.external_upload_identifier,
      key: external_upload_stub.key,
      part_number: part_number,
    )
  end

  render json: { presigned_urls: presigned_urls }
end

#complete_external_uploadObject



67
68
69
70
71
72
73
74
# File 'lib/external_upload_helpers.rb', line 67

def complete_external_upload
  unique_identifier = params.require(:unique_identifier)
  external_upload_stub =
    ExternalUploadStub.find_by(unique_identifier: unique_identifier, created_by: current_user)
  return render_404 if external_upload_stub.blank?

  complete_external_upload_via_manager(external_upload_stub)
end

#complete_multipartObject



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/external_upload_helpers.rb', line 214

def complete_multipart
  unique_identifier = params.require(:unique_identifier)
  parts = params.require(:parts)

  RateLimiter.new(
    current_user,
    "complete-multipart-upload",
    SiteSetting.max_complete_multipart_per_minute,
    1.minute,
  ).performed!

  external_upload_stub =
    ExternalUploadStub.find_by(unique_identifier: unique_identifier, created_by: current_user)
  return render_404 if external_upload_stub.blank?

  return render_404 if !multipart_upload_exists?(external_upload_stub)

  store = multipart_store(external_upload_stub.upload_type)
  parts =
    parts
      .map do |part|
        part_number = part[:part_number]
        etag = part[:etag]
        part_number = validate_part_number(part_number)

        if etag.blank?
          raise Discourse::InvalidParameters.new(
                  "All parts must have an etag and a valid part number",
                )
        end

        # this is done so it's an array of hashes rather than an array of
        # ActionController::Parameters
        { part_number: part_number, etag: etag }
      end
      .sort_by { |part| part[:part_number] }

  begin
    store.complete_multipart(
      upload_id: external_upload_stub.external_upload_identifier,
      key: external_upload_stub.key,
      parts: parts,
    )
  rescue Aws::S3::Errors::ServiceError => err
    return(
      render_json_error(
        debug_upload_error(
          err,
          I18n.t(
            "upload.complete_multipart_failure",
            additional_detail: "external upload stub id: #{external_upload_stub.id}",
          ),
        ),
        status: 422,
      )
    )
  end

  complete_external_upload_via_manager(external_upload_stub)
end

#create_multipartObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/external_upload_helpers.rb', line 76

def create_multipart
  RateLimiter.new(
    current_user,
    "create-multipart-upload",
    SiteSetting.max_create_multipart_per_minute,
    1.minute,
  ).performed!

  file_name = params.require(:file_name)
  file_size = params.require(:file_size).to_i
  upload_type = params.require(:upload_type)

  begin
    validate_before_create_multipart(
      file_name: file_name,
      file_size: file_size,
      upload_type: upload_type,
    )
  rescue ExternalUploadValidationError => err
    return render_json_error(err.message, status: 422)
  end

  begin
    external_upload_data =
      create_direct_multipart_upload do
        ExternalUploadManager.create_direct_multipart_upload(
          current_user: current_user,
          file_name: file_name,
          file_size: file_size,
          upload_type: upload_type,
          metadata: (params[:metadata]),
        )
      end
  rescue ExternalUploadHelpers::ExternalUploadValidationError => err
    return render_json_error(err.message, status: 422)
  end

  render json: external_upload_data
end

#generate_presigned_putObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/external_upload_helpers.rb', line 33

def generate_presigned_put
  RateLimiter.new(
    current_user,
    "generate-presigned-put-upload-stub",
    SiteSetting.max_presigned_put_per_minute,
    1.minute,
  ).performed!

  file_name = params.require(:file_name)
  file_size = params.require(:file_size).to_i
  type = params.require(:type)

  begin
    validate_before_create_direct_upload(
      file_name: file_name,
      file_size: file_size,
      upload_type: type,
    )
  rescue ExternalUploadValidationError => err
    return render_json_error(err.message, status: 422)
  end

  external_upload_data =
    ExternalUploadManager.create_direct_upload(
      current_user: current_user,
      file_name: file_name,
      file_size: file_size,
      upload_type: type,
      metadata: (params[:metadata]),
    )

  render json: external_upload_data
end

#multipart_upload_exists?(external_upload_stub) ⇒ Boolean

Returns:

  • (Boolean)


155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/external_upload_helpers.rb', line 155

def multipart_upload_exists?(external_upload_stub)
  store = multipart_store(external_upload_stub.upload_type)
  begin
    store.list_multipart_parts(
      upload_id: external_upload_stub.external_upload_identifier,
      key: external_upload_stub.key,
      max_parts: 1,
    )
  rescue Aws::S3::Errors::NoSuchUpload => err
    debug_upload_error(
      err,
      I18n.t(
        "upload.external_upload_not_found",
        additional_detail: "path: #{external_upload_stub.key}",
      ),
    )
    return false
  end
  true
end