Class: ExternalUploadManager

Inherits:
Object
  • Object
show all
Defined in:
app/services/external_upload_manager.rb

Defined Under Namespace

Classes: CannotPromoteError, ChecksumMismatchError, DownloadFailedError, SizeMismatchError

Constant Summary collapse

DOWNLOAD_LIMIT =
100.megabytes
SIZE_MISMATCH_BAN_MINUTES =
5
BAN_USER_REDIS_PREFIX =
"ban_user_from_external_uploads_"
UPLOAD_TYPES_EXCLUDED_FROM_UPLOAD_PROMOTION =
["backup"].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(external_upload_stub, upload_create_opts = {}) ⇒ ExternalUploadManager

Returns a new instance of ExternalUploadManager.



95
96
97
98
99
# File 'app/services/external_upload_manager.rb', line 95

def initialize(external_upload_stub, upload_create_opts = {})
  @external_upload_stub = external_upload_stub
  @upload_create_opts = upload_create_opts
  @store = ExternalUploadManager.store_for_upload_type(external_upload_stub.upload_type)
end

Instance Attribute Details

#external_upload_stubObject (readonly)

Returns the value of attribute external_upload_stub.



22
23
24
# File 'app/services/external_upload_manager.rb', line 22

def external_upload_stub
  @external_upload_stub
end

Class Method Details

.ban_user_from_external_uploads!(user:, ban_minutes: 5) ⇒ Object



24
25
26
# File 'app/services/external_upload_manager.rb', line 24

def self.ban_user_from_external_uploads!(user:, ban_minutes: 5)
  Discourse.redis.setex("#{BAN_USER_REDIS_PREFIX}#{user.id}", ban_minutes.minutes.to_i, "1")
end

.create_direct_multipart_upload(current_user:, file_name:, file_size:, upload_type:, metadata: {}) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/services/external_upload_manager.rb', line 54

def self.create_direct_multipart_upload(
  current_user:,
  file_name:,
  file_size:,
  upload_type:,
  metadata: {}
)
  content_type = MiniMime.lookup_by_filename(file_name)&.content_type
  store = store_for_upload_type(upload_type)
  multipart_upload = store.create_multipart(file_name, content_type, metadata: )

  upload_stub =
    ExternalUploadStub.create!(
      key: multipart_upload[:key],
      created_by: current_user,
      original_filename: file_name,
      upload_type: upload_type,
      external_upload_identifier: multipart_upload[:upload_id],
      multipart: true,
      filesize: file_size,
    )

  {
    external_upload_identifier: upload_stub.external_upload_identifier,
    key: upload_stub.key,
    unique_identifier: upload_stub.unique_identifier,
  }
end

.create_direct_upload(current_user:, file_name:, file_size:, upload_type:, metadata: {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/services/external_upload_manager.rb', line 32

def self.create_direct_upload(current_user:, file_name:, file_size:, upload_type:, metadata: {})
  store = store_for_upload_type(upload_type)
  url, signed_headers = store.signed_request_for_temporary_upload(file_name, metadata: )
  key = store.s3_helper.path_from_url(url)

  upload_stub =
    ExternalUploadStub.create!(
      key: key,
      created_by: current_user,
      original_filename: file_name,
      upload_type: upload_type,
      filesize: file_size,
    )

  {
    url: url,
    key: key,
    unique_identifier: upload_stub.unique_identifier,
    signed_headers: signed_headers,
  }
end

.store_for_upload_type(upload_type) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'app/services/external_upload_manager.rb', line 83

def self.store_for_upload_type(upload_type)
  if upload_type == "backup"
    if !SiteSetting.enable_backups? ||
         SiteSetting.backup_location != BackupLocationSiteSetting::S3
      raise Discourse::InvalidAccess.new
    end
    BackupRestore::BackupStore.create
  else
    Discourse.store
  end
end

.user_banned?(user) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'app/services/external_upload_manager.rb', line 28

def self.user_banned?(user)
  Discourse.redis.get("#{BAN_USER_REDIS_PREFIX}#{user.id}") == "1"
end

Instance Method Details

#can_promote?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'app/services/external_upload_manager.rb', line 101

def can_promote?
  external_upload_stub.status == ExternalUploadStub.statuses[:created]
end

#transform!Object



105
106
107
108
109
110
111
112
113
114
115
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
# File 'app/services/external_upload_manager.rb', line 105

def transform!
  raise CannotPromoteError if !can_promote?
  external_upload_stub.update!(status: ExternalUploadStub.statuses[:uploaded])

  # We require that the file size is specified ahead of time, and compare
  # it here to make sure that people are not uploading excessively large
  # files to the external provider. If this happens, the user will be banned
  # from uploading to the external provider for N minutes.
  if external_size != external_upload_stub.filesize
    ExternalUploadManager.ban_user_from_external_uploads!(
      user: external_upload_stub.created_by,
      ban_minutes: SIZE_MISMATCH_BAN_MINUTES,
    )
    raise SizeMismatchError.new(
            "expected: #{external_upload_stub.filesize}, actual: #{external_size}",
          )
  end

  if UPLOAD_TYPES_EXCLUDED_FROM_UPLOAD_PROMOTION.include?(external_upload_stub.upload_type)
    move_to_final_destination
  else
    promote_to_upload
  end
rescue StandardError
  if !SiteSetting.enable_upload_debug_mode
    # We don't need to do anything special to abort multipart uploads here,
    # because at this point (calling promote_to_upload!), the multipart
    # upload would already be complete.
    @store.delete_file(external_upload_stub.key)
    external_upload_stub.destroy!
  else
    external_upload_stub.update(status: ExternalUploadStub.statuses[:failed])
  end

  raise
end