Class: Service::PinataService

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pinata_api_key:, pinata_secret_api_key:, api_endpoint:, gateway_endpoint:) ⇒ PinataService

Returns a new instance of PinataService.



7
8
9
# File 'lib/active_storage/service/pinata_service.rb', line 7

def initialize(pinata_api_key:, pinata_secret_api_key:, api_endpoint:, gateway_endpoint:)
  @client = Pinata::Client.new pinata_api_key, pinata_secret_api_key, api_endpoint, gateway_endpoint
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



5
6
7
# File 'lib/active_storage/service/pinata_service.rb', line 5

def client
  @client
end

Instance Method Details

#download(key, &block) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/active_storage/service/pinata_service.rb', line 32

def download(key, &block)
  if block_given?
    instrument :streaming_download, key: key do
      @client.download key, &block
    end
  else
    instrument :download, key: key do
      @client.download key
    end
  end
end

#download_chunk(key, range) ⇒ Object



44
45
46
47
48
# File 'lib/active_storage/service/pinata_service.rb', line 44

def download_chunk(key, range)
  instrument :download_chunk, key: key, range: range do
    @client.cat key, range.begin, range.size
  end
end

#exists?(key) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
60
# File 'lib/active_storage/service/pinata_service.rb', line 56

def exists?(key)
  instrument :exist, key: key do
    @client.file_exists?(key)
  end
end

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

File is uploaded to Pinata and a hash is returned which is used to retrieve the file Change the key of the blob to that of the hash



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/active_storage/service/pinata_service.rb', line 14

def upload(key, io, checksum: nil, **)
  instrument :upload, key: key, checksum: checksum do
    data = @client.add io.path
    cid_key = data['IpfsHash']

    if blob_exists?(cid_key)
      existing_blob = find_blob(cid_key)
      new_blob = find_blob(key)
      attachment = Attachment.last
      
      attachment.update blob_id: existing_blob.id
      new_blob.destroy!
    else
      find_blob(key).update key: cid_key
    end
  end
end

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



50
51
52
53
54
# File 'lib/active_storage/service/pinata_service.rb', line 50

def url(key, content_type: nil, filename: nil, expires_in: nil, disposition: nil)
  instrument :url, key: key do
    @client.build_file_url key
  end
end

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



62
63
64
65
66
# File 'lib/active_storage/service/pinata_service.rb', line 62

def url_for_direct_upload(key, expires_in: nil, content_type: nil, content_length: nil, checksum: nil)
  instrument :url_for_direct_upload, key: key do
    "#{@client.api_endpoint}/api/v0/add"
  end
end