activestorage-ipfs 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6761f8c2f4fa18e89e3fda8f197c77db370569c47854df1c40900cc1b96d58ce
4
- data.tar.gz: cf84436a46cd3769f3c8457d9a7c4ad06c02ab96164b06fac5ef6d7b2e7b37af
3
+ metadata.gz: f7447b6d72f0ded377e6c047d37706939ebab0ee056222d9a2483a74a272757a
4
+ data.tar.gz: f8073646a0845875686ffb33c38f3c1d168bd8b8717a4fe93a2519b5e3c5f855
5
5
  SHA512:
6
- metadata.gz: 5b81a67d258e459a5f45550f3c10ceda64cf8675c7b1d519598dc5e81a48791be62b84bbfe969d728cba5c61be2399954394a603a2b850b0ea707d20ff1e0e9f
7
- data.tar.gz: '08bf8641b83c558906f35ef9bf7a2b75aa661a2ddc16994da4eaa554e32168d96c46abae5a6f20dd30b096aedfa1ef89da43c682d18870162cc9239bc113aac8'
6
+ metadata.gz: c47771e35cb1ad5c605c3b1c01244157cc199bb09bbba0ab2c7883c9e638ef6c681ced55dc04a5c5be2f5e735866dbcb813535f166cd1ef28a565255c4cf62f2
7
+ data.tar.gz: 30f1ab2978507691be6eb660fc9dfd9b88eb631e622142e386b19d810590e6e55d60bb8638d1f94b617f085d75bdd8a734e24f3f8f72e2c31911060d42d77cb6
@@ -1,8 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- activestorage-ipfs (0.0.1)
5
- http (~> 3.0)
4
+ activestorage-ipfs (0.0.2)
5
+ http (~> 4.3)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
@@ -12,30 +12,35 @@ GEM
12
12
  diff-lcs (1.3)
13
13
  domain_name (0.5.20190701)
14
14
  unf (>= 0.0.5, < 1.0.0)
15
- http (3.3.0)
15
+ ffi (1.13.0)
16
+ ffi-compiler (1.0.1)
17
+ ffi (>= 1.0.0)
18
+ rake
19
+ http (4.4.1)
16
20
  addressable (~> 2.3)
17
21
  http-cookie (~> 1.0)
18
- http-form_data (~> 2.0)
19
- http_parser.rb (~> 0.6.0)
22
+ http-form_data (~> 2.2)
23
+ http-parser (~> 1.2.0)
20
24
  http-cookie (1.0.3)
21
25
  domain_name (~> 0.5)
22
26
  http-form_data (2.3.0)
23
- http_parser.rb (0.6.0)
27
+ http-parser (1.2.1)
28
+ ffi-compiler (>= 1.0, < 2.0)
24
29
  public_suffix (4.0.5)
25
30
  rake (12.3.3)
26
31
  rspec (3.9.0)
27
32
  rspec-core (~> 3.9.0)
28
33
  rspec-expectations (~> 3.9.0)
29
34
  rspec-mocks (~> 3.9.0)
30
- rspec-core (3.9.1)
31
- rspec-support (~> 3.9.1)
32
- rspec-expectations (3.9.1)
35
+ rspec-core (3.9.2)
36
+ rspec-support (~> 3.9.3)
37
+ rspec-expectations (3.9.2)
33
38
  diff-lcs (>= 1.2.0, < 2.0)
34
39
  rspec-support (~> 3.9.0)
35
40
  rspec-mocks (3.9.1)
36
41
  diff-lcs (>= 1.2.0, < 2.0)
37
42
  rspec-support (~> 3.9.0)
38
- rspec-support (3.9.2)
43
+ rspec-support (3.9.3)
39
44
  unf (0.1.4)
40
45
  unf_ext
41
46
  unf_ext (0.0.7.7)
