Class: ActiveStorage::Imgur::Service

Inherits:
Service
  • Object
show all
Defined in:
lib/active_storage/imgur/service.rb

Defined Under Namespace

Classes: NotAnImage

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id:, client_secret:, refresh_token:, access_token:) ⇒ Service

Returns a new instance of Service.



11
12
13
14
15
# File 'lib/active_storage/imgur/service.rb', line 11

def initialize(client_id:, client_secret:, refresh_token:, access_token:)
  @client = ::Imgurapi::Session.instance(
    client_id: client_id, client_secret: client_secret,
    refresh_token: refresh_token, access_token: access_token)
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



9
10
11
# File 'lib/active_storage/imgur/service.rb', line 9

def client
  @client
end

Instance Method Details

#delete(key) ⇒ Object

Delete the file at the key.



61
62
63
64
65
66
67
68
69
# File 'lib/active_storage/imgur/service.rb', line 61

def delete(key)
  instrument :delete, key: key do
    map = find_map_by_key(key)
    if map
      client.image.image_delete(map.imgur_id)
      map.destroy!
    end
  end
end

#delete_prefixed(prefix) ⇒ Object

Delete files at keys starting with the prefix.



72
73
74
75
76
77
78
79
80
# File 'lib/active_storage/imgur/service.rb', line 72

def delete_prefixed(prefix)
  instrument :delete_prefixed, prefix: prefix do
    maps = ActiveStorage::ImgurKeyMapping.by_prefix_key(prefix)
    maps.each do |map|
      client.image.image_delete(map.imgur_id)
      map.destroy!
    end
  end
end

#download(key, &block) ⇒ Object

Return the content of the file at the key.



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/active_storage/imgur/service.rb', line 39

def download(key, &block)
  if block_given?
    instrument :streaming_download, key: key do
      stream(key, &block)
    end
  else
    instrument :download, key: key do
      File.binread file_for(key)
    end
  end
end

#download_chunk(key, range) ⇒ Object

Return the partial content in the byte range of the file at the key.



52
53
54
55
56
57
58
# File 'lib/active_storage/imgur/service.rb', line 52

def download_chunk(key, range)
  instrument :download_chunk, key: key, range: range do
    file = file_for(key)
    file.seek range.begin
    file.read range.size
  end
end

#exist?(key) ⇒ Boolean

Return true if a file exists at the key.

Returns:

  • (Boolean)


83
84
85
86
87
88
89
90
91
# File 'lib/active_storage/imgur/service.rb', line 83

def exist?(key)
  instrument :exist, key: key do |payload|
    id = map_key_to_id(key)
    answer = id.present?

    payload[:exist] = answer
    answer
  end
end

#headers_for_direct_upload(key, content_type:, checksum:) ⇒ Object



130
131
132
# File 'lib/active_storage/imgur/service.rb', line 130

def headers_for_direct_upload(key, content_type:, checksum:, **)
  { "Content-Type" => content_type, "Content-MD5" => checksum }
end

#upload(key, io, checksum: nil) ⇒ Object

Upload the io to the key specified. If a checksum is provided, the service will ensure a match when the upload has completed or raise an ActiveStorage::IntegrityError.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/active_storage/imgur/service.rb', line 19

def upload(key, io, checksum: nil)
  instrument :upload, key: key, checksum: checksum do
    if io.is_a?(StringIO)
      io = string_io_to_file(key, io)
    end

    ensure_integrity_of(key, io, checksum) if checksum
    image = client.image.image_upload(io)

    ActiveStorage::ImgurKeyMapping.create(key: key, imgur_id: image.id)
  end
rescue StandardError => e
  if e.message.match(/must be an image/)
    raise NotAnImage
  else
    raise e
  end
end

#url(key, expires_in:, disposition:, filename:, content_type:) ⇒ Object

Returns a signed, temporary URL for the file at the key. The URL will be valid for the amount of seconds specified in expires_in. You must also provide the disposition (:inline or :attachment), filename, and content_type that you wish the file to be served with on request.



96
97
98
99
100
101
102
103
104
# File 'lib/active_storage/imgur/service.rb', line 96

def url(key, expires_in:, disposition:, filename:, content_type:)
  instrument :url, key: key do |payload|
    image = image_for(key)

    image.link.tap do |url|
      payload[:url] = url
    end
  end
end

#url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:) ⇒ Object

Returns a signed, temporary URL that a direct upload file can be PUT to on the key. The URL will be valid for the amount of seconds specified in expires_in. You must also provide the content_type, content_length, and checksum of the file that will be uploaded. All these attributes will be validated by the service upon upload.



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/active_storage/imgur/service.rb', line 110

def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:)
  instrument :url, key: key do |payload|
    verified_token_with_expiration = ActiveStorage.verifier.generate(
      {
        key: key,
        content_type: content_type,
        content_length: content_length,
        checksum: checksum
      },
      { expires_in: expires_in,
        purpose: :blob_token }
    )

    generated_url = url_helpers.update_rails_imgur_service_url(verified_token_with_expiration, host: current_host)

    payload[:url] = generated_url
    generated_url
  end
end