1
1
'use strict'
2
2
3
- const IPFS = require ( 'ipfs' )
3
+ // const IPFS = require('ipfs')
4
4
5
5
// Node
6
6
const $nodeId = document . querySelector ( '.node-id' )
@@ -22,6 +22,9 @@ const $allDisabledButtons = document.querySelectorAll('button:disabled')
22
22
const $allDisabledInputs = document . querySelectorAll ( 'input:disabled' )
23
23
const $allDisabledElements = document . querySelectorAll ( '.disabled' )
24
24
25
+ const FILES = [ ]
26
+ const workspace = location . hash
27
+
25
28
let node
26
29
let info
27
30
let Buffer
@@ -33,6 +36,9 @@ let Buffer
33
36
function start ( ) {
34
37
if ( ! node ) {
35
38
const options = {
39
+ EXPERIMENTAL : {
40
+ pubsub : true
41
+ } ,
36
42
repo : 'ipfs-' + Math . random ( ) ,
37
43
config : {
38
44
Addresses : {
@@ -41,7 +47,8 @@ function start () {
41
47
}
42
48
}
43
49
44
- node = new IPFS ( options )
50
+ // node = new IPFS(options)
51
+ node = new window . Ipfs ( options )
45
52
46
53
Buffer = node . types . Buffer
47
54
@@ -52,16 +59,57 @@ function start () {
52
59
updateView ( 'ready' , node )
53
60
onSuccess ( 'Node is ready.' )
54
61
setInterval ( refreshPeerList , 1000 )
62
+ // setInterval(sendFileList, 10000)
55
63
} )
56
64
. catch ( ( error ) => onError ( err ) )
65
+
66
+ subscribeToWorkpsace ( )
57
67
} )
58
68
}
59
69
}
60
70
71
+ /* ===========================================================================
72
+ Pubsub
73
+ =========================================================================== */
74
+
75
+ const messageHandler = ( message ) => {
76
+ const myNode = info . id
77
+ const hash = message . data . toString ( )
78
+ const messageSender = message . from
79
+
80
+ console . log ( 'Node ID:' , myNode )
81
+ console . log ( 'Received message from:' , messageSender )
82
+ console . log ( 'Message:' , hash )
83
+
84
+ // append new files when someone uploads them
85
+ if ( myNode !== messageSender && ! isFileInList ( hash ) ) {
86
+ console . log ( ':: Append new file' )
87
+ $multihashInput . value = hash
88
+ getFile ( )
89
+ }
90
+ }
91
+
92
+ const subscribeToWorkpsace = ( ) => {
93
+ node . pubsub . subscribe ( workspace , messageHandler )
94
+ . then ( ( ) => console . log ( ':: Subscribed to workspace:' , workspace ) )
95
+ }
96
+
97
+ const publishHash = ( hash ) => {
98
+ const data = Buffer . from ( hash )
99
+
100
+ node . pubsub . publish ( workspace , data )
101
+ . then ( ( ) => console . log ( ':: Successfully published to workspace' ) )
102
+ . catch ( ( error ) => console . log ( ':: Error publishing:' , error ) )
103
+ }
104
+
61
105
/* ===========================================================================
62
106
Files handling
63
107
=========================================================================== */
64
108
109
+ const isFileInList = ( hash ) => FILES . indexOf ( hash ) !== - 1
110
+
111
+ const sendFileList = ( ) => FILES . forEach ( ( hash ) => publishHash ( hash ) )
112
+
65
113
function appendFile ( name , hash , size , data ) {
66
114
const file = new window . Blob ( [ data ] , { type : 'application/octet-binary' } )
67
115
const url = window . URL . createObjectURL ( file )
@@ -89,22 +137,27 @@ function appendFile (name, hash, size, data) {
89
137
row . appendChild ( downloadCell )
90
138
91
139
$fileHistory . insertBefore ( row , $fileHistory . firstChild )
140
+
141
+ publishHash ( hash )
92
142
}
93
143
94
144
function getFile ( ) {
95
- const cid = $multihashInput . value
145
+ const hash = $multihashInput . value
96
146
97
147
$multihashInput . value = ''
98
148
99
- if ( ! cid ) {
149
+ if ( ! hash ) {
100
150
return onError ( 'No multihash was inserted.' )
151
+ } else if ( isFileInList ( hash ) ) {
152
+ return onSuccess ( 'The file is already in the current workspace.' )
101
153
}
102
154
103
- node . files . get ( cid )
155
+ node . files . get ( hash )
104
156
. then ( ( files ) => {
105
157
files . forEach ( ( file ) => {
106
158
if ( file . content ) {
107
- appendFile ( file . name , cid , file . size , file . content )
159
+ FILES . push ( hash )
160
+ appendFile ( file . name , hash , file . size , file . content )
108
161
onSuccess ( `The ${ file . name } file was added.` )
109
162
$emptyRow . style . display = 'none'
110
163
}
@@ -260,6 +313,7 @@ function updateView (state, ipfs) {
260
313
const startApplication = ( ) => {
261
314
// Setup event listeners
262
315
$dragContainer . addEventListener ( 'dragenter' , onDragEnter )
316
+ $dragContainer . addEventListener ( 'dragover' , onDragEnter )
263
317
$dragContainer . addEventListener ( 'drop' , onDrop )
264
318
$dragContainer . addEventListener ( 'dragleave' , onDragLeave )
265
319
$fetchButton . addEventListener ( 'click' , getFile )
0 commit comments