line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::Ethereum::Swarm; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
52303
|
use 5.020002; |
|
1
|
|
|
|
|
2
|
|
4
|
1
|
|
|
1
|
|
4
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
15
|
|
5
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
27
|
|
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
601
|
use LWP::UserAgent; |
|
1
|
|
|
|
|
38671
|
|
|
1
|
|
|
|
|
37
|
|
8
|
1
|
|
|
1
|
|
453
|
use File::Slurp; |
|
1
|
|
|
|
|
10932
|
|
|
1
|
|
|
|
|
48
|
|
9
|
1
|
|
|
1
|
|
6
|
use HTTP::Request (); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
13
|
|
10
|
1
|
|
|
1
|
|
510
|
use JSON; |
|
1
|
|
|
|
|
7809
|
|
|
1
|
|
|
|
|
4
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $VERSION = '0.04'; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=pod |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=encoding utf8 |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=head1 NAME |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
Net::Ethereum::Swarm - Perl Framework for a distributed storage platform and content distribution service Ethereum Swarm. |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=head1 SYNOPSIS |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
# Upload text file to Ethereum Swarm |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
use Net::Ethereum::Swarm; |
31
|
|
|
|
|
|
|
my $uploaded_file_path = $ARGV[0]; |
32
|
|
|
|
|
|
|
my $sw_node = Net::Ethereum::Swarm->new('http://localhost:8500/'); |
33
|
|
|
|
|
|
|
my $rc = $sw_node->_swarp_node_upload_text_file($uploaded_file_path, 'plain/text; charset=UTF-8'); |
34
|
|
|
|
|
|
|
print Dumper($rc), "\n"; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
# Upload binary file to Ethereum Swarm |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
use Net::Ethereum::Swarm; |
40
|
|
|
|
|
|
|
my $uploaded_file_path = $ARGV[0]; |
41
|
|
|
|
|
|
|
my $sw_node = Net::Ethereum::Swarm->new('http://localhost:8500/'); |
42
|
|
|
|
|
|
|
my $rc = $sw_node->_swarp_node_upload_binary_file($uploaded_file_path, 'image/jpeg'); |
43
|
|
|
|
|
|
|
print Dumper($rc), "\n"; |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
# Get manifest by manifest id |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
use Net::Ethereum::Swarm; |
49
|
|
|
|
|
|
|
my $manifest_id = $ARGV[0]; |
50
|
|
|
|
|
|
|
my $sw_node = Net::Ethereum::Swarm->new('http://localhost:8500/'); |
51
|
|
|
|
|
|
|
my $rc = $sw_node->_swarp_node_get_manifest($manifest_id); |
52
|
|
|
|
|
|
|
print Dumper($rc), "\n"; |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
# Get file from Ethereum Swarm |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
use Net::Ethereum::Swarm; |
58
|
|
|
|
|
|
|
my $manifest_id = $ARGV[0]; |
59
|
|
|
|
|
|
|
my $file_path_to_save = $ARGV[1]; |
60
|
|
|
|
|
|
|
my $sw_node = Net::Ethereum::Swarm->new('http://localhost:8500/'); |
61
|
|
|
|
|
|
|
my $rc = $sw_node->_swarp_node_get_file($manifest_id, $file_path_to_save, 'plain/text; charset=UTF-8'); |
62
|
|
|
|
|
|
|
print Dumper($rc), "\n"; |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=head1 DESCRIPTION |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
Net::Ethereum::Swarm - Perl Framework for a distributed storage platform and content distribution service Ethereum Swarm. |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=head1 FUNCTIONS |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=head2 new() |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
my $sw_node = Net::Ethereum::Swarm->new('http://localhost:8500/'); |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=cut |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub new |
81
|
|
|
|
|
|
|
{ |
82
|
0
|
|
|
0
|
1
|
|
my ($this, $swarm_api_url) = @_; |
83
|
0
|
|
|
|
|
|
my $self = {}; |
84
|
0
|
|
|
|
|
|
bless( $self, $this ); |
85
|
|
|
|
|
|
|
|
86
|
0
|
|
|
|
|
|
$self->{api_url} = $swarm_api_url; |
87
|
0
|
|
|
|
|
|
$self->{debug} = 0; |
88
|
|
|
|
|
|
|
|
89
|
0
|
|
|
|
|
|
return $self; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=pod |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=head2 _swarp_node_get_manifest |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
Get manifest by manifest id |
97
|
|
|
|
|
|
|
my $rc = $sw_node->_swarp_node_get_manifest($manifest_id); |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=cut |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub _swarp_node_get_manifest() |
102
|
|
|
|
|
|
|
{ |
103
|
0
|
|
|
0
|
|
|
my ($this, $manifest_id) = @_; |
104
|
0
|
|
|
|
|
|
my $header = ['Content-Type' => 'plain/text']; |
105
|
0
|
|
|
|
|
|
my $rc = $this->_swarp_node_request('GET', 'bzz-list:/'.$manifest_id.'/', $header, ''); |
106
|
0
|
|
|
|
|
|
my $manifest = JSON::decode_json($rc); |
107
|
0
|
|
|
|
|
|
return $manifest; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=pod |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head2 _swarp_node_get_file |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
Get file from Ethereum Swarm |
116
|
|
|
|
|
|
|
my $rc = $sw_node->_swarp_node_get_file($manifest_id, $file_path_to_save, 'plain/text; charset=UTF-8'); |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=cut |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
sub _swarp_node_get_file() |
121
|
|
|
|
|
|
|
{ |
122
|
0
|
|
|
0
|
|
|
my ($this, $manifest_id, $content_type, $path) = @_; |
123
|
0
|
|
|
|
|
|
my $header = ['Content-Type' => $content_type]; |
124
|
0
|
|
|
|
|
|
my $file_content = $this->_swarp_node_request('GET', 'bzz:/'.$manifest_id.'/'.$path, $header, ''); |
125
|
0
|
|
|
|
|
|
return $file_content; |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=pod |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=head2 _swarp_node_upload_text_file |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
Upload text file to Ethereum Swarm |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
my $rc = $sw_node->_swarp_node_upload_text_file($uploaded_file_path, 'plain/text; charset=UTF-8'); |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=cut |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
sub _swarp_node_upload_text_file() |
141
|
|
|
|
|
|
|
{ |
142
|
0
|
|
|
0
|
|
|
my ($this, $uploaded_file_path, $content_type) = @_; |
143
|
|
|
|
|
|
|
|
144
|
0
|
|
|
|
|
|
my $file_content = read_file( $uploaded_file_path, scalar_ref => 1); |
145
|
|
|
|
|
|
|
# my $header = ['Content-Type' => 'plain/text; charset=UTF-8']; |
146
|
0
|
|
|
|
|
|
my $header = ['Content-Type' => $content_type]; |
147
|
0
|
|
|
|
|
|
my $ua_rc = $this->_swarp_node_request('POST', 'bzz:/', $header, $file_content); |
148
|
0
|
|
|
|
|
|
return $ua_rc; |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=pod |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=head2 _swarp_node_upload_binary_file |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
Upload binary file to Ethereum Swarm |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
my $rc = $sw_node->_swarp_node_upload_binary_file($uploaded_file_path, 'image/jpeg'); |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=cut |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
sub _swarp_node_upload_binary_file() |
164
|
|
|
|
|
|
|
{ |
165
|
0
|
|
|
0
|
|
|
my ($this, $uploaded_file_path, $content_type) = @_; |
166
|
|
|
|
|
|
|
|
167
|
0
|
|
|
|
|
|
my $file_content = read_file( $uploaded_file_path , binmode => ':raw' , scalar_ref => 1 ); |
168
|
|
|
|
|
|
|
# my $header = ['Content-Type' => 'image/jpeg']; |
169
|
0
|
|
|
|
|
|
my $header = ['Content-Type' => $content_type]; |
170
|
0
|
|
|
|
|
|
my $ua_rc = $this->_swarp_node_request('POST', 'bzz:/', $header, $file_content); |
171
|
0
|
|
|
|
|
|
return $ua_rc; |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=pod |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=head2 _swarp_node_request |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
Internal method. |
180
|
|
|
|
|
|
|
Send request to Ethereum Swarm |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
my $ua_rc = $this->_swarp_node_request('POST', 'bzz:/', $header, $file_content); |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=cut |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
sub _swarp_node_request() |
188
|
|
|
|
|
|
|
{ |
189
|
0
|
|
|
0
|
|
|
my ($this, $rq_type, $bzz_protocol, $header, $content) = @_; |
190
|
0
|
|
|
|
|
|
my $req = HTTP::Request->new($rq_type, $this->{api_url}.$bzz_protocol, $header, $content); |
191
|
0
|
|
|
|
|
|
my $ua = LWP::UserAgent->new; |
192
|
0
|
|
|
|
|
|
return $ua->request($req)->{ _content }; |
193
|
|
|
|
|
|
|
} |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=pod |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=head2 set_debug_mode |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
Set dubug mode. Debug info printed to console. |
202
|
|
|
|
|
|
|
$node->set_debug_mode($mode); |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
$mode: 1 - debug on, 0 - debug off. |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=cut |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
sub set_debug_mode() |
209
|
|
|
|
|
|
|
{ |
210
|
0
|
|
|
0
|
1
|
|
my ($this, $debug_mode) = @_; |
211
|
0
|
|
|
|
|
|
$this->{debug} = $debug_mode; |
212
|
|
|
|
|
|
|
} |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
1; |
217
|
|
|
|
|
|
|
__END__ |