Class: Service::IpfsService

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_endpoint:, gateway_endpoint:) ⇒ IpfsService

Returns a new instance of IpfsService.



22
23
24
# File 'lib/active_storage/service/ipfs_service.rb', line 22

def initialize(api_endpoint:, gateway_endpoint:)
  @client = Ipfs::Client.new api_endpoint, gateway_endpoint
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



20
21
22
# File 'lib/active_storage/service/ipfs_service.rb', line 20

def client
  @client
end

Instance Method Details

#download(key, &block) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/active_storage/service/ipfs_service.rb', line 47

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



59
60
61
62
63
# File 'lib/active_storage/service/ipfs_service.rb', line 59

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)


71
72
73
74
75
# File 'lib/active_storage/service/ipfs_service.rb', line 71

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

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

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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/active_storage/service/ipfs_service.rb', line 29

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

    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



65
66
67
68
69
# File 'lib/active_storage/service/ipfs_service.rb', line 65

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

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



77
78
79
80
81
# File 'lib/active_storage/service/ipfs_service.rb', line 77

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