data/README.md CHANGED
@@ -1,8 +1,7 @@
1
1
  # Activestorage::Ipfs
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/activestorage/ipfs`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ This gem extends the ActiveStorage::Service with an implementation for
4
+ [IPFS](https://ipfs.io)
6
5
 
7
6
  ## Installation
8
7
 
@@ -16,13 +15,61 @@ And then execute:
16
15
 
17
16
  $ bundle install
18
17
 
19
- Or install it yourself as:
18
+ ## Usage
20
19
 
21
- $ gem install activestorage-ipfs
20
+ In your Rails 5.2+ app, run:
22
21
 
23
- ## Usage
22
+ ```
23
+ rails active_storage:install
24
+ ```
25
+
26
+ This will copy over active_storage migration for creating the tables and then run:
27
+
28
+ ```
29
+ rails db:migrate
30
+ ```
31
+ We now need to tell activestorage to use the ipfs service. Declare a Ipfs service in `config/storage.yml`. Each service requires a `api_endpoint` and a `gateway_endpoint`
32
+
33
+ ```yml
34
+ ipfs:
35
+ service: Ipfs
36
+ api_endpoint: http://localhost:5001
37
+ gateway_endpoint: http://localhost:8080
38
+ ```
39
+
40
+ ## Direct Upload
41
+
42
+ Direct uploads can be used by installing `activestorage-ipfs-js` run:
43
+
44
+ ```
45
+ yarn install activestorage-ipfs-js
46
+ ```
47
+
48
+ After installing the js package replace this line in `app/javascript/packs/application.js`
49
+
50
+ ```js
51
+ require("@rails/activestorage").start()
52
+ ```
53
+
54
+ With this line:
55
+
56
+ ```js
57
+ require("activestorage-ipfs-js").start()
58
+ ```
59
+
60
+ We annotate the file inputs with the direct upload url, and the ipfs api url
61
+
62
+ ```
63
+ <%= form.file_field :file, direct_upload: true, data: { ipfs_url: 'http://localhost:5001' } =%>
64
+ ```
65
+ If Ipfs is running at localhost run the following cmds:
66
+
67
+ ```
68
+ ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin '["*"]'
69
+ ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods '["PUT", "GET", "POST"]
70
+ ```
24
71
 
25
- TODO: Write usage instructions here
72
+ That's it! Upon submission the file upload begins
26
73
 
27
74
  ## Development
28
75
 
@@ -32,7 +79,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
79
 
33
80
  ## Contributing
34
81
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/activestorage-ipfs. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/activestorage-ipfs/blob/master/CODE_OF_CONDUCT.md).
82
+ Bug reports and pull requests are welcome on GitHub at https://github.com/mul53/activestorage-ipfs. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/activestorage-ipfs/blob/master/CODE_OF_CONDUCT.md).
36
83
 
37
84
 
38
85
  ## License
@@ -27,5 +27,5 @@ Gem::Specification.new do |spec|
27
27
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
28
  spec.require_paths = ["lib"]
29
29
 
30
- spec.add_dependency 'http', '~> 3.0'
30
+ spec.add_dependency 'http', '~> 4.3'
31
31
  end
@@ -10,14 +10,14 @@ end
10
10
  class ActiveStorage::Blob
11
11
  class << self
12
12
  def create_before_direct_upload!(key: nil, filename:, byte_size:, checksum:, content_type: nil, metadata: nil)
13
- create! key: key, filename: filename, byte_size: byte_size, checksum: checksum, content_type: content_type, metadata: metadata
13
+ find_or_create_by! key: key, filename: filename, byte_size: byte_size, checksum: checksum, content_type: content_type
14
14
  end
15
15
  end
16
16
  end
17
17
 
18
18
  module ActiveStorage
19
19
  class Service::IpfsService < Service
20
- attr_reader :client
20
+ attr_accessor :client
21
21
 
22
22
  def initialize(api_endpoint:, gateway_endpoint:)
23
23
  @client = Ipfs::Client.new api_endpoint, gateway_endpoint
@@ -29,13 +29,22 @@ module ActiveStorage
29
29
  def upload(key, io, checksum: nil, **)
30
30
  instrument :upload, key: key, checksum: checksum do
31
31
  data = @client.add io.path
32
- find_blob(key).update(key: data['Hash'])
33
- # TODO: Ensure integrity of checksum
32
+ cid_key = data['Hash']
33
+
34
+ if blob_exists?(cid_key)
35
+ existing_blob = find_blob(cid_key)
36
+ new_blob = find_blob(key)
37
+ attachment = Attachment.last
38
+
39
+ attachment.update blob_id: existing_blob.id
40
+ new_blob.destroy!
41
+ else
42
+ find_blob(key).update key: cid_key
43
+ end
34
44
  end
35
45
  end
36
46
 
37
47
  def download(key, &block)
38
- puts key
39
48
  if block_given?
40
49
  instrument :streaming_download, key: key do
41
50
  @client.download key, &block
@@ -54,18 +63,18 @@ module ActiveStorage
54
63
  end
55
64
 
56
65
  def url(key, content_type: nil, filename: nil, expires_in: nil, disposition: nil)
57
- @client.build_file_url key, filename.to_s
66
+ instrument :url, key: key do
67
+ @client.build_file_url key, filename.to_s
68
+ end
58
69
  end
59
70
 
60
71
  def exists?(key)
61
- instrument :exist, key: key do |payload|
62
- answer = @client.file_exists?(key)
63
- payload[:exist] = answer
64
- answer
72
+ instrument :exist, key: key do
73
+ @client.file_exists?(key)
65
74
  end
66
75
  end
67
76
 
68
- def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:)
77
+ def url_for_direct_upload(key, expires_in: nil, content_type: nil, content_length: nil, checksum: nil)
69
78
  instrument :url_for_direct_upload, key: key do
70
79
  "#{@client.api_endpoint}/api/v0/add"
71
80
  end
@@ -76,5 +85,9 @@ module ActiveStorage
76
85
  def find_blob(key)
77
86
  Blob.find_by_key key
78
87
  end
88
+
89
+ def blob_exists?(key)
90
+ Blob.exists?(key: key)
91
+ end
79
92
  end
80
93
  end
@@ -1,5 +1,5 @@
1
1
  module ActiveStorage
2
2
  module IpfsService
3
- VERSION = '0.0.1'.freeze
3
+ VERSION = '0.0.2'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activestorage-ipfs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mulenga Bowa
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-06-07 00:00:00.000000000 Z
11
+ date: 2020-06-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: http
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '3.0'
19
+ version: '4.3'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '3.0'
26
+ version: '4.3'
27
27
  description: A ActiveStorage Service
28
28
  email:
29
29
  - mulengabowa53@gmail.com