-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIPFS.js
228 lines (201 loc) · 7.16 KB
/
IPFS.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// page_onstart.push("IPFS.onready=function(){IPFS.List()};IPFS.init()");
IPFS={
// wss://node-shave.zymologia.fi
endpoint: 'https://ipfs.zymologia.fi/', // proxy_pass http://127.0.0.1:8181;
endpointSave: 'https://ipfs.zymologia.fi/OOO_Add_IDDQD', // proxy_pass http://127.0.0.1:5001/api/v0/add?hash=blake3;
endpointRm: 'https://ipfs.zymologia.fi/OOO_Rm_IDDQD',
endpointLs: 'https://ipfs.zymologia.fi/OOO_ls',
// ñþäà ìîæíî çàïèñàòü ñâîþ ôóíêöèþ ïðè ñòàðòå
onready: function(){},
// Çàãðóçèòü áèáëèîòåêó ïåðâûì äåëîì ïðè ñòàðòå ñòðàíèöû
init: async function() {
if(typeof(mainjs)!='undefined') await LOADS_promice(mainjs+'ipfs.js');
else await LOADS_promice('https://unpkg.com/multiformats/dist/index.min.js');
this.onready();
},
// ïåðåâåñòè ìàññèâ áàéò â ñòðîêó âèäà "11 22 33 44 55 66 77 88 99 00 aa bb cc dd ee ff"
HEX: function(ara) {
var o=[]; ara.forEach( (x)=> { o.push( (x >= 16 ? '':'0') + (x & 0xFF).toString(16) ); });
return o.join(' ').toUpperCase();
},
// ïåðåâåñòè ìàññèâ áàéò â ñòðîêó âèäà "0x11223344556677889900aabbccddeeff"
hex0x: function(ara) {
var o="0x"; ara.forEach( (x)=> { o+=( (x >= 16 ? '':'0') + (x & 0xFF).toString(16) ); });
return o;
},
// èçâëå÷ü ìàññèâ 32 áàéò èç àäðåñà ipfs
cid2bytes: function(url) {
if(url.indexOf('/')>=0) {
url=url.split('/');
url=url[url.length-1];
}
return Multiformats.CID.parse(url).multihash.digest;
},
// ïåðåâåñòè çíà÷èìóþ ÷àñòü 32 áàéòà âäðåñà ipfs â ñòðîêó âèäà "0x11223344556677889900aabbccddeeff"
cid2hex: function(url) {
return this.hex0x(this.cid2bytes(url));
},
// ïåðåâåñòè ñòðîêó âèäà "0x11223344556677889900aabbccddeeff" â çíà÷èìóþ ÷àñòü 32 áàéòà âäðåñà ipfs
hex2cid: function(hex) {
hex = (30).toString(16)+(32).toString(16)+(''+hex).replace(/^0x/g,'');
const x = Uint8Array.from(hex.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)));
return Multiformats.CID.createV1(85,{bytes: x}).toString();
},
// äîáàâèòü â íà÷àëå https://ipfs.zymologia.fi/
hex2url: function(hex) {
return this.endpoint+this.hex2cid(hex);
},
// ïî õýøó íàéòè âåðõíèé îáúåêò TR â òàáëèöå âüþâåðà ipfs-list-table
find_tr: function(hash) {
if(typeof(hash)=='object') return (hash.nodeName == 'TR' ? hash : hash.closest('TR') );
if(hash.indexOf('://')<0) hash=this.endpoint+hash;
return idd('ipfs-list-table').querySelector("A[href='"+hash+"']").closest('TR');
},
// âåðíóòü òî÷íî õýø (äàæå åñëè ýòî áûë îáúåêò)
find_hash: function(hash) {
if(typeof(hash)=='object') hash=hash.closest('TR').querySelector('A').href;
return hash.replace(/^.*\//g,'');
},
// Óäàëåíèå ñ Âåáà
Del: function(hash) {
if(!confirm('Delete?')) return;
hash=this.find_hash(hash);
AJAX(this.endpointRm,{
callback:function(o){
try {
var j=JSON.parse(o).Pins;
if(j.length!=1) do_catch_error();
clean( IPFS.find_tr(j[0]) );
clean('ipfs-view');
} catch(e){ alert('error'); }
},
onerror:function(o,u,s){
try { o=print_r(JSON.parse(o)); } catch(e){ o=h(o); }
ohelpc('idie2',
red(h(this.status)+' '+h(this.statusText)),
h(this.responseURL)+'<br>[ '+print_r(s)+' ]<br>'+o.replace(/\n/g,'<br>')
);
},
noajax:1
}, {arg:hash} );
},
// Ìåòîäîì HEAD ïîëó÷èòü content-type è 'content-length' è âûïîëíèòü ñ íèìè çàäàííóþ fn()
Type: function(hash,fn) {
hash=this.find_hash(hash);
AJAX(this.endpoint+hash,{method:'HEAD',callback:function(o,url){
var type=this.getResponseHeader('content-type').replace(/;.+/g,'');
var leng=this.getResponseHeader('content-length');
if(leng===null) leng='';
fn(hash,type,leng);
}});
},
// Âåá-âüþâåð òåêñòîâûõ ôàéëîâ
ViewFile: function(type,url) {
AJAX(this.endpoint+url,function(o){
o=h(o).replace(/\n/g,'<br>');
if(type.indexOf('unknown')>=0) o="<div class='br'>"+o+"</div>";
var header="<i alt='Delete' onclick=\"IPFS.Del('"+url+"')\" class='e_cancel mv'></i> "+h(type);
ohelpc('ipfs-view',header,o);
});
},
// Âüþâåð âñåõ ôàéëîâ (äëÿ òåêñòîâûõ âûçîâåò ViewFile)
View: function(hash) {
var tr=this.find_tr(hash);
var hash=this.find_hash(tr);
var type=tr.getAttribute('content-type') || 'text/plain';
var t=type.split('/')[0];
if(t=='text') IPFS.ViewFile(type,hash);
else if(t=='audio') changemp3x(this.endpoint+hash+'?type=.mp3',hash);
else IPFS.ViewFile('unknown type: '+type,hash);
return false;
},
// Âåá-ýêñïëîðåð ôàéëîâ ipfs
List: function() {
AJ(this.endpointLs,function(o){
try {
var j=JSON.parse(o).Keys;
var o='',i=0;
var tmpl="<tr>"
+"<td>{i}</td>"
+"<td><i alt='Delete' onclick='IPFS.Del(this)' class='e_cancel mv'></i></td>"
+"<td><a class='r' onclick='return IPFS.View(this)' href='{#url}'>{#hash}</a></td>"
+"<td><i class='e_help'></i></td>"
+"<td class='r'></td>"
+"<td class='r leng'></td>"
+"<td>{type}</td>"
+"<td class='br'><a href='{ipfsurl}' target='_blank'>ipfs.io</td>"
+"</tr>";
for(var hash in j) {
var type = j[hash].Type;
if(type == 'recursive') type=green(type); else type=red(type);
o+=mpers(tmpl,{
i: ++i,
type:type,
hash:hash,
url:IPFS.endpoint+hash,
ipfsurl: "https://ipfs.io/ipfs/"+hash,
});
}
o="<table border='0' cellspacing='10' id='ipfs-list-table'>"+o+"</table>";
if(idd('ipfs-list')) zabil('ipfs-list',o); else ohelpc('ipfs-list','IPFS files',o);
for(var hash in j) {
IPFS.Type(hash,function(hash,type,leng){
var tr=IPFS.find_tr(hash);
if(!tr) return;
tr.setAttribute('content-type',type);
tr.querySelector('TD.r').innerHTML=h(type);
tr.querySelector('TD.leng').innerHTML=h(leng);
var t=type.split('/')[0];
var c = 'e_ledpurple'
if(type=='text/plain') c='e_kontact_journal';
else if(type=='text/html') c='e_kontact_notes';
else if(t=='audio') c='e_ljvideo';
else if(t=='video') c='e_play-youtube';
else if(t=='image') c='e_image';
else if(t=='document') c='e_filenew';
tr.querySelector('I.e_help').className=c;
});
}
} catch(e){ alert('error'); }
},{quiet:true,stream:true,type:"recursive"});
},
save: function(s){
return new Promise(function(resolve, reject) {
ajaxon();
var blob = new Blob([s], { type: "text/plain"});
var formData = new FormData();
formData.append("file", blob, "comment.txt");
AJ(IPFS.endpointSave,function(o){
ajaxoff();
try {
var j=JSON.parse(o);
if(j.Hash) resolve(j.Hash);
else resolve(false);
} catch(e){ reject(''+e); }
},formData);
});
},
// Âåá-çàïèñü íîâîé èòåðàöèè â áëîê÷åéí
Save: async function(s) {
s=idd('ipfs_text').value;
var hash=await this.save(s);
if(!hash) idie('Error hash!');
var url=this.endpoint+hash;
var w=idd('new_ipfs'); if(w) dobavil(w,"<div><a target='_blank' href='"+url+"'>"+url+"</a>"
+"<div class='br'><b>"+this.cid2hex(url)+"</b>"
+" <i onclick=\"if(confirm('Delete?'))IPFS.rm('"+url+"')\" class='e_cancel mv'></i>"
+"</div></div>");
this.List();
},
// Óòèëèòêà äëÿ èçó÷åíèÿ íîâûõ JS-áèáëèîòåê
libtest: function(url) {
alert('ok');
return;
var OiShoBylo=[]; for(var i in window) OiShoBylo.push(i);
LOADS_sync(url,function(){
var OiShoStalo={}; Object.assign(OiShoStalo,window);
for(var i of OiShoBylo) delete(OiShoStalo[i]);
dier(OiShoStalo);
});
},
};