Class: TpCommon::FileStorage::Uploaders::Public

Inherits:
Base
  • Object
show all
Defined in:
lib/tp_common/file_storage/uploaders/public.rb

Overview

Upload a content to file storage used to public like avatar, logo Use in case content to upload is inside of system/server

Constant Summary

Constants inherited from Base

Base::MAX_RETRIES

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from TpCommon::FileStorage::Base

Instance Method Details

#upload(file_key, content, _content_type = nil) ⇒ Object

Upload content to file_key Currently, _content_type is ignore but kept for compatible. Will be removed in next release


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/tp_common/file_storage/uploaders/public.rb', line 11

def upload(file_key, content, _content_type = nil)
  retry_count = 0

  begin
    directory.files.create(
      key: mask_key(file_key),
      body: content,
      public: true)
  rescue StandardError => e
    retry_count += 1
    retry if retry_count < MAX_RETRIES
    raise e
  end

  mask_key(file_key)
end

#url(file_key) ⇒ Object

Get public url from key of file #upload above


30
31
32
# File 'lib/tp_common/file_storage/uploaders/public.rb', line 30

def url(file_key)
  directory.files.get(mask_key(file_key))&.public_url
end