activestorage-ipfs 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +15 -10
- data/README.md +55 -8
- data/activestorage-ipfs.gemspec +1 -1
- data/lib/active_storage/service/ipfs_service.rb +24 -11
- data/lib/active_storage/service/version.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f7447b6d72f0ded377e6c047d37706939ebab0ee056222d9a2483a74a272757a
|
4
|
+
data.tar.gz: f8073646a0845875686ffb33c38f3c1d168bd8b8717a4fe93a2519b5e3c5f855
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c47771e35cb1ad5c605c3b1c01244157cc199bb09bbba0ab2c7883c9e638ef6c681ced55dc04a5c5be2f5e735866dbcb813535f166cd1ef28a565255c4cf62f2
|
7
|
+
data.tar.gz: 30f1ab2978507691be6eb660fc9dfd9b88eb631e622142e386b19d810590e6e55d60bb8638d1f94b617f085d75bdd8a734e24f3f8f72e2c31911060d42d77cb6
|
data/Gemfile.lock
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
activestorage-ipfs (0.0.
|
5
|
-
http (~> 3
|
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
|
-
|
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.
|
19
|
-
|
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
|
-
|
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.
|
31
|
-
rspec-support (~> 3.9.
|
32
|
-
rspec-expectations (3.9.
|
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.
|
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
|
-
|
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
|
-
|
18
|
+
## Usage
|
20
19
|
|
21
|
-
|
20
|
+
In your Rails 5.2+ app, run:
|
22
21
|
|
23
|
-
|
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
|
-
|
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/
|
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
|
data/activestorage-ipfs.gemspec
CHANGED
@@ -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
|
-
|
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
|
-
|
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
|
-
|
33
|
-
|
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
|
-
|
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
|
62
|
-
|
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
|
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
|
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.
|
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-
|
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
|
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
|
26
|
+
version: '4.3'
|
27
27
|
description: A ActiveStorage Service
|
28
28
|
email:
|
29
29
|
- mulengabowa53@gmail.com
